From f98a43d850bbb5af1f1799279723409a041231a5 Mon Sep 17 00:00:00 2001 From: jalletto Date: Sun, 7 Jul 2024 18:45:44 -0500 Subject: [PATCH] add ability to create and update placement types --- app/controllers/casa_org_controller.rb | 5 ++ app/controllers/placement_types_controller.rb | 46 +++++++++++++++++ app/models/placement_type.rb | 2 + app/policies/application_policy.rb | 2 +- app/policies/placement_type_policy.rb | 6 +++ app/views/casa_org/_placement_types.html.erb | 50 +++++++++++++++++++ app/views/casa_org/edit.html.erb | 14 ++++++ app/views/placement_types/_form.html.erb | 30 +++++++++++ app/views/placement_types/edit.html.erb | 1 + app/views/placement_types/new.html.erb | 1 + config/routes.rb | 1 + spec/models/placement_type_spec.rb | 12 +++++ spec/policies/placement_type_policy_spec.rb | 29 +++++++++++ spec/views/casa_orgs/edit.html.erb_spec.rb | 1 + 14 files changed, 199 insertions(+), 1 deletion(-) create mode 100644 app/controllers/placement_types_controller.rb create mode 100644 app/policies/placement_type_policy.rb create mode 100644 app/views/casa_org/_placement_types.html.erb create mode 100644 app/views/placement_types/_form.html.erb create mode 100644 app/views/placement_types/edit.html.erb create mode 100644 app/views/placement_types/new.html.erb create mode 100644 spec/policies/placement_type_policy_spec.rb diff --git a/app/controllers/casa_org_controller.rb b/app/controllers/casa_org_controller.rb index 77392b4ee1..1d0d2d1406 100644 --- a/app/controllers/casa_org_controller.rb +++ b/app/controllers/casa_org_controller.rb @@ -7,6 +7,7 @@ class CasaOrgController < ApplicationController before_action :set_learning_hour_topics, only: %i[edit update] before_action :set_sent_emails, only: %i[edit update] before_action :set_contact_topics, only: %i[edit update] + before_action :set_placement_types, only: %i[edit update] before_action :require_organization! after_action :verify_authorized before_action :set_active_storage_url_options, only: %i[edit update] @@ -86,6 +87,10 @@ def set_learning_hour_topics @learning_hour_topics = LearningHourTopic.for_organization(@casa_org) end + def set_placement_types + @placement_types = PlacementType.for_organization(@casa_org) + end + def set_contact_topics @contact_topics = @casa_org.contact_topics.where(soft_delete: false) end diff --git a/app/controllers/placement_types_controller.rb b/app/controllers/placement_types_controller.rb new file mode 100644 index 0000000000..8bab774218 --- /dev/null +++ b/app/controllers/placement_types_controller.rb @@ -0,0 +1,46 @@ +class PlacementTypesController < ApplicationController + before_action :set_placement_type, only: %i[edit update] + + def new + authorize PlacementType + @placement_type = PlacementType.new + end + + def edit + authorize @placement_type + end + + def create + authorize PlacementType + @placement_type = PlacementType.new(placement_type_params) + @placement_type.casa_org = current_organization + respond_to do |format| + if @placement_type.save + format.html { redirect_to edit_casa_org_path(current_organization.id), notice: "Placement Type was successfully created." } + else + format.html { render :new, status: :unprocessable_entity } + end + end + end + + def update + authorize @placement_type + respond_to do |format| + if @placement_type.update(placement_type_params) + format.html { redirect_to edit_casa_org_path(current_organization.id), notice: "Placement Type was successfully updated." } + else + format.html { render :edit, status: :unprocessable_entity } + end + end + end + + private + + def set_placement_type + @placement_type = PlacementType.find(params[:id]) + end + + def placement_type_params + params.require(:placement_type).permit(:name) + end +end diff --git a/app/models/placement_type.rb b/app/models/placement_type.rb index 49ffd47054..b5b3997cfb 100644 --- a/app/models/placement_type.rb +++ b/app/models/placement_type.rb @@ -1,6 +1,8 @@ class PlacementType < ApplicationRecord belongs_to :casa_org validates :name, presence: true + + scope :for_organization, ->(org) { where(casa_org: org) } end # == Schema Information diff --git a/app/policies/application_policy.rb b/app/policies/application_policy.rb index 84b43ef090..4736e9baf7 100644 --- a/app/policies/application_policy.rb +++ b/app/policies/application_policy.rb @@ -55,7 +55,7 @@ def same_org? case record when CasaOrg user.casa_org == record - when CasaAdmin, CasaCase, Volunteer, Supervisor, HearingType, ContactTypeGroup, ContactTopic + when CasaAdmin, CasaCase, Volunteer, Supervisor, HearingType, ContactTypeGroup, ContactTopic, PlacementType user.casa_org == record.casa_org when CourtDate, CaseContact, CaseAssignment user.casa_org == record&.casa_case&.casa_org diff --git a/app/policies/placement_type_policy.rb b/app/policies/placement_type_policy.rb new file mode 100644 index 0000000000..0ba5b83fda --- /dev/null +++ b/app/policies/placement_type_policy.rb @@ -0,0 +1,6 @@ +class PlacementTypePolicy < ApplicationPolicy + alias_method :create?, :is_admin_same_org? + alias_method :edit?, :is_admin_same_org? + alias_method :new?, :is_admin_same_org? + alias_method :update?, :is_admin_same_org? +end diff --git a/app/views/casa_org/_placement_types.html.erb b/app/views/casa_org/_placement_types.html.erb new file mode 100644 index 0000000000..8ae5ae4bc7 --- /dev/null +++ b/app/views/casa_org/_placement_types.html.erb @@ -0,0 +1,50 @@ +
+
+
+
+
+

