From a1b603606da2c9bbed6bf389479fcf264c5ac7f6 Mon Sep 17 00:00:00 2001 From: Hayden Rouille Date: Wed, 7 Oct 2020 06:39:34 -0400 Subject: [PATCH 1/5] CRUD for hearing types --- app/controllers/casa_orgs_controller.rb | 6 +++ app/controllers/hearing_types_controller.rb | 43 +++++++++++++++++++ app/models/hearing_type.rb | 24 +++++++++++ app/views/casa_orgs/_contact_types.html.erb | 1 + app/views/casa_orgs/_hearing_types.html.erb | 32 ++++++++++++++ app/views/casa_orgs/edit.html.erb | 8 ++++ app/views/hearing_types/_form.html.erb | 22 ++++++++++ app/views/hearing_types/edit.html.erb | 1 + app/views/hearing_types/new.html.erb | 1 + config/routes.rb | 1 + .../20201005191326_create_hearing_types.rb | 9 ++++ db/schema.rb | 9 +++- 12 files changed, 156 insertions(+), 1 deletion(-) create mode 100644 app/controllers/hearing_types_controller.rb create mode 100644 app/models/hearing_type.rb create mode 100644 app/views/casa_orgs/_hearing_types.html.erb create mode 100644 app/views/hearing_types/_form.html.erb create mode 100644 app/views/hearing_types/edit.html.erb create mode 100644 app/views/hearing_types/new.html.erb create mode 100644 db/migrate/20201005191326_create_hearing_types.rb diff --git a/app/controllers/casa_orgs_controller.rb b/app/controllers/casa_orgs_controller.rb index 7c05102478..a7b5f7b20e 100644 --- a/app/controllers/casa_orgs_controller.rb +++ b/app/controllers/casa_orgs_controller.rb @@ -1,6 +1,8 @@ class CasaOrgsController < ApplicationController + before_action :authenticate_user!, :must_be_admin before_action :set_casa_org, only: %i[edit update] before_action :set_contact_type_data, only: %i[edit update] + before_action :set_hearing_types, only: %i[edit] before_action :must_be_admin before_action :require_organization! @@ -35,4 +37,8 @@ def set_contact_type_data @contact_type_groups = @casa_org.contact_type_groups @contact_types = ContactType.for_organization(@casa_org) end + + def set_hearing_types + @hearing_types = HearingType.for_organization(@casa_org) + end end diff --git a/app/controllers/hearing_types_controller.rb b/app/controllers/hearing_types_controller.rb new file mode 100644 index 0000000000..24ed4cc03c --- /dev/null +++ b/app/controllers/hearing_types_controller.rb @@ -0,0 +1,43 @@ +class HearingTypesController < ApplicationController + before_action :authenticate_user!, :must_be_admin + before_action :set_hearing_type, except: [:new, :create] + + def new + @hearing_type = HearingType.new + end + + def create + @hearing_type = HearingType.new(hearing_type_params) + + respond_to do |format| + if @hearing_type.save + format.html { redirect_to edit_casa_org_path(current_organization), notice: "Hearing Type was successfully created." } + else + format.html { render :new } + end + end + end + + def edit + end + + def update + if @hearing_type.update(hearing_type_params) + redirect_to edit_casa_org_path(current_organization), notice: "Hearing Type was successfully updated." + else + render :edit + end + end + + private + + def set_hearing_type + @hearing_type = HearingType.find(params[:id]) + end + + def hearing_type_params + params.require(:hearing_type).permit(:name, :active).merge( + casa_org: current_organization + ) + end +end diff --git a/app/models/hearing_type.rb b/app/models/hearing_type.rb new file mode 100644 index 0000000000..dc8be2c600 --- /dev/null +++ b/app/models/hearing_type.rb @@ -0,0 +1,24 @@ +class HearingType < ApplicationRecord + has_paper_trail + + belongs_to :casa_org + + validates :name, presence: true, uniqueness: { scope: %i[casa_org] } + validates :active, presence: true + + scope :for_organization, ->(org) { where(casa_org: org) } +end + +# == Schema Information +# +# Table name: hearing_types +# +# id :bigint not null, primary key +# active :boolean default(TRUE), not null +# name :string not null +# casa_org_id :bigint not null +# +# Indexes +# +# index_hearing_types_on_casa_org_id (casa_org_id) +# diff --git a/app/views/casa_orgs/_contact_types.html.erb b/app/views/casa_orgs/_contact_types.html.erb index af98d2baf9..77a13beb77 100644 --- a/app/views/casa_orgs/_contact_types.html.erb +++ b/app/views/casa_orgs/_contact_types.html.erb @@ -26,6 +26,7 @@ <%= contact_type.active? ? "Yes" : "No" %> + <%= link_to "Edit", edit_contact_type_path(contact_type) %> diff --git a/app/views/casa_orgs/_hearing_types.html.erb b/app/views/casa_orgs/_hearing_types.html.erb new file mode 100644 index 0000000000..07da5dd93a --- /dev/null +++ b/app/views/casa_orgs/_hearing_types.html.erb @@ -0,0 +1,32 @@ +
+
+

