Skip to content

Commit

Permalink
Merge pull request #1942 from FireLemons/custom_court_report_template…
Browse files Browse the repository at this point in the history
…s_per_org

Custom court report templates per org
  • Loading branch information
compwron authored Apr 15, 2021
2 parents a0f467b + 13355dd commit ef1f75b
Show file tree
Hide file tree
Showing 11 changed files with 59 additions and 43 deletions.
2 changes: 1 addition & 1 deletion app/controllers/casa_orgs_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def set_casa_org
end

def casa_org_update_params
params.require(:casa_org).permit(:name, :display_name, :address, :logo)
params.require(:casa_org).permit(:name, :display_name, :address, :logo, :court_report_template)
end

def set_contact_type_data
Expand Down
15 changes: 9 additions & 6 deletions app/controllers/case_court_reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,12 +62,15 @@ def set_casa_case
def generate_report_to_string(casa_case)
return unless casa_case

court_report = CaseCourtReport.new(
volunteer_id: current_user.id, # ??? not a volunteer ? linda
case_id: casa_case.id,
path_to_template: "app/documents/templates/report_template.docx"
)
court_report.generate_to_string
casa_case.casa_org.open_org_court_report_template do |template_docx_file|
court_report = CaseCourtReport.new(
volunteer_id: current_user.id, # ??? not a volunteer ? linda
case_id: casa_case.id,
path_to_template: template_docx_file.to_path
)

return court_report.generate_to_string
end
end

def save_report(report_data, casa_case)
Expand Down
Binary file not shown.
Binary file not shown.
Binary file not shown.
12 changes: 10 additions & 2 deletions app/models/casa_org.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class CasaOrg < ApplicationRecord
CASA_DEFAULT_COURT_REPORT = File.new(Rails.root.join("app", "documents", "templates", "default_report_template.docx"), "r")
CASA_DEFAULT_LOGO = Rails.root.join("public", "logo.jpeg")

has_paper_trail
Expand All @@ -10,8 +11,7 @@ class CasaOrg < ApplicationRecord
has_many :hearing_types, dependent: :destroy
has_many :case_assignments, through: :users, source: :casa_cases
has_one_attached :logo

delegate :url, :alt_text, :size, to: :casa_org_logo, prefix: :logo, allow_nil: true
has_one_attached :court_report_template

def casa_admins
CasaAdmin.in_organization(self)
Expand All @@ -38,6 +38,14 @@ def org_logo
CASA_DEFAULT_LOGO
end
end

def open_org_court_report_template(&block)
if court_report_template.attached?
court_report_template.open(&block)
else
yield CASA_DEFAULT_COURT_REPORT
end
end
end

# == Schema Information
Expand Down
24 changes: 17 additions & 7 deletions app/models/case_court_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,29 +10,26 @@ def initialize(args = {})
@casa_case = CasaCase.find(args[:case_id])
@volunteer = Volunteer.find(args[:volunteer_id])

@context = prepare_context
@context = prepare_context(args[:path_to_template].end_with?("default_report_template.docx"))
@template = Sablon.template(args[:path_to_template])

# optional
@report_path = args[:path_to_report]
end

# TODO is this used?
def generate!
@template.render_to_file(@report_path, @context)
end

def generate_to_string
@template.render_to_string(@context)
end

private

