Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/controllers/casa_orgs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ class CasaOrgsController < ApplicationController
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 :set_judges, only: %i[edit update]
before_action :must_be_admin
before_action :require_organization!

Expand Down Expand Up @@ -42,4 +43,8 @@ def set_contact_type_data
def set_hearing_types
@hearing_types = HearingType.for_organization(@casa_org)
end

def set_judges
@judges = Judge.for_organization(@casa_org)
end
end
43 changes: 43 additions & 0 deletions app/controllers/judges_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
class JudgesController < ApplicationController
before_action :authenticate_user!, :must_be_admin
before_action :set_judge, except: [:new, :create]

def new
@judge = Judge.new
end

def create
@judge = Judge.new(judge_params)

respond_to do |format|
if @judge.save
format.html { redirect_to edit_casa_org_path(current_organization), notice: "Judge was successfully created." }
else
format.html { render :new }
end
end
end

def edit
end

def update
if @judge.update(judge_params)
redirect_to edit_casa_org_path(current_organization), notice: "Judge was successfully updated."
else
render :edit
end
end

private

def set_judge
@judge = Judge.find(params[:id])
end

def judge_params
params.require(:judge).permit(:name, :active).merge(
casa_org: current_organization
)
end
end
30 changes: 30 additions & 0 deletions app/models/judge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
class Judge < ApplicationRecord
has_paper_trail

belongs_to :casa_org

validates :name, presence: true, uniqueness: {scope: %i[casa_org]}

scope :for_organization, ->(org) { where(casa_org: org) }
scope :active, -> { where(active: true) }
end

# == Schema Information
#
# Table name: judges
#
# id :bigint not null, primary key
# active :boolean default(TRUE)
# name :string
# created_at :datetime not null
# updated_at :datetime not null
# casa_org_id :bigint not null
#
# Indexes
#
# index_judges_on_casa_org_id (casa_org_id)
#
# Foreign Keys
#
# fk_rails_... (casa_org_id => casa_orgs.id)
#
2 changes: 1 addition & 1 deletion app/views/casa_orgs/_hearing_types.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<thead>
<tr>
<th>Name</th>
<th>Active</th>
<th>Active?</th>
<th>Actions</th>
</tr>
</thead>
Expand Down
32 changes: 32 additions & 0 deletions app/views/casa_orgs/_judges.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
<div class="row">
<div class="col-sm-12 dashboard-table-header pt-2 pb-2">
<h3>Judge</h3>
<%= link_to "New Judge", new_judge_path, class: "btn btn-primary" %>
</div>
</div>

<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Active?</th>
<th>Actions</th>
</tr>
</thead>

<tbody>
<% @judges.each do |judge| %>
<tr id="judge-<%= judge.id %>">
<td scope="row">
<%= judge.name %>
</td>
<td scope="row">
<%= judge.active ? "Yes" : "No" %>
</td>
<td>
<%= link_to "Edit", edit_judge_path(judge) %>
</td>
</tr>
<% end %>
</tbody>
</table>
1 change: 1 addition & 0 deletions app/views/casa_orgs/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -47,5 +47,6 @@
<div class="card card-container">
<div class="card-body">
<%= render "hearing_types" %>
<%= render "judges" %>
</div>
</div>
22 changes: 22 additions & 0 deletions app/views/judges/_form.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
<%= link_to 'Back', :back %>

<h1><%= title %></h1>

<div class="card card-container">
<div class="card-body">
<%= form_with(model: judge, local: true) do |form| %>
<%= render "/shared/error_messages", resource: judge %>
<div class="field form-group">
<%= form.label :name %>
<%= form.text_field :name, class: "form-control" %>
</div>
<div class="field form-group">
<%= form.check_box :active %>
<%= form.label :active %>
</div>
<div class="actions">
<%= form.submit "Submit", class: "btn btn-primary" %>
</div>
<% end %>
</div>
</div>
1 change: 1 addition & 0 deletions app/views/judges/edit.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render partial: "form", locals: { title: "Edit Judge", judge: @judge } %>
1 change: 1 addition & 0 deletions app/views/judges/new.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
<%= render partial: "form", locals: { title: "New Judge", judge: @judge } %>
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
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 :judges, only: %i[new create edit update]

resources :supervisors, except: %i[destroy]
resources :supervisor_volunteers, only: %i[create] do
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20201023233638_create_judges.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class CreateJudges < ActiveRecord::Migration[6.0]
def change
create_table :judges do |t|
t.references :casa_org, null: false, foreign_key: true

t.timestamps
end
end
end
5 changes: 5 additions & 0 deletions db/migrate/20201023234325_add_active_to_judge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddActiveToJudge < ActiveRecord::Migration[6.0]
def change
add_column :judges, :active, :boolean, default: true
end
end
5 changes: 5 additions & 0 deletions db/migrate/20201024003821_add_name_to_judge.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddNameToJudge < ActiveRecord::Migration[6.0]
def change
add_column :judges, :name, :string
end
end
12 changes: 11 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 2020_10_20_095451) do
ActiveRecord::Schema.define(version: 2020_10_24_003821) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -153,6 +153,15 @@
t.index ["casa_org_id"], name: "index_hearing_types_on_casa_org_id"
end

