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/controllers/organizations/staff/default_pet_tasks_controller.rb b/app/controllers/organizations/staff/default_pet_tasks_controller.rb deleted file mode 100644 index 391616e2d..000000000 --- a/app/controllers/organizations/staff/default_pet_tasks_controller.rb +++ /dev/null @@ -1,61 +0,0 @@ -class Organizations::Staff::DefaultPetTasksController < Organizations::BaseController - before_action :context_authorize!, only: %i[index new create] - before_action :set_task, only: %i[edit update destroy] - - layout "dashboard" - - def index - @default_pet_tasks = authorized_scope(DefaultPetTask.all) - end - - def new - @task = DefaultPetTask.new - end - - def create - @task = DefaultPetTask.new(task_params) - - if @task.save - redirect_to staff_default_pet_tasks_path, notice: t(".success") - else - flash.now[:alert] = t(".error") - render :new, status: :unprocessable_entity - end - end - - def edit - end - - def update - if @task.update(task_params) - redirect_to staff_default_pet_tasks_path, notice: t(".success") - else - render :edit, status: :unprocessable_entity - end - end - - def destroy - @task.destroy - - redirect_to staff_default_pet_tasks_path, notice: t(".success") - end - - private - - def task_params - params.require(:default_pet_task).permit(:name, :description, :due_in_days, :recurring) - end - - def set_task - @task = DefaultPetTask.find(params[:id]) - - authorize! @task - rescue ActiveRecord::RecordNotFound - redirect_to staff_default_pet_tasks_path, alert: t(".error") - end - - def context_authorize! - authorize! DefaultPetTask, - context: {organization: Current.organization} - end -end diff --git a/app/models/default_pet_task.rb b/app/models/task_template.rb similarity index 82% rename from app/models/default_pet_task.rb rename to app/models/task_template.rb index 7694e3fd0..720ca64e5 100644 --- a/app/models/default_pet_task.rb +++ b/app/models/task_template.rb @@ -1,6 +1,6 @@ # == Schema Information # -# Table name: default_pet_tasks +# Table name: task_templates # # id :bigint not null, primary key # description :string @@ -13,13 +13,13 @@ # # Indexes # -# index_default_pet_tasks_on_organization_id (organization_id) +# index_task_templates_on_organization_id (organization_id) # # Foreign Keys # # fk_rails_... (organization_id => organizations.id) # -class DefaultPetTask < ApplicationRecord +class TaskTemplate < ApplicationRecord acts_as_tenant(:organization) validates :name, presence: true 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/app/policies/organizations/default_pet_task_policy.rb b/app/policies/organizations/default_pet_task_policy.rb deleted file mode 100644 index e1df20afb..000000000 --- a/app/policies/organizations/default_pet_task_policy.rb +++ /dev/null @@ -1,10 +0,0 @@ -class Organizations::DefaultPetTaskPolicy < ApplicationPolicy - pre_check :verify_organization! - pre_check :verify_active_staff! - - alias_rule :new?, :create?, :index?, to: :manage? - - def manage? - permission?(:manage_default_pet_tasks) - end -end diff --git a/config/routes.rb b/config/routes.rb index 3eef065b4..a30d47181 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -23,7 +23,9 @@ post "attach_files", on: :member, to: "pets#attach_files" end - resources :default_pet_tasks + namespace :checklist do + resources :task_templates # here erin + end resources :faqs resources :dashboard, only: [:index] do collection do diff --git a/db/migrate/20240625184654_rename_default_pet_tasks_to_task_templates.rb b/db/migrate/20240625184654_rename_default_pet_tasks_to_task_templates.rb new file mode 100644 index 000000000..612350369 --- /dev/null +++ b/db/migrate/20240625184654_rename_default_pet_tasks_to_task_templates.rb @@ -0,0 +1,5 @@ +class RenameDefaultPetTasksToTaskTemplates < ActiveRecord::Migration[7.1] + def change + rename_table :default_pet_tasks, :task_templates + end +end diff --git a/db/schema.rb b/db/schema.rb index d7d225122..c663a49ca 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_06_19_203450) do +ActiveRecord::Schema[7.1].define(version: 2024_06_25_184654) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -109,17 +109,6 @@ t.index ["organization_id"], name: "index_adopter_foster_profiles_on_organization_id" end - create_table "default_pet_tasks", force: :cascade do |t| - t.string "name", null: false - t.string "description" - t.bigint "organization_id", null: false - t.datetime "created_at", null: false - t.datetime "updated_at", null: false - t.integer "due_in_days" - t.boolean "recurring", default: false - t.index ["organization_id"], name: "index_default_pet_tasks_on_organization_id" - end - create_table "faqs", force: :cascade do |t| t.string "question", null: false t.text "answer", null: false @@ -298,6 +287,17 @@ t.index ["user_id"], name: "index_submitted_answers_on_user_id" end + create_table "task_templates", force: :cascade do |t| + t.string "name", null: false + t.string "description" + t.bigint "organization_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.integer "due_in_days" + t.boolean "recurring", default: false + t.index ["organization_id"], name: "index_task_templates_on_organization_id" + end + create_table "tasks", force: :cascade do |t| t.string "name", null: false t.text "description" @@ -356,7 +356,6 @@ add_foreign_key "adopter_foster_accounts", "users" add_foreign_key "adopter_foster_profiles", "adopter_foster_accounts" add_foreign_key "adopter_foster_profiles", "locations" - add_foreign_key "default_pet_tasks", "organizations" add_foreign_key "faqs", "organizations" add_foreign_key "form_profiles", "forms" add_foreign_key "forms", "organizations" @@ -375,5 +374,6 @@ add_foreign_key "staff_accounts", "users" add_foreign_key "submitted_answers", "questions" add_foreign_key "submitted_answers", "users" + add_foreign_key "task_templates", "organizations" add_foreign_key "tasks", "pets" end