def prepare_context
def prepare_context(is_default_template)
{
created_date: I18n.l(Date.today, format: :full, default: nil),
casa_case: prepare_case_details,
case_contacts: prepare_case_contacts,
case_mandates: prepare_case_mandates,
org_address: (@volunteer.casa_org.address if is_default_template),
volunteer: {
name: @volunteer.display_name,
supervisor_name: @volunteer.supervisor&.display_name || "",
Expand Down Expand Up @@ -61,6 +58,19 @@ def prepare_case_contacts
end
end

def prepare_case_mandates
case_mandate_data = []

@casa_case.case_court_mandates.each do |case_mandate|
case_mandate_data << {
order: case_mandate.mandate_text,
status: case_mandate.implementation_status
}
end

case_mandate_data
end

def filter_out_old_case_contacts(interviewees)
most_recent_court_date = @casa_case.past_court_dates.order(:date).last&.date
if most_recent_court_date
Expand Down
10 changes: 4 additions & 6 deletions app/views/casa_orgs/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,22 @@
<%= form.label :name %>
<%= form.text_field :name, class: "form-control" %>
</div>

<div class="field form-group">
<%= form.label :display_name %>
<%= form.text_field :display_name, class: "form-control" %>
</div>

<div class="field form-group">
<%= form.label :address %>
<%= form.text_field :address, class: "form-control" %>
</div>

<div class="custom-file mb-5">
<%= form.label :logo %>
<%= form.file_field :logo, class: "form-control" %>
</div>
<br>

<div class="custom-file mb-5">
<%= form.label :court_report_template %>
<%= form.file_field :court_report_template, class: "form-control" %>
</div>
<div class="actions">
<%= form.submit t("button.submit"), class: "btn btn-primary" %>
</div>
Expand All @@ -37,7 +36,6 @@
<div class="card card-container">
<div class="card-body">
<%= render "contact_type_groups", contact_type_groups: @contact_type_groups %>
<%= render "contact_types", contact_types: @contact_types %>
</div>
</div>
Expand Down
15 changes: 15 additions & 0 deletions lib/tasks/deployment/20210415012736_task_name.rake
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
namespace :after_party do
desc "Deployment task: set_custom_court_docs_for_orgs"
task task_name: :environment do
CasaOrg.where(name: "Prince George CASA").map do |casa_org|
casa_org.court_report_template.attach(io: File.new(Rails.root.join("app", "documents", "templates", "prince_george_report_template.docx")), filename: "prince_george_report_template.docx")
end

CasaOrg.where(name: "Voices for Children Montgomery").map do |casa_org|
casa_org.court_report_template.attach(io: File.new(Rails.root.join("app", "documents", "templates", "montgomery_report_template.docx")), filename: "montgomery_report_template.docx")
end

AfterParty::TaskRecord
.create version: AfterParty::TaskRecorder.new(__FILE__).timestamp
end
end
22 changes: 2 additions & 20 deletions spec/models/case_court_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
describe "when receiving valid case, volunteer, and path_to_template" do
let(:casa_case_without_contacts) { volunteer.casa_cases.second }
let(:casa_case_with_contacts) { volunteer.casa_cases.first }
let(:path_to_template) { "app/documents/templates/report_template.docx" }
let(:path_to_template) { "app/documents/templates/default_report_template.docx" }
let(:path_to_report) { "tmp/test_report.docx" }
let(:report) do
CaseCourtReport.new(
Expand Down Expand Up @@ -50,15 +50,6 @@
end
end

describe "has valid @path_to_template" do
it "is existing" do
path = report.template.instance_variable_get(:@path)

expect(File.exist?(path_to_template)).to eq true
expect(File.exist?(path)).to eq true
end
end

describe "has valid @context" do
subject { report.context }

Expand All @@ -67,7 +58,7 @@

it "has the following keys [:created_date, :casa_case, :case_contacts, :volunteer]" do
expected = %i[created_date casa_case case_contacts volunteer]
expect(subject.keys).to eq expected
expect(subject.keys).to include(*expected)
end

it "must have Case Contacts as type Array" do
Expand All @@ -82,15 +73,6 @@
expect(report_as_data).not_to be_nil
expect(report_as_data).to be_instance_of String
end

it "successfully generates to file" do
report.generate!

expect(File.exist?(path_to_report)).to eq true

# clean up after testing
File.delete(path_to_report) if File.exist?(path_to_report)
end
end
end

Expand Down
2 changes: 1 addition & 1 deletion spec/system/casa_cases/edit_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -365,7 +365,7 @@ def sign_in_and_assign_volunteer
report = CaseCourtReport.new(
volunteer_id: volunteer.id,
case_id: casa_case.id,
path_to_template: "app/documents/templates/report_template.docx"
path_to_template: "app/documents/templates/default_report_template.docx"
)
casa_case.court_reports.attach(io: StringIO.new(report.generate_to_string), filename: "report#{n}.docx")
attached_report = casa_case.latest_court_report
Expand Down

0 comments on commit ef1f75b

Please sign in to comment.