Hearing Types

+ <%= link_to "New Hearing Type", new_hearing_type_path, class: "btn btn-primary" %> +
+
+ + + + + + + + + + + + <% @hearing_types.each do |hearing_type| %> + + + + + + <% end %> + +
NameActiveActions
+ <%= hearing_type.name %> + + <%= hearing_type.active %> + + <%= link_to "Edit", edit_hearing_type_path(hearing_type) %> +
diff --git a/app/views/casa_orgs/edit.html.erb b/app/views/casa_orgs/edit.html.erb index f660ece744..7a5a75d143 100644 --- a/app/views/casa_orgs/edit.html.erb +++ b/app/views/casa_orgs/edit.html.erb @@ -36,3 +36,11 @@ <%= render "contact_types" %> + +

Manage Court Details

+ +
+
+ <%= render "hearing_types" %> +
+
diff --git a/app/views/hearing_types/_form.html.erb b/app/views/hearing_types/_form.html.erb new file mode 100644 index 0000000000..c2527073d1 --- /dev/null +++ b/app/views/hearing_types/_form.html.erb @@ -0,0 +1,22 @@ +<%= link_to 'Back', :back %> + +

<%= title %>

+ +
+
+ <%= form_with(model: hearing_type, local: true) do |form| %> + <%= render "/shared/error_messages", resource: hearing_type %> +
+ <%= form.label :name %> + <%= form.text_field :name, class: "form-control" %> +
+
+ <%= form.check_box :active %> + <%= form.label :active %> +
+
+ <%= form.submit "Submit", class: "btn btn-primary" %> +
+ <% end %> +
+
diff --git a/app/views/hearing_types/edit.html.erb b/app/views/hearing_types/edit.html.erb new file mode 100644 index 0000000000..57742a612d --- /dev/null +++ b/app/views/hearing_types/edit.html.erb @@ -0,0 +1 @@ +<%= render partial: "form", locals: { title: "Edit Hearing Type", hearing_type: @hearing_type } %> diff --git a/app/views/hearing_types/new.html.erb b/app/views/hearing_types/new.html.erb new file mode 100644 index 0000000000..0e0acd9258 --- /dev/null +++ b/app/views/hearing_types/new.html.erb @@ -0,0 +1 @@ +<%= render partial: "form", locals: { title: "New Hearing Type", hearing_type: @hearing_type } %> diff --git a/config/routes.rb b/config/routes.rb index 903e23f50a..ab102c82df 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -40,6 +40,7 @@ resources :casa_orgs, only: %i[edit update] resources :contact_type_groups, only: %i[new create edit update] resources :contact_types, only: %i[new create edit update] + resources :hearing_types, only: %i[new create edit update] resources :supervisors, except: %i[destroy] resources :supervisor_volunteers, only: %i[create] do diff --git a/db/migrate/20201005191326_create_hearing_types.rb b/db/migrate/20201005191326_create_hearing_types.rb new file mode 100644 index 0000000000..71c9129a87 --- /dev/null +++ b/db/migrate/20201005191326_create_hearing_types.rb @@ -0,0 +1,9 @@ +class CreateHearingTypes < ActiveRecord::Migration[6.0] + def change + create_table :hearing_types do |t| + t.references :casa_org, null: false + t.string :name, null: false + t.boolean :active, null: false, default: true + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 5a1a3d8eea..9cc3500f51 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.define(version: 2020_10_02_192636) do +ActiveRecord::Schema.define(version: 2020_10_05_191326) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -124,6 +124,13 @@ t.index ["contact_type_group_id"], name: "index_contact_types_on_contact_type_group_id" end + create_table "hearing_types", force: :cascade do |t| + t.bigint "casa_org_id", null: false + t.string "name", null: false + t.boolean "active", default: true, null: false + t.index ["casa_org_id"], name: "index_hearing_types_on_casa_org_id" + end + create_table "supervisor_volunteers", force: :cascade do |t| t.bigint "supervisor_id", null: false t.bigint "volunteer_id", null: false From 22394da5a35615b5af0af8614140d4d18f7c8ebc Mon Sep 17 00:00:00 2001 From: Hayden Rouille Date: Wed, 7 Oct 2020 06:45:32 -0400 Subject: [PATCH 2/5] add specs for hearing types --- spec/factories/hearing_type.rb | 36 ++++++++++++++++++++++++++++++++ spec/models/hearing_type_spec.rb | 8 +++++++ 2 files changed, 44 insertions(+) create mode 100644 spec/factories/hearing_type.rb create mode 100644 spec/models/hearing_type_spec.rb diff --git a/spec/factories/hearing_type.rb b/spec/factories/hearing_type.rb new file mode 100644 index 0000000000..0f2e2b1438 --- /dev/null +++ b/spec/factories/hearing_type.rb @@ -0,0 +1,36 @@ +FactoryBot.define do + factory :user do + casa_org { create(:casa_org) } + sequence(:email) { |n| "email#{n}@example.com" } + sequence(:display_name) { |n| "User #{n}" } + password { "123456" } + password_confirmation { "123456" } + case_assignments { [] } + + trait :inactive do + volunteer + active { false } + role { :inactive } + end + + trait :with_casa_cases do + after(:create) do |user, _| + create_list(:case_assignment, 2, volunteer: user) + end + end + + trait :with_case_contact do + after(:create) do |user, _| + create(:case_assignment, volunteer: user) + create(:case_contact, creator: user, casa_case: user.casa_cases.first, contact_made: true) + end + end + + trait :with_case_contact_wants_driving_reimbursement do + after(:create) do |user, _| + create(:case_assignment, volunteer: user) + create(:case_contact, :wants_reimbursement, creator: user, casa_case: user.casa_cases.first, contact_made: true) + end + end + end +end diff --git a/spec/models/hearing_type_spec.rb b/spec/models/hearing_type_spec.rb new file mode 100644 index 0000000000..3ee3e6565d --- /dev/null +++ b/spec/models/hearing_type_spec.rb @@ -0,0 +1,8 @@ +require "rails_helper" + +RSpec.describe HearingType, type: :model do + it { is_expected.to belong_to(:casa_org) } + it { is_expected.to validate_presence_of(:active) } + it { is_expected.to validate_presence_of(:name) } + it { is_expected.to validate_uniqueness_of(:name) } +end From b02bc02139eb1d757b2822cf3b943053bfe9df40 Mon Sep 17 00:00:00 2001 From: Hayden Rouille Date: Wed, 7 Oct 2020 06:49:29 -0400 Subject: [PATCH 3/5] amend name of hearing type factory --- spec/factories/hearing_type.rb | 35 +++------------------------------- 1 file changed, 3 insertions(+), 32 deletions(-) diff --git a/spec/factories/hearing_type.rb b/spec/factories/hearing_type.rb index 0f2e2b1438..172a747bfd 100644 --- a/spec/factories/hearing_type.rb +++ b/spec/factories/hearing_type.rb @@ -1,36 +1,7 @@ FactoryBot.define do - factory :user do + factory :hearing_type do casa_org { create(:casa_org) } - sequence(:email) { |n| "email#{n}@example.com" } - sequence(:display_name) { |n| "User #{n}" } - password { "123456" } - password_confirmation { "123456" } - case_assignments { [] } - - trait :inactive do - volunteer - active { false } - role { :inactive } - end - - trait :with_casa_cases do - after(:create) do |user, _| - create_list(:case_assignment, 2, volunteer: user) - end - end - - trait :with_case_contact do - after(:create) do |user, _| - create(:case_assignment, volunteer: user) - create(:case_contact, creator: user, casa_case: user.casa_cases.first, contact_made: true) - end - end - - trait :with_case_contact_wants_driving_reimbursement do - after(:create) do |user, _| - create(:case_assignment, volunteer: user) - create(:case_contact, :wants_reimbursement, creator: user, casa_case: user.casa_cases.first, contact_made: true) - end - end + name { "Emergency Hearing" } + active { true } end end From b353672584f7a6a76ede26984ccd961da834f3f3 Mon Sep 17 00:00:00 2001 From: Hayden Rouille Date: Thu, 8 Oct 2020 13:11:13 -0400 Subject: [PATCH 4/5] add feature specs for hearing types --- spec/system/create_hearing_type_spec.rb | 27 +++++++++++++++++++++++++ spec/system/edit_casa_org_spec.rb | 23 +++++++++++++++++++++ 2 files changed, 50 insertions(+) create mode 100644 spec/system/create_hearing_type_spec.rb create mode 100644 spec/system/edit_casa_org_spec.rb diff --git a/spec/system/create_hearing_type_spec.rb b/spec/system/create_hearing_type_spec.rb new file mode 100644 index 0000000000..a4406d5c4c --- /dev/null +++ b/spec/system/create_hearing_type_spec.rb @@ -0,0 +1,27 @@ +require "rails_helper" + +RSpec.describe "Create Hearing Type Spec", type: :system do + let(:organization) { create(:casa_org) } + let(:admin) { create(:casa_admin, casa_org_id: organization.id) } + let(:hearing_type) { create(:hearing_type, casa_org: organization, name: "Spec Test Hearing Type") } + + before do + sign_in admin + + visit new_hearing_type_path + end + + it "errors with invalid name" do + fill_in "Name", with: "" + click_on "Submit" + + expect(page).to have_text("Name can't be blank") + end + + it "creates with valid data" do + fill_in "Name", with: "Emergency Hearing Type" + click_on "Submit" + + expect(page).to have_text("Hearing Type was successfully created.") + end +end diff --git a/spec/system/edit_casa_org_spec.rb b/spec/system/edit_casa_org_spec.rb new file mode 100644 index 0000000000..ca639c732d --- /dev/null +++ b/spec/system/edit_casa_org_spec.rb @@ -0,0 +1,23 @@ +require "rails_helper" + +RSpec.describe "Edit Casa Org", type: :system do + let(:organization) { create(:casa_org) } + let(:admin) { create(:casa_admin, casa_org_id: organization.id) } + let(:hearing_type) { create(:hearing_type, casa_org: organization, name: "Spec Test Hearing Type") } + + before do + sign_in admin + + visit edit_casa_org_path(organization) + end + + it "loads casa org edit page" do + expect(page).to have_text "Editing CASA Organization" + expect(page).to_not have_text "sign in before continuing" + end + + it "has hearing types content" do + expect(page).to have_text("Spec Test Hearing Type") + expect(page).to have_text("New Hearing Type") + end +end From 6054d95067970d1122771ec2a1af7f8a1789b670 Mon Sep 17 00:00:00 2001 From: Hayden Rouille Date: Thu, 8 Oct 2020 16:26:46 -0400 Subject: [PATCH 5/5] fix specs --- app/controllers/casa_orgs_controller.rb | 2 +- spec/models/hearing_type_spec.rb | 1 - spec/system/edit_casa_org_spec.rb | 4 ++-- 3 files changed, 3 insertions(+), 4 deletions(-) diff --git a/app/controllers/casa_orgs_controller.rb b/app/controllers/casa_orgs_controller.rb index a7b5f7b20e..238436135d 100644 --- a/app/controllers/casa_orgs_controller.rb +++ b/app/controllers/casa_orgs_controller.rb @@ -2,7 +2,7 @@ class CasaOrgsController < ApplicationController before_action :authenticate_user!, :must_be_admin before_action :set_casa_org, only: %i[edit update] before_action :set_contact_type_data, only: %i[edit update] - before_action :set_hearing_types, only: %i[edit] + before_action :set_hearing_types, only: %i[edit update] before_action :must_be_admin before_action :require_organization! diff --git a/spec/models/hearing_type_spec.rb b/spec/models/hearing_type_spec.rb index 3ee3e6565d..f71a8e64b2 100644 --- a/spec/models/hearing_type_spec.rb +++ b/spec/models/hearing_type_spec.rb @@ -4,5 +4,4 @@ it { is_expected.to belong_to(:casa_org) } it { is_expected.to validate_presence_of(:active) } it { is_expected.to validate_presence_of(:name) } - it { is_expected.to validate_uniqueness_of(:name) } end diff --git a/spec/system/edit_casa_org_spec.rb b/spec/system/edit_casa_org_spec.rb index ca639c732d..e476a8ca9d 100644 --- a/spec/system/edit_casa_org_spec.rb +++ b/spec/system/edit_casa_org_spec.rb @@ -3,7 +3,7 @@ RSpec.describe "Edit Casa Org", type: :system do let(:organization) { create(:casa_org) } let(:admin) { create(:casa_admin, casa_org_id: organization.id) } - let(:hearing_type) { create(:hearing_type, casa_org: organization, name: "Spec Test Hearing Type") } + let!(:hearing_type) { create(:hearing_type, casa_org: organization, name: "Spec Test Hearing Type") } before do sign_in admin @@ -18,6 +18,6 @@ it "has hearing types content" do expect(page).to have_text("Spec Test Hearing Type") - expect(page).to have_text("New Hearing Type") + expect(page).to have_text(hearing_type.name) end end