From 46cf24805c83c564e94bc657e8bcdacdd25be729 Mon Sep 17 00:00:00 2001 From: Erin Claudio Date: Wed, 26 Jun 2024 10:17:00 -0400 Subject: [PATCH 01/27] Update Controller and routing changes * add: authenticate_user! as a before action in application controller instead of calling it within multiple controllers * add: skip_before_action authenticate_user! for controllers that don't need user authentication * remove: commented out 'skip_before_action' from controllers * fix failing tests: after adding authentication * add: config for devise configuration for setting up custom failures through warden in the devise.rb * add custom failure file * update: failure app Co-authored-by: Paul DobbinSchmaltz * add more context to why this authentication failure app is needed. * update: rails standard fix --- .../checklist/task_template_controller.rb | 67 +++++++++++++++++++ .../staff/default_pet_tasks_controller.rb | 61 ----------------- .../{default_pet_task.rb => task_template.rb} | 6 +- .../checklist/task_template_policy.rb | 14 ++++ .../organizations/default_pet_task_policy.rb | 10 --- config/routes.rb | 4 +- ...ame_default_pet_tasks_to_task_templates.rb | 5 ++ db/schema.rb | 26 +++---- 8 files changed, 105 insertions(+), 88 deletions(-) create mode 100644 app/controllers/organizations/staff/checklist/task_template_controller.rb delete mode 100644 app/controllers/organizations/staff/default_pet_tasks_controller.rb rename app/models/{default_pet_task.rb => task_template.rb} (82%) create mode 100644 app/policies/organizations/checklist/task_template_policy.rb delete mode 100644 app/policies/organizations/default_pet_task_policy.rb create mode 100644 db/migrate/20240625184654_rename_default_pet_tasks_to_task_templates.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/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 From d5b0d0edc48fd84c9fe71eae07b01c9305246a1a Mon Sep 17 00:00:00 2001 From: Erin Claudio Date: Fri, 5 Jul 2024 12:48:42 -0400 Subject: [PATCH 02/27] Update with main pull --- .../adoptable_pets_controller.rb | 2 +- .../staff/custom_pages_controller.rb | 29 ++++++++++++ .../staff/page_texts_controller.rb | 25 ----------- app/models/concerns/authorizable.rb | 2 +- app/models/{page_text.rb => custom_page.rb} | 6 +-- app/models/organization.rb | 2 +- .../organizations/custom_page_policy.rb | 8 ++++ .../organizations/page_text_policy.rb | 8 ---- app/services/organizations/create_service.rb | 6 +-- app/views/layouts/dashboard/_sidebar.html.erb | 4 +- app/views/organizations/home/index.html.erb | 14 +++--- .../_form.html.erb | 8 ++-- .../edit.html.erb | 7 +-- config/breadcrumbs.rb | 4 +- config/locales/en.yml | 2 +- config/routes.rb | 2 +- .../20240225160909_create_page_texts.rb | 2 +- ...40_add_adoptable_pet_info_to_page_texts.rb | 2 +- ...65551_change_page_texts_to_custom_pages.rb | 5 +++ db/schema.rb | 44 ++++++++++++++----- db/seeds/01_alta.rb | 2 +- db/seeds/02_baja.rb | 2 +- .../image_attachment_table_component_test.rb | 6 +-- ...est.rb => custom_pages_controller_test.rb} | 24 +++++----- .../{page_text.rb => custom_page.rb} | 10 ++--- test/factories/organizations.rb | 4 +- test/integration/adoptable_pet_show_test.rb | 2 +- .../adoption_application_reviews_test.rb | 2 +- ...{page_text_test.rb => custom_page_test.rb} | 18 ++++---- test/models/organization_test.rb | 2 +- ...icy_test.rb => custom_page_policy_test.rb} | 4 +- .../organizations/create_service_test.rb | 4 +- test/system/adoption_fosterer_test.rb | 2 +- test/system/home_page_test.rb | 4 +- test/system/login_test.rb | 4 +- test/system/registration_test.rb | 2 +- test/system/users_test.rb | 2 +- 37 files changed, 154 insertions(+), 122 deletions(-) create mode 100644 app/controllers/organizations/staff/custom_pages_controller.rb delete mode 100644 app/controllers/organizations/staff/page_texts_controller.rb rename app/models/{page_text.rb => custom_page.rb} (88%) create mode 100644 app/policies/organizations/custom_page_policy.rb delete mode 100644 app/policies/organizations/page_text_policy.rb rename app/views/organizations/staff/{page_texts => custom_pages}/_form.html.erb (89%) rename app/views/organizations/staff/{page_texts => custom_pages}/edit.html.erb (53%) create mode 100644 db/migrate/20240628165551_change_page_texts_to_custom_pages.rb rename test/controllers/organizations/staff/{page_texts_controller_test.rb => custom_pages_controller_test.rb} (50%) rename test/factories/{page_text.rb => custom_page.rb} (75%) rename test/models/{page_text_test.rb => custom_page_test.rb} (53%) rename test/policies/organizations/{page_text_policy_test.rb => custom_page_policy_test.rb} (93%) diff --git a/app/controllers/organizations/adoptable_pets_controller.rb b/app/controllers/organizations/adoptable_pets_controller.rb index dc50d00cc..e3beba871 100644 --- a/app/controllers/organizations/adoptable_pets_controller.rb +++ b/app/controllers/organizations/adoptable_pets_controller.rb @@ -17,7 +17,7 @@ def index end def show - @adoptable_pet_info = PageText.first&.adoptable_pet_info + @adoptable_pet_info = CustomPage.first&.adoptable_pet_info @pet = Pet.find(params[:id]) authorize! @pet, with: Organizations::AdoptablePetPolicy diff --git a/app/controllers/organizations/staff/custom_pages_controller.rb b/app/controllers/organizations/staff/custom_pages_controller.rb new file mode 100644 index 000000000..34e9ff159 --- /dev/null +++ b/app/controllers/organizations/staff/custom_pages_controller.rb @@ -0,0 +1,29 @@ +module Organizations + module Staff + class CustomPagesController < Organizations::BaseController + layout "dashboard" + before_action :set_custom_page, only: %i[edit update] + def edit + end + + def update + if @custom_page.update(custom_page_params) + redirect_to edit_staff_custom_page_path, notice: t(".success") + else + redirect_to edit_staff_custom_page_path, alert: @custom_page.errors.full_messages.to_sentence + end + end + + private + + def custom_page_params + params.require(:custom_page).permit(:hero, :about, :hero_image, :adoptable_pet_info, about_us_images: []) + end + + def set_custom_page + @custom_page = CustomPage.first + authorize! @custom_page + end + end + end +end diff --git a/app/controllers/organizations/staff/page_texts_controller.rb b/app/controllers/organizations/staff/page_texts_controller.rb deleted file mode 100644 index 3c76eab2e..000000000 --- a/app/controllers/organizations/staff/page_texts_controller.rb +++ /dev/null @@ -1,25 +0,0 @@ -class Organizations::Staff::PageTextsController < Organizations::BaseController - layout "dashboard" - before_action :set_page_text, only: %i[edit update] - def edit - end - - def update - if @page_text.update(page_text_params) - redirect_to edit_staff_page_text_path, notice: t(".success") - else - redirect_to edit_staff_page_text_path, alert: @page_text.errors.full_messages.to_sentence - end - end - - private - - def page_text_params - params.require(:page_text).permit(:hero, :about, :hero_image, :adoptable_pet_info, about_us_images: []) - end - - def set_page_text - @page_text = PageText.first - authorize! @page_text - end -end diff --git a/app/models/concerns/authorizable.rb b/app/models/concerns/authorizable.rb index 16dcb18b7..d41e6b311 100644 --- a/app/models/concerns/authorizable.rb +++ b/app/models/concerns/authorizable.rb @@ -47,7 +47,7 @@ activate_staff invite_staff manage_organization_profile - manage_page_text + manage_custom_page manage_staff change_user_roles ] diff --git a/app/models/page_text.rb b/app/models/custom_page.rb similarity index 88% rename from app/models/page_text.rb rename to app/models/custom_page.rb index f8364cb6d..261ef8d2f 100644 --- a/app/models/page_text.rb +++ b/app/models/custom_page.rb @@ -1,6 +1,6 @@ # == Schema Information # -# Table name: page_texts +# Table name: custom_pages # # id :bigint not null, primary key # about :text @@ -12,13 +12,13 @@ # # Indexes # -# index_page_texts_on_organization_id (organization_id) +# index_custom_pages_on_organization_id (organization_id) # # Foreign Keys # # fk_rails_... (organization_id => organizations.id) # -class PageText < ApplicationRecord +class CustomPage < ApplicationRecord acts_as_tenant(:organization) has_one_attached :hero_image diff --git a/app/models/organization.rb b/app/models/organization.rb index 4d9a55695..b17148b5d 100644 --- a/app/models/organization.rb +++ b/app/models/organization.rb @@ -25,5 +25,5 @@ class Organization < ApplicationRecord has_one :profile, dependent: :destroy, class_name: "OrganizationProfile", required: true has_one :location, through: :profile - has_one :page_text, dependent: :destroy + has_one :custom_page, dependent: :destroy end diff --git a/app/policies/organizations/custom_page_policy.rb b/app/policies/organizations/custom_page_policy.rb new file mode 100644 index 000000000..a3fa00b57 --- /dev/null +++ b/app/policies/organizations/custom_page_policy.rb @@ -0,0 +1,8 @@ +class Organizations::CustomPagePolicy < ApplicationPolicy + pre_check :verify_organization! + pre_check :verify_active_staff! + + def manage? + permission?(:manage_custom_page) + end +end diff --git a/app/policies/organizations/page_text_policy.rb b/app/policies/organizations/page_text_policy.rb deleted file mode 100644 index f24fb9323..000000000 --- a/app/policies/organizations/page_text_policy.rb +++ /dev/null @@ -1,8 +0,0 @@ -class Organizations::PageTextPolicy < ApplicationPolicy - pre_check :verify_organization! - pre_check :verify_active_staff! - - def manage? - permission?(:manage_page_text) - end -end diff --git a/app/services/organizations/create_service.rb b/app/services/organizations/create_service.rb index ee6783f54..7d0394fa7 100644 --- a/app/services/organizations/create_service.rb +++ b/app/services/organizations/create_service.rb @@ -39,7 +39,7 @@ def signal(args) create_staff_account add_admin_role_to_staff_account send_email - create_page_text + create_custom_page end rescue => e raise "An error occurred: #{e.message}" @@ -102,9 +102,9 @@ def send_email .create_new_org_and_admin(@organization.slug).deliver_now end - def create_page_text + def create_custom_page ActsAsTenant.with_tenant(@organization) do - PageText.create! + CustomPage.create! end end end diff --git a/app/views/layouts/dashboard/_sidebar.html.erb b/app/views/layouts/dashboard/_sidebar.html.erb index 5725a6b1c..2b97c8850 100644 --- a/app/views/layouts/dashboard/_sidebar.html.erb +++ b/app/views/layouts/dashboard/_sidebar.html.erb @@ -71,8 +71,8 @@ <% end %>