Skip to content

Commit

Permalink
refactor: expose court report methods
Browse files Browse the repository at this point in the history
Expose methods in court report context to make unit
testing easier.

Write additional unit tests.
Remove old tests.

Remove redundant tests, some behavior is
already tested by the Dates object.
  • Loading branch information
elasticspoon committed Mar 27, 2024
1 parent 6788e19 commit 7ded9ab
Showing 3 changed files with 147 additions and 292 deletions.
42 changes: 23 additions & 19 deletions app/models/case_court_report_context.rb
Original file line number Diff line number Diff line change
@@ -15,37 +15,40 @@ def initialize(args = {})
end

def context
prepare_context(@path_to_template.end_with?("default_report_template.docx"))
end

private

def prepare_context(is_default_template)
latest_hearing_date = @casa_case.most_recent_past_court_date

{
created_date: I18n.l(Time.current.in_time_zone(@time_zone).to_date, format: :full, default: nil),
casa_case: prepare_case_details,
case_contacts: prepare_case_contacts,
case_court_orders: prepare_case_orders,
case_mandates: prepare_case_orders, # backwards compatible with old Montgomery template - keep this! TODO test full generation
latest_hearing_date: latest_hearing_date.nil? ? "___<LATEST HEARING DATE>____" : I18n.l(latest_hearing_date.date, format: :full, default: nil),
org_address: org_address(is_default_template),
casa_case: case_details,
case_contacts: case_contacts,
case_court_orders: case_orders(@case_court_orders),
case_mandates: case_orders(@case_court_orders), # backwards compatible with old Montgomery template - keep this! TODO test full generation
latest_hearing_date: latest_hearing_date,
org_address: org_address(@path_to_template),
volunteer: volunteer_info,
hearing_type_name: @court_date&.hearing_type&.name || "None"
}
end

def prepare_case_contacts
# @return [Array<Hash>]
# Each hash includes:
# - :name [String]
# - :type [String]
# - :dates [Array<String>]
# - :dates_by_medium_type [Array<String>]
def case_contacts
cccts = CaseContactContactType.includes(:case_contact, :contact_type).where("case_contacts.casa_case_id": @casa_case.id)
interviewees = filter_out_old_case_contacts(cccts)
return [] unless interviewees.size.positive?

CaseContactsContactDates.new(interviewees).contact_dates_details
end

def prepare_case_orders
@case_court_orders.map do |case_order|
def latest_hearing_date
latest_hearing_date = @casa_case.most_recent_past_court_date
latest_hearing_date.nil? ? "___<LATEST HEARING DATE>____" : I18n.l(latest_hearing_date.date, format: :full, default: nil)
end

def case_orders(orders)
orders.map do |case_order|
{
order: case_order.text,
status: case_order.implementation_status&.humanize
@@ -62,7 +65,7 @@ def filter_out_old_case_contacts(interviewees)
end
end

def prepare_case_details
def case_details
{
court_date: I18n.l(@court_date&.date, format: :full, default: nil),
case_number: @casa_case.case_number,
@@ -82,7 +85,8 @@ def volunteer_info
end
end

def org_address(is_default_template)
def org_address(path_to_template)
is_default_template = path_to_template.end_with?("default_report_template.docx")
@volunteer.casa_org.address if @volunteer && is_default_template
end
end
4 changes: 2 additions & 2 deletions spec/factories/case_court_report_context.rb
Original file line number Diff line number Diff line change
@@ -15,13 +15,13 @@
volunteer_for_context = volunteer.nil? ? create(:volunteer) : volunteer
casa_case_for_context = casa_case.nil? ? create(:casa_case) : casa_case

unless volunteer_for_context.casa_cases.where(id: casa_case_for_context.id).exists?
if volunteer_for_context && volunteer_for_context.casa_cases.where(id: casa_case_for_context.id).none?
volunteer_for_context.casa_cases << casa_case_for_context
end

new(
case_id: casa_case_for_context.id,
volunteer_id: volunteer_for_context.id,
volunteer_id: volunteer_for_context.try(:id),
path_to_report: path_to_report,
path_to_template: path_to_template,
court_date: court_date,
Loading

0 comments on commit 7ded9ab

Please sign in to comment.