create_table "judges", force: :cascade do |t|
t.bigint "casa_org_id", null: false
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
t.boolean "active", default: true
t.string "name"
t.index ["casa_org_id"], name: "index_judges_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
Expand Down Expand Up @@ -213,6 +222,7 @@
add_foreign_key "case_assignments", "users", column: "volunteer_id"
add_foreign_key "case_contacts", "casa_cases"
add_foreign_key "case_contacts", "users", column: "creator_id"
add_foreign_key "judges", "casa_orgs"
add_foreign_key "supervisor_volunteers", "users", column: "supervisor_id"
add_foreign_key "supervisor_volunteers", "users", column: "volunteer_id"
add_foreign_key "users", "casa_orgs"
Expand Down
7 changes: 7 additions & 0 deletions spec/factories/judges.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
FactoryBot.define do
factory :judge do
casa_org { create(:casa_org) }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine, but for future simplicity, you can just write it like:

FactoryBot.define do
  factory :judge do
    casa_org
    name { Faker::Name.name }
    active { true }
  end
end

And it will automatically create the casa_org association.

name { Faker::Name.name }
active { true }
end
end
11 changes: 11 additions & 0 deletions spec/models/judge_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
require "rails_helper"

RSpec.describe Judge, type: :model do
it { is_expected.to belong_to(:casa_org) }
it { is_expected.to validate_presence_of(:name) }
Comment thread
littleforest marked this conversation as resolved.

it "has a valid factory" do
judge = build(:judge)
expect(judge.valid?).to be true
end
end
147 changes: 147 additions & 0 deletions spec/requests/judges_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
require "rails_helper"

RSpec.describe "/judges", type: :request do
describe "GET /judges/new" do
context "logged in as admin user" do
it "can successfully access a judge create page" do
sign_in_as_admin

get new_judge_path

expect(response).to be_successful
end
end

context "logged in as a non-admin user" do
it "cannot access a judge create page" do
sign_in_as_volunteer

get new_judge_path

expect(response).to redirect_to root_path
expect(response.request.flash[:notice]).to eq "Sorry, you are not authorized to perform this action."
end
end

context "unauthenticated request" do
it "cannot access a judge create page" do
get new_judge_path

expect(response).to redirect_to new_user_session_path
end
end
end

describe "POST /judges" do
let(:params) { {judge: {name: "Joe Judge", active: true}} }
context "logged in as admin user" do
it "can successfully create a judge" do
casa_org = create(:casa_org)
sign_in create(:casa_admin, casa_org: casa_org)

expect {
post judges_path, params: params
}.to change(Judge, :count).by(1)

judge = Judge.last

expect(judge.name).to eql "Joe Judge"
expect(judge.casa_org).to eql casa_org
expect(judge.active).to be_truthy
expect(response).to redirect_to edit_casa_org_path(casa_org)
expect(response.request.flash[:notice]).to eq "Judge was successfully created."
end
end

context "logged in as a non-admin user" do
it "cannot create a judge" do
sign_in_as_volunteer

post judges_path, params: params

expect(response).to redirect_to root_path
expect(response.request.flash[:notice]).to eq "Sorry, you are not authorized to perform this action."
end
end

context "unauthenticated request" do
it "cannot create a judge" do
post judges_path, params: params

expect(response).to redirect_to new_user_session_path
end
end
end

describe "GET /judges/:id/edit" do
let(:judge) { create(:judge) }

context "logged in as admin user" do
it "can successfully access a judge edit page" do
sign_in_as_admin

get edit_judge_path(judge)

expect(response).to be_successful
end
end

context "logged in as a non-admin user" do
it "cannot access a judge edit page" do
sign_in_as_volunteer

get edit_judge_path(judge)

expect(response).to redirect_to root_path
expect(response.request.flash[:notice]).to eq "Sorry, you are not authorized to perform this action."
end
end

context "unauthenticated request" do
it "cannot access a judge edit page" do
get edit_judge_path(judge)

expect(response).to redirect_to new_user_session_path
end
end
end

describe "PUT /judges/:id" do
let(:judge) { create(:judge) }
let(:params) { {judge: {name: "New Name", judge_id: judge.id, active: false}} }
context "logged in as admin user" do
it "can successfully update a judge" do
casa_org = create(:casa_org)
sign_in create(:casa_admin, casa_org: casa_org)

put judge_path(judge), params: params

judge.reload
expect(judge.name).to eq "New Name"
expect(judge.active).to be_falsey

expect(response).to redirect_to edit_casa_org_path(casa_org)
expect(response.request.flash[:notice]).to eq "Judge was successfully updated."
end
end

context "logged in as a non-admin user" do
it "cannot update a judge" do
sign_in_as_volunteer

put judge_path(judge), params: params

expect(response).to redirect_to root_path
expect(response.request.flash[:notice]).to eq "Sorry, you are not authorized to perform this action."
end
end

context "unauthenticated request" do
it "cannot update a judge" do
put judge_path(judge), params: params

expect(response).to redirect_to new_user_session_path
end
end
end
end
Loading