From 7d72b16f7075dd6397763af606d0a7d6a256dc37 Mon Sep 17 00:00:00 2001 From: Erin Claudio Date: Tue, 2 Jul 2024 14:19:03 -0400 Subject: [PATCH] Update to controller variables --- .../checklist/task_template_controller.rb | 67 +++++++++++++++++++ .../checklist/task_template_policy.rb | 12 ---- .../checklist/task_template_policy.rb | 14 ++++ config/routes.rb | 2 +- 4 files changed, 82 insertions(+), 13 deletions(-) create mode 100644 app/controllers/organizations/staff/checklist/task_template_controller.rb delete mode 100644 app/policies/checklist/task_template_policy.rb create mode 100644 app/policies/organizations/checklist/task_template_policy.rb diff --git a/app/controllers/organizations/staff/checklist/task_template_controller.rb b/app/controllers/organizations/staff/checklist/task_template_controller.rb new file mode 100644 index 000000000..e80a854a4 --- /dev/null +++ b/app/controllers/organizations/staff/checklist/task_template_controller.rb @@ -0,0 +1,67 @@ +module Organizations + module Staff + module Checklist + class TaskTemplatesController < Organizations::BaseController + before_action :context_authorize!, only: %i[index new create] + before_action :set_task, only: %i[edit update destroy] + + layout "dashboard" + + def index + @task_templates = authorized_scope(TaskTemplate.all) + end + + def new + @task_template = TaskTemplate.new + end + + def create + @task_template = TaskTemplate.new(task_params) + + if @task_template + redirect_to staff_task_templates_path, notice: t(".success") + else + flash.now[:alert] = t(".error") + render :new, status: :unprocessable_entity + end + end + + def edit + end + + def update + if @task_template.update(task_params) + redirect_to staff_task_templates_path, notice: t(".success") + else + render :edit, status: :unprocessable_entity + end + end + + def destroy + @task_template.destroy + + redirect_to staff_task_templates_path, notice: t(".success") + end + + private + + def task_params + params.require(:task_template).permit(:name, :description, :due_in_days, :recurring) + end + + def set_task + @task_template = TaskTemplate.find(params[:id]) + + authorize! @task_template + rescue ActiveRecord::RecordNotFound + redirect_to staff_task_templates_path, alert: t(".error") + end + + def context_authorize! + authorize! TaskTemplate, + context: {organization: Current.organization} + end + end + end + end +end diff --git a/app/policies/checklist/task_template_policy.rb b/app/policies/checklist/task_template_policy.rb deleted file mode 100644 index febcfd5ab..000000000 --- a/app/policies/checklist/task_template_policy.rb +++ /dev/null @@ -1,12 +0,0 @@ -module Checklist - class Organizations::TaskTemplatePolicy < ApplicationPolicy - pre_check :verify_organization! - pre_check :verify_active_staff! - - alias_rule :new?, :create?, :index?, to: :manage? - - def manage? - permission?(:manage_task_templates) - end - end -end diff --git a/app/policies/organizations/checklist/task_template_policy.rb b/app/policies/organizations/checklist/task_template_policy.rb new file mode 100644 index 000000000..86688e4fd --- /dev/null +++ b/app/policies/organizations/checklist/task_template_policy.rb @@ -0,0 +1,14 @@ +module Organizations + module Checklist + class TaskTemplatePolicy < ApplicationPolicy + pre_check :verify_organization! + pre_check :verify_active_staff! + + alias_rule :new?, :create?, :index?, to: :manage? + + def manage? + permission?(:manage_task_templates) + end + end + end +end diff --git a/config/routes.rb b/config/routes.rb index b2312feaf..a30d47181 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -24,7 +24,7 @@ end namespace :checklist do - resources :task_templates + resources :task_templates # here erin end resources :faqs resources :dashboard, only: [:index] do