Placement Types

+
+
+ +
+
+
+ + + + + + + + + <% @placement_types.each do |placement_type| %> + + + + + + <% end %> + +
NameActions
+ <%= placement_type.name %> + + <%= link_to edit_placement_type_path(placement_type) do %> +
+ +
+ <% end %> +
+
+
+
+
diff --git a/app/views/casa_org/edit.html.erb b/app/views/casa_org/edit.html.erb index 7805b8250f..1ee9a9856c 100644 --- a/app/views/casa_org/edit.html.erb +++ b/app/views/casa_org/edit.html.erb @@ -163,3 +163,17 @@
<%= render "contact_topics" %>
+
+
+
+
+

+ Manage Placement Types +

+
+
+
+
+
+ <%= render "placement_types" %> +
diff --git a/app/views/placement_types/_form.html.erb b/app/views/placement_types/_form.html.erb new file mode 100644 index 0000000000..0faceb9b6a --- /dev/null +++ b/app/views/placement_types/_form.html.erb @@ -0,0 +1,30 @@ +
+
+
+
+

+ <%= title %> +

+
+
+
+
+ +
+ <%= form_with(model: placement_type, local: true) do |form| %> +
+ <%= render "/shared/error_messages", resource: placement_type %> +
+ +
+ <%= form.label :name, "Name" %> + <%= form.text_field :name, class: "form-control", required: true %> +
+ +
+ <%= button_tag(type: "submit", class: "btn-sm main-btn primary-btn btn-hover") do %> + Submit + <% end %> +
+ <% end %> +
diff --git a/app/views/placement_types/edit.html.erb b/app/views/placement_types/edit.html.erb new file mode 100644 index 0000000000..d5dfaf905d --- /dev/null +++ b/app/views/placement_types/edit.html.erb @@ -0,0 +1 @@ +<%= render partial: "form", locals: { title: "Edit Placement Type", placement_type: @placement_type } %> diff --git a/app/views/placement_types/new.html.erb b/app/views/placement_types/new.html.erb new file mode 100644 index 0000000000..016f16ab0e --- /dev/null +++ b/app/views/placement_types/new.html.erb @@ -0,0 +1 @@ +<%= render partial: "form", locals: { title: "New Placement Type", placement_type: @placement_type } %> diff --git a/config/routes.rb b/config/routes.rb index cba2c83874..cf6e842d79 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -138,6 +138,7 @@ def set_up_flipper delete "soft_delete", on: :member end + resources :placement_types, except: %i[index show delete] resources :followup_reports, only: :index resources :placement_reports, only: :index resources :banners, except: %i[show] do diff --git a/spec/models/placement_type_spec.rb b/spec/models/placement_type_spec.rb index 22f5bd45f6..e24437ce3e 100644 --- a/spec/models/placement_type_spec.rb +++ b/spec/models/placement_type_spec.rb @@ -5,4 +5,16 @@ it { is_expected.to validate_presence_of(:name) } it { is_expected.to belong_to(:casa_org) } + + describe "for_organization" do + let!(:casa_org_1) { create(:casa_org) } + let!(:casa_org_2) { create(:casa_org) } + let!(:placement_type_1) { create(:placement_type, casa_org: casa_org_1) } + let!(:placement_type_2) { create(:placement_type, casa_org: casa_org_2) } + + it "returns only records matching the specified organization" do + expect(described_class.for_organization(casa_org_1)).to eq([placement_type_1]) + expect(described_class.for_organization(casa_org_2)).to eq([placement_type_2]) + end + end end diff --git a/spec/policies/placement_type_policy_spec.rb b/spec/policies/placement_type_policy_spec.rb new file mode 100644 index 0000000000..96eefc0a4c --- /dev/null +++ b/spec/policies/placement_type_policy_spec.rb @@ -0,0 +1,29 @@ +require "rails_helper" + +RSpec.describe PlacementTypePolicy, type: :policy do + subject { described_class } + let(:placement_type) { build(:placement_type, casa_org: organization) } + + let(:organization) { build(:casa_org) } + let(:casa_admin) { create(:casa_admin, casa_org: organization) } + let(:other_org_admin) { create(:casa_admin) } + let(:volunteer) { build(:volunteer, casa_org: organization) } + let(:supervisor) { build(:supervisor, casa_org: organization) } + + permissions :create?, :edit?, :new?, :update? do + it "allows same org casa_admins" do + is_expected.to permit(casa_admin, placement_type) + end + + it "allows does not allow different org casa_admins" do + is_expected.to_not permit(other_org_admin, placement_type) + end + it "does not permit supervisor" do + is_expected.to_not permit(supervisor, placement_type) + end + + it "does not permit volunteer" do + is_expected.to_not permit(volunteer, placement_type) + end + end +end diff --git a/spec/views/casa_orgs/edit.html.erb_spec.rb b/spec/views/casa_orgs/edit.html.erb_spec.rb index fccc63d40e..24ada1614d 100644 --- a/spec/views/casa_orgs/edit.html.erb_spec.rb +++ b/spec/views/casa_orgs/edit.html.erb_spec.rb @@ -10,6 +10,7 @@ assign(:learning_hour_topics, []) assign(:sent_emails, []) assign(:contact_topics, []) + assign(:placement_types, []) sign_in build_stubbed(:casa_admin) end