diff --git a/app/assets/stylesheets/pages/casa_cases.scss b/app/assets/stylesheets/pages/casa_cases.scss index 544409c430..34ad221b4f 100644 --- a/app/assets/stylesheets/pages/casa_cases.scss +++ b/app/assets/stylesheets/pages/casa_cases.scss @@ -169,4 +169,4 @@ body.casa_cases-show { flex-direction: column; gap: .2rem; } -} +} \ No newline at end of file diff --git a/app/controllers/placements_controller.rb b/app/controllers/placements_controller.rb index fe24ffa4e4..0bc7490e61 100644 --- a/app/controllers/placements_controller.rb +++ b/app/controllers/placements_controller.rb @@ -3,25 +3,52 @@ class PlacementsController < ApplicationController before_action :set_placement, only: %i[edit show generate update destroy] before_action :require_organization! - rescue_from ActiveRecord::RecordNotFound, with: -> { head :not_found } + def index + @placements = policy_scope(@casa_case.placements).includes(:placement_type).order(placement_started_at: :desc) + end def show - authorize @casa_case + authorize @placement end def new + @placement = Placement.new(casa_case: @casa_case) + authorize @placement end def edit + authorize @placement end def create + @placement = Placement.new(placement_params) + authorize @placement + + if @placement.save + redirect_to casa_case_placements_path(@casa_case), notice: "Placement was successfully created." + else + render :new, status: :unprocessable_entity + end end def update + authorize @placement + + if @placement.update(placement_params) + redirect_to casa_case_placements_path(@casa_case), notice: "Placement was successfully updated." + else + render :edit, status: :unprocessable_entity + end end def destroy + authorize @placement + + if @placement.destroy + redirect_to casa_case_placements_path(@casa_case), notice: "Placement was successfully deleted." + else + render :edit, status: :unprocessable_entity + end end private @@ -33,4 +60,11 @@ def set_casa_case def set_placement @placement = @casa_case.placements.find(params[:id]) end + + def placement_params + params.require(:placement).permit( + :placement_started_at, + :placement_type_id + ).merge({creator_id: current_user.id, casa_case_id: @casa_case.id}) + end end diff --git a/app/models/casa_org.rb b/app/models/casa_org.rb index b5b9c7461c..7aed3f2ab1 100644 --- a/app/models/casa_org.rb +++ b/app/models/casa_org.rb @@ -28,6 +28,7 @@ class CasaOrg < ApplicationRecord has_many :contact_topics has_one_attached :logo has_one_attached :court_report_template + has_many :placement_types, dependent: :destroy def casa_admins CasaAdmin.in_organization(self) diff --git a/app/models/placement.rb b/app/models/placement.rb index f1f4c71b30..f0ef8c102c 100644 --- a/app/models/placement.rb +++ b/app/models/placement.rb @@ -2,6 +2,7 @@ class Placement < ApplicationRecord belongs_to :casa_case belongs_to :placement_type belongs_to :creator, class_name: "User" + has_one :casa_org, through: :casa_case validates :placement_started_at, comparison: { greater_than_or_equal_to: "1989-01-01".to_date, diff --git a/app/models/placement_type.rb b/app/models/placement_type.rb index 49ffd47054..f4515c325f 100644 --- a/app/models/placement_type.rb +++ b/app/models/placement_type.rb @@ -1,6 +1,9 @@ class PlacementType < ApplicationRecord belongs_to :casa_org + validates :name, presence: true + scope :for_organization, ->(org) { where(casa_org: org) } + scope :order_alphabetically, -> { order(:name) } end # == Schema Information diff --git a/app/policies/placement_policy.rb b/app/policies/placement_policy.rb new file mode 100644 index 0000000000..da622c4b0d --- /dev/null +++ b/app/policies/placement_policy.rb @@ -0,0 +1,26 @@ +class PlacementPolicy < ApplicationPolicy + class Scope < ApplicationPolicy::Scope + def resolve + return scope.none unless @user&.casa_org + + scope.joins(:casa_case).where(casa_cases: {casa_org: @user.casa_org}) + end + end + + def allowed_to_edit_casa_case? + casa_case_policy.edit? + end + + alias_method :show?, :allowed_to_edit_casa_case? + alias_method :edit?, :allowed_to_edit_casa_case? + alias_method :update?, :allowed_to_edit_casa_case? + alias_method :new?, :allowed_to_edit_casa_case? + alias_method :create?, :allowed_to_edit_casa_case? + alias_method :destroy?, :admin_or_supervisor? + + private + + def casa_case_policy + CasaCasePolicy.new(user, record.casa_case) + end +end diff --git a/app/views/casa_cases/_placements.html.erb b/app/views/casa_cases/_placements.html.erb index ce7a5aa30d..be1545446c 100644 --- a/app/views/casa_cases/_placements.html.erb +++ b/app/views/casa_cases/_placements.html.erb @@ -1,13 +1,18 @@ -<% placements = casa_case.placements %> - -<% if placements.empty? %> - No Placements -<% else %> - +<% current_placement = casa_case.placements.order(placement_started_at: :desc).first %> + +<% if current_placement %> +
+ Current Placement: + <%= current_placement.placement_type.name %> +
+ +

Placed since: <%= current_placement.decorate.formatted_date %>

+

<%= link_to "See All Placements", casa_case_placements_path(casa_case), class: 'text-primary hover-underline' %>

+<% elsif casa_case.casa_org.placement_types.present? %> +
+ Current Placement: + Unknown +
+ +

<%= link_to "See All Placements", casa_case_placements_path(casa_case), class: 'text-primary hover-underline' %>

<% end %> diff --git a/app/views/placements/_fields.html.erb b/app/views/placements/_fields.html.erb new file mode 100644 index 0000000000..79922aafc3 --- /dev/null +++ b/app/views/placements/_fields.html.erb @@ -0,0 +1,18 @@ +
+ <%= form.label :placement_started_at, "Placement Started At" %> + <%= form.date_field :placement_started_at, + value: placement.placement_started_at&.to_date || Time.zone.now, + class: "form-control" %> +
+
+ <%= form.label :placement_type_id, "Placement Type" %> +
+ <%= form.collection_select( + :placement_type_id, + PlacementType.for_organization(current_organization).order_alphabetically, + :id, :name, + {include_hidden: false, include_blank: "-Select Placement Type-"}, + {class: "form-control"} + ) %> +
+
diff --git a/app/views/placements/_form.html.erb b/app/views/placements/_form.html.erb new file mode 100644 index 0000000000..e4d0d5abae --- /dev/null +++ b/app/views/placements/_form.html.erb @@ -0,0 +1,29 @@ +
+
+ <%= render "/shared/error_messages", resource: placement %> +
+ <%= form_with(model: placement, url: [casa_case, placement], local: true, + data: { controller: "placement-form", nested_form_wrapper_selector_value: ".nested-form-wrapper" }) do |form| %> +
+
+
Case Number: <%= link_to casa_case.case_number, casa_case %>
+
+
+ +
+
+
+ <%= render 'placements/fields', placement: placement, form: form, casa_case: casa_case %> +
+ <% end %> +
diff --git a/app/views/placements/edit.html.erb b/app/views/placements/edit.html.erb new file mode 100644 index 0000000000..1ac06ef77d --- /dev/null +++ b/app/views/placements/edit.html.erb @@ -0,0 +1,11 @@ +
+
+
+
+

Editing Placement

+
+
+
+
+ +<%= render 'form', casa_case: @casa_case, placement: @placement %> diff --git a/app/views/placements/index.html.erb b/app/views/placements/index.html.erb new file mode 100644 index 0000000000..b87fdc9ddc --- /dev/null +++ b/app/views/placements/index.html.erb @@ -0,0 +1,69 @@ +
+
+
+
+

Placement History for + <%= link_to "#{@casa_case.case_number}", casa_case_path(@casa_case) %> +

+
+
+
+ +
+
+
+
+
+ + + + + + + + + + + <% @placements.each_with_index do |placement, idx| %> + + + + + + + <%= render(Modal::GroupComponent.new(id: placement.id)) do |component| %> + <% component.with_header(text: "Delete Placement?", id: placement.id) %> + <% component.with_footer do %> + <%= link_to casa_case_placement_path(@casa_case, placement), method: :delete, class: "btn-sm main-btn danger-btn btn-hover ms-auto" do %> + + Confirm + <% end %> + <% end %> + <% end %> + <% end %> + +
Placement TypeDate
<%= placement.placement_type.name %> + <%= placement.decorate.formatted_date %> + - + <% if idx.zero? %> + Present + <% elsif @placements[idx - 1].placement_started_at %> + <%= @placements[idx - 1].decorate.formatted_date %> + <% end %> + <%= link_to edit_casa_case_placement_path(@casa_case, placement), class: "btn-sm main-btn primary-btn-outline btn-hover ms-auto" do %> + + Edit + <% end %> + + <%= render(Modal::OpenLinkComponent.new(target: placement.id, klass: "btn-sm main-btn danger-btn-outline btn-hover ms-auto")) do %> + + Delete + <% end %> +
+
+
diff --git a/app/views/placements/new.html.erb b/app/views/placements/new.html.erb new file mode 100644 index 0000000000..a66c469829 --- /dev/null +++ b/app/views/placements/new.html.erb @@ -0,0 +1,11 @@ +
+
+
+
+

New Placement

+
+
+
+
+ +<%= render 'form', casa_case: @casa_case, placement: @placement %> diff --git a/app/views/placements/show.html.erb b/app/views/placements/show.html.erb index e8946ea04a..819f17a4d5 100644 --- a/app/views/placements/show.html.erb +++ b/app/views/placements/show.html.erb @@ -8,7 +8,7 @@