Skip to content

Commit

Permalink
Clearer CaseContact occurred_at validation
Browse files Browse the repository at this point in the history
Remove non-standard/confusing enum wizard implementation,
use explicit methods recommended by gem instead. Validate unless
CAseContact in 'started' status.
  • Loading branch information
thejonroberts committed Aug 5, 2024
1 parent ed69a57 commit 7be0d03
Showing 1 changed file with 37 additions and 26 deletions.
63 changes: 37 additions & 26 deletions app/models/case_contact.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,20 @@ class CaseContact < ApplicationRecord
validate :contact_made_chosen
validates :miles_driven, numericality: {greater_than_or_equal_to: 0, less_than: 10000}
validates :medium_type, presence: true, if: :active_or_details?
validates :occurred_at, presence: true, if: :active_or_details?
validates :duration_minutes, presence: true, if: :active_or_details?
validate :occurred_at_not_in_future
validates :occurred_at, comparison: {
greater_than_or_equal_to: "1989-01-01".to_date,
message: "is not valid: Date of Contact cannot be prior to 1/1/1989.",
allow_nil: true
}
validates :occurred_at, presence: true, unless: :started?
validates :occurred_at, unless: :started?,
comparison: {
less_than: Date.tomorrow,
message: "cannot be in the future",
allow_nil: true
}
validates :occurred_at, unless: :started?,
comparison: {
greater_than_or_equal_to: "1989-01-01".to_date,
message: "is not valid: Date of Contact cannot be prior to 1/1/1989.",
allow_nil: true
}
validate :reimbursement_only_when_miles_driven, if: :active_or_expenses?
validate :volunteer_address_when_reimbursement_wanted, if: :active_or_expenses?
validate :volunteer_address_is_valid, if: :active_or_expenses?
Expand All @@ -39,19 +45,6 @@ class CaseContact < ApplicationRecord

after_save_commit ::CaseContactMetadataCallback.new

# Corresponds to the steps in the controller, so validations for certain columns can happen at those steps.
# These steps must be listed in order and have an html template in case_contacts/form.
FORM_STEPS = %i[details notes expenses].freeze
enum status: (%w[started active] + FORM_STEPS.map(&:to_s)).zip((%w[started active] + FORM_STEPS.map(&:to_s))).to_h

def active_or_details?
status == "details" || active?
end

def active_or_expenses?
status == "expenses" || active?
end

accepts_nested_attributes_for :additional_expenses, reject_if: :all_blank, allow_destroy: true
validates_associated :additional_expenses

Expand Down Expand Up @@ -161,6 +154,30 @@ def active_or_expenses?
]
)

# Corresponds to the steps in the controller, so validations for certain columns can happen at those steps.
# These steps must be listed in order and have an html template in case_contacts/form.
FORM_STEPS = %i[details notes expenses].freeze

def active?
status == "active"
end

def started?
status == "started"
end

def active_or_details?
status == "details" || active?
end

def active_or_notes?
status == "notes" || active?
end

def active_or_expenses?
status == "expenses" || active?
end

IN_PERSON = "in-person".freeze
TEXT_EMAIL = "text/email".freeze
VIDEO = "video".freeze
Expand All @@ -175,12 +192,6 @@ def update_cleaning_contact_types(args)
end
end

def occurred_at_not_in_future
return unless occurred_at && occurred_at >= Date.tomorrow

errors.add(:occurred_at, :invalid, message: "cannot be in the future")
end

# Displays occurred_at in the format January 1, 1970
# @return [String]
def occurred_at_display
Expand Down

0 comments on commit 7be0d03

Please sign in to comment.