diff --git a/app/controllers/casa_orgs_controller.rb b/app/controllers/casa_orgs_controller.rb index 7c05102478..238436135d 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 update] 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 9a6fc36bcd..ffdcd4e70f 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -41,6 +41,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 f0eea57d91..23863c8e2d 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_04_165322) 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" @@ -125,6 +125,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 diff --git a/spec/factories/hearing_type.rb b/spec/factories/hearing_type.rb new file mode 100644 index 0000000000..172a747bfd --- /dev/null +++ b/spec/factories/hearing_type.rb @@ -0,0 +1,7 @@ +FactoryBot.define do + factory :hearing_type do + casa_org { create(:casa_org) } + name { "Emergency Hearing" } + active { true } + end +end diff --git a/spec/models/hearing_type_spec.rb b/spec/models/hearing_type_spec.rb new file mode 100644 index 0000000000..f71a8e64b2 --- /dev/null +++ b/spec/models/hearing_type_spec.rb @@ -0,0 +1,7 @@ +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) } +end 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..e476a8ca9d --- /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(hearing_type.name) + end +end