Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CaseContact validates usage and spec changes - case contact form prep #6061

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
13 changes: 2 additions & 11 deletions app/models/case_contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ class CaseContact < ApplicationRecord

attr_accessor :duration_hours

validate :contact_made_chosen
validates :contact_made, inclusion: {in: [true, false], message: :must_be_true_or_false}
validates :miles_driven, numericality: {greater_than_or_equal_to: 0, less_than: 10000}
validates :medium_type, presence: true, if: :active_or_details?
validates :duration_minutes, presence: true, if: :active_or_details?
Expand Down Expand Up @@ -35,7 +35,7 @@ class CaseContact < ApplicationRecord
belongs_to :casa_case, optional: true
has_one :casa_org, through: :casa_case
validates :casa_case_id, presence: true, if: :active?
validate :draft_case_ids_not_empty, unless: :started?
validates :draft_case_ids, presence: {message: :must_be_selected}, unless: :started?

has_many :case_contact_contact_types
has_many :contact_types, through: :case_contact_contact_types
Expand Down Expand Up @@ -231,15 +231,6 @@ def volunteer_address_is_valid
end
end

def contact_made_chosen
errors.add(:base, "Must enter whether the contact was made.") if contact_made.nil?
!contact_made.nil?
end

def draft_case_ids_not_empty
errors.add(:base, "You must select at least one casa case.") if draft_case_ids.empty?
end

def supervisor_id
supervisor.id
end
Expand Down
14 changes: 14 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,24 @@ en:
activerecord:
attributes:
case_contact:
case_contact_contact_types:
one: Contact Type
other: Contact Types
contact_types:
one: Contact Type
other: Contact Types
draft_case_ids:
one: CASA Case
other: CASA Cases
occurred_at: Date
case_contact/additional_expenses:
other_expenses_amount: Expense amount
other_expenses_describe: Expense description
errors:
messages:
cant_be_future: can't be in the future
must_be_selected: must be selected
must_be_true_or_false: must be true or false
time:
formats:
day_and_date: "%A, %b %d, %Y"
Expand Down
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@
member do
post :restore
end
resources :form, controller: "case_contacts/form"
resources :form, controller: "case_contacts/form", only: %i[show update]
Copy link
Contributor Author

Choose a reason for hiding this comment

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

these are the only actions in the controller, so this is not a behavioral change (except preventing controller errors)

resources :followups, only: %i[create], controller: "case_contacts/followups", shallow: true do
patch :resolve, on: :member
end
Expand Down
10 changes: 10 additions & 0 deletions spec/factories/casa_cases.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,16 @@
court_report_status { :not_submitted }
case_court_orders { [] }

transient do
volunteers { [] }
end

after(:create) do |casa_case, evaluator|
Array.wrap(evaluator.volunteers).each do |volunteer|
create(:case_assignment, casa_case:, volunteer:)
end
end

trait :pre_transition do
birth_month_year_youth { 13.years.ago }
end
Expand Down
5 changes: 5 additions & 0 deletions spec/factories/casa_orgs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,11 @@
logo { Rack::Test::UploadedFile.new(Rails.root.join("spec", "fixtures", "org_logo.jpeg")) }
end

trait :all_reimbursements_enabled do
additional_expenses_enabled { true }
show_driving_reimbursement { true }
end

trait :with_placement_types do
transient { placement_names { ["Reunification", "Adoption", "Foster Care", "Kinship"] } }

Expand Down
4 changes: 3 additions & 1 deletion spec/factories/case_contacts.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
association :creator, factory: :user
casa_case

contact_types { [create(:contact_type)] }
contact_types { [association(:contact_type)] }
Copy link
Collaborator

Choose a reason for hiding this comment

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

not familiar with this. what does it do vs just the create?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

It will play a little nicer with build/build_stubbed according to the docs, so fewer db hits in those situations.

duration_minutes { 60 }
occurred_at { Time.zone.today }
contact_made { false }
Expand All @@ -38,12 +38,14 @@
trait :wants_reimbursement do
miles_driven { 456 }
want_driving_reimbursement { true }
volunteer_address { "123 Contact Factory St" }
end

trait :started_status do
started # enum trait

casa_case { nil }
contact_types { [] }
draft_case_ids { [] }
medium_type { nil }
occurred_at { nil }
Expand Down
10 changes: 10 additions & 0 deletions spec/factories/supervisors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,16 @@
display_name { Faker::Name.unique.name }
active { true }

transient do
volunteers { [] }
end

after(:create) do |supervisor, evaluator|
Array.wrap(evaluator.volunteers).each do |volunteer|
create(:supervisor_volunteer, supervisor:, volunteer:)
end
end

trait :with_casa_cases do
after(:create) do |user, _|
volunteer = create(:volunteer)
Expand Down
9 changes: 5 additions & 4 deletions spec/factories/volunteers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@
end

trait :with_assigned_supervisor do
transient { supervisor { create(:supervisor) } }
transient { supervisor { nil } }

after(:create) do |user, evaluator|
create(:supervisor_volunteer, volunteer: user, supervisor: evaluator.supervisor)
after(:create) do |volunteer, evaluator|
supervisor = evaluator.supervisor || create(:supervisor, casa_org: volunteer.casa_org)
create(:supervisor_volunteer, volunteer:, supervisor:)
end
end

Expand All @@ -46,7 +47,7 @@
end
end

trait :with_disasllow_reimbursement do
trait :with_disallow_reimbursement do
Comment on lines -49 to +50
Copy link
Collaborator

Choose a reason for hiding this comment

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

lol

after(:create) do |user, _|
create(:case_assignment, :disallow_reimbursement, casa_case: create(:casa_case, casa_org: user.casa_org), volunteer: user)
end
Expand Down
6 changes: 3 additions & 3 deletions spec/models/case_contact_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
it "validates that contact_made cannot be null" do
case_contact = build_stubbed(:case_contact, contact_made: nil)
expect(case_contact).not_to be_valid
expect(case_contact.errors[:base]).to eq(["Must enter whether the contact was made."])
expect(case_contact.errors.full_messages).to include("Contact made must be true or false")
end

it "can be updated even if it is old" do
Expand Down Expand Up @@ -113,7 +113,7 @@
it "requires a case to be selected" do
case_contact = build_stubbed(:case_contact, :details_status, draft_case_ids: [])
expect(case_contact).not_to be_valid
expect(case_contact.errors.full_messages).to include("You must select at least one casa case.")
expect(case_contact.errors.full_messages).to include("CASA Case must be selected")
end

it "requires occurred at" do
Expand All @@ -140,7 +140,7 @@
it "requires a case to be selected" do
obj = build_stubbed(:case_contact, :expenses_status, :wants_reimbursement, draft_case_ids: [])
expect(obj).not_to be_valid
expect(obj.errors.full_messages).to include("You must select at least one casa case.")
expect(obj.errors.full_messages).to include("CASA Case must be selected")
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/requests/case_contacts/form_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@
end

context "when updating contact types" do
let(:old_contact_type) { create(:case_contact_contact_type, case_contact: case_contact, contact_type: contact_type_group_b.contact_types.first.id) }
let!(:old_contact_type) { create(:case_contact_contact_type, case_contact: case_contact, contact_type: contact_type_group_b.contact_types.first) }

it "removes unselected ones" do
expect(case_contact.contact_types.count).to eq 1
Expand Down
2 changes: 2 additions & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -44,4 +44,6 @@
# the seed, which is printed after each run.
# --seed 1234
config.order = :random

RSpec::Matchers.define_negated_matcher :not_change, :change
end
15 changes: 0 additions & 15 deletions spec/support/fill_out_minimum_required_form_fields.rb

This file was deleted.

2 changes: 1 addition & 1 deletion spec/system/case_contacts/new_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@

context "when driving reimbursement is hidden when volunteer not allowed to request" do
let(:casa_org) { build(:casa_org, show_driving_reimbursement: true) }
let(:volunteer) { create(:volunteer, :with_disasllow_reimbursement, casa_org:) }
let(:volunteer) { create(:volunteer, :with_disallow_reimbursement, casa_org:) }

it "does not show for case_contacts" do
subject
Expand Down