Skip to content

Commit

Permalink
add ability to create and update placement types
Browse files Browse the repository at this point in the history
  • Loading branch information
jalletto committed Jul 7, 2024
1 parent a4d983d commit f98a43d
Show file tree
Hide file tree
Showing 14 changed files with 199 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/controllers/casa_org_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down Expand Up @@ -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
Expand Down
46 changes: 46 additions & 0 deletions app/controllers/placement_types_controller.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 2 additions & 0 deletions app/models/placement_type.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
2 changes: 1 addition & 1 deletion app/policies/application_policy.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions app/policies/placement_type_policy.rb
Original file line number Diff line number Diff line change
@@ -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
50 changes: 50 additions & 0 deletions app/views/casa_org/_placement_types.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<div class="row">
<div class="col-lg-12">
<div class="card-style mb-30">
<div class="row align-items-center">
<div class="col-md-6">
<h3>Placement Types</h3>
</div>
<div class="col-md-6">
<div class="breadcrumb-wrapper">
<span class="ml-5">
<%= link_to new_placement_type_path, class: "btn-sm main-btn primary-btn btn-hover" do %>
<i class="lni lni-plus mr-10"></i>
New Placement Type
<% end %>
</span>
</div>
</div>
</div>
<div class="table-wrapper table-responsive">
<table class="table striped-table">
<thead>
<tr>
<th>Name</th>
<th>Actions</th>
</tr>
</thead>
<tbody>
<% @placement_types.each do |placement_type| %>
<tr id="placement_types-<%= placement_type.id %>">
<td scope="row" class="min-width">
<%= placement_type.name %>
</td>

<td>
<%= link_to edit_placement_type_path(placement_type) do %>
<div class="action">
<button class="text-danger">
<i class="lni lni-pencil-alt"></i> Edit
</button>
</div>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</div>
</div>
</div>
14 changes: 14 additions & 0 deletions app/views/casa_org/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -163,3 +163,17 @@
<div class="tables-wrapper">
<%= render "contact_topics" %>
</div>
<div class="title-wrapper pt-30">
<div class="row align-items-center">
<div class="col-md-6">
<div class="title mb-30">
<h2>
Manage Placement Types
</h2>
</div>
</div>
</div>
</div>
<div class="tables-wrapper">
<%= render "placement_types" %>
</div>
30 changes: 30 additions & 0 deletions app/views/placement_types/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
<div class="title-wrapper pt-30">
<div class="row align-items-center">
<div class="col-md-6">
<div class="title mb-30">
<h1>
<%= title %>
</h1>
</div>
</div>
</div>
</div><!-- ==== end title ==== -->

<div class="card-style mb-30">
<%= form_with(model: placement_type, local: true) do |form| %>
<div class="alert-box danger-alert">
<%= render "/shared/error_messages", resource: placement_type %>
</div>

<div class="input-style-1">
<%= form.label :name, "Name" %>
<%= form.text_field :name, class: "form-control", required: true %>
</div>

<div class="actions mb-10">
<%= button_tag(type: "submit", class: "btn-sm main-btn primary-btn btn-hover") do %>
<i class="lni lni-checkmark-circle mr-5"></i> Submit
<% end %>
</div>
<% end %>
</div>
1 change: 1 addition & 0 deletions app/views/placement_types/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render partial: "form", locals: { title: "Edit Placement Type", placement_type: @placement_type } %>
1 change: 1 addition & 0 deletions app/views/placement_types/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render partial: "form", locals: { title: "New Placement Type", placement_type: @placement_type } %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions spec/models/placement_type_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
29 changes: 29 additions & 0 deletions spec/policies/placement_type_policy_spec.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/views/casa_orgs/edit.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
assign(:learning_hour_topics, [])
assign(:sent_emails, [])
assign(:contact_topics, [])
assign(:placement_types, [])

sign_in build_stubbed(:casa_admin)
end
Expand Down

0 comments on commit f98a43d

Please sign in to comment.