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

[#1741] Generate past court reports from past court date #2095

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
19 changes: 14 additions & 5 deletions app/controllers/past_court_dates_controller.rb
Original file line number Diff line number Diff line change
@@ -1,23 +1,32 @@
class PastCourtDatesController < ApplicationController
before_action :set_casa_case, only: %i[show]
before_action :set_past_court_date, only: %i[show]
before_action :set_past_court_date, only: %i[show generate]
before_action :require_organization!

rescue_from ActiveRecord::RecordNotFound, with: -> { head :not_found }

def show
authorize @casa_case

respond_to do |format|
format.html {}
format.docx do
send_data @past_court_date.generate_report,
type: :docx,
filename: "#{@past_court_date.display_name}.docx",
disposition: "attachment",
status: :ok
end
end
end

private

def set_casa_case
@casa_case = current_organization.casa_cases.find(params[:casa_case_id])
rescue ActiveRecord::RecordNotFound
head :not_found
end

def set_past_court_date
@past_court_date = @casa_case.past_court_dates.find(params[:id])
rescue ActiveRecord::RecordNotFound
head :not_found
end
end
Binary file not shown.
37 changes: 37 additions & 0 deletions app/models/past_court_date.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
# frozen_string_literal: true

require "sablon"

class PastCourtDate < ApplicationRecord
belongs_to :casa_case

has_many :case_court_mandates
belongs_to :hearing_type, optional: true
belongs_to :judge, optional: true

DOCX_TEMPLATE_PATH = "app/documents/templates/default_past_court_date_template.docx"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Style note: prefer to use Rails.root.join


# get reports associated with the case this belongs to before this court date but after the court date before this one
def associated_reports
prev = casa_case.past_court_dates.where("date < ?", date).order(:date).last
Expand All @@ -22,6 +28,37 @@ def latest_associated_report
def additional_info?
case_court_mandates.any? || hearing_type || judge
end

def generate_report
template = Sablon.template(File.expand_path(DOCX_TEMPLATE_PATH))

template.render_to_string(context_hash)
end

def display_name
"#{casa_case.case_number} - Past Court Date - #{I18n.l(date.to_date)}"
end

private

def context_hash
{
court_date: date,
case_number: casa_case.case_number,
judge_name: judge&.name || "None",
hearing_type_name: hearing_type&.name || "None",
case_court_mandates: case_court_mandates_context_hash
}
end

def case_court_mandates_context_hash
case_court_mandates.map do |mandate|
{
text: mandate.mandate_text,
implementation_status: mandate.implementation_status&.humanize
}
end
end
end
# == Schema Information
#
Expand Down
6 changes: 5 additions & 1 deletion app/views/past_court_dates/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
<% @past_court_date.case_court_mandates.each do |court_mandate| %>
<tr>
<td><%= court_mandate.mandate_text %></td>
<td class="text-center"><%= court_mandate.implementation_status.humanize %></td>
<td class="text-center"><%= court_mandate.implementation_status&.humanize %></td>
</tr>
<% end %>
</tbody>
Expand All @@ -52,5 +52,9 @@
</h6>
</p>
<% end %>

<%= link_to t(".label.download_report_docx"),
casa_case_past_court_date_path(@casa_case, @past_court_date, format: :docx),
class: "btn btn-primary" %>
</div>
</div>
1 change: 1 addition & 0 deletions config/locales/views.en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,7 @@ en:
casa_case_court_mandate:
mandate_text: Court Mandate Text
implementation_status: Implementation Status
download_report_docx: Download Report (.docx)
casa_admins:
form:
button:
Expand Down
67 changes: 67 additions & 0 deletions spec/models/past_court_date.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
require "rails_helper"

RSpec.describe PastCourtDate, type: :model do
describe "validations" do
it { is_expected.to belong_to(:casa_case) }
it { is_expected.to belong_to(:hearing_type).optional(true) }
it { is_expected.to belong_to(:judge).optional(true) }
end

describe "methods" do
let(:past_court_date) { build_stubbed(:past_court_date) }

pending "#associated_reports"
Copy link
Collaborator

Choose a reason for hiding this comment

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

please implement the pending tests also

pending "#latest_associated_report"

describe "#additional_info?" do
subject(:additional_info?) { past_court_date.additional_info? }

context "without court details" do
it { is_expected.to be_falsy }
end

context "with court details" do
context "with judge" do
before { past_court_date.judge = build_stubbed(:judge) }

it { is_expected.to be_truthy }
end

context "with hearing type" do
before { past_court_date.hearing_type = build_stubbed(:hearing_type) }

it { is_expected.to be_truthy }
end

context "with both judge and hearing type" do
before do
past_court_date.judge = build_stubbed(:judge)
past_court_date.hearing_type = build_stubbed(:hearing_type)
end

it { is_expected.to be_truthy }
end
end
end

describe "#generate_report" do
subject(:generate_report) { past_court_date.generate_report }

it { is_expected.not_to be_nil }
end

describe "#display_name" do
subject(:display_name) { past_court_date.display_name }

let(:casa_case) { build_stubbed(:casa_case, case_number: "CINA-000") }
let(:past_court_date) { build_stubbed(:past_court_date, casa_case: casa_case, date: Time.zone.local(2020, 5, 3)) }

it { is_expected.to eq "CINA-000 - Past Court Date - 2020-05-03" }
end
end

it "has a valid factory" do
past_court_date = build(:past_court_date)
expect(past_court_date.valid?).to be_truthy
end
end
13 changes: 13 additions & 0 deletions spec/requests/past_court_dates_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,5 +21,18 @@

it { expect(response).to redirect_to new_user_session_path }
end

context "when request format is word document" do
subject(:show) { get casa_case_past_court_date_path(casa_case, past_court_date), headers: headers }

let(:headers) { {accept: "application/vnd.openxmlformats-officedocument.wordprocessingml.document"} }

before do
sign_in_as_admin
show
end

it { expect(response).to be_successful }
end
end
end
10 changes: 8 additions & 2 deletions spec/views/past_court_dates/show.html.erb_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@
let(:past_court_date) { create(:past_court_date, :with_court_details) }
let(:case_court_mandate) { past_court_date.case_court_mandates.first }

it "displays all court details" do
render template: "past_court_dates/show"
before { render template: "past_court_dates/show" }

it "displays all court details" do
expect(rendered).to include(past_court_date.judge.name)
expect(rendered).to include(past_court_date.hearing_type.name)

expect(rendered).to include(case_court_mandate.mandate_text)
expect(rendered).to include(case_court_mandate.implementation_status.humanize)
end

it "displays the download button for .docx" do
expect(rendered).to include "Download Report (.docx)"
expect(rendered).to include "/casa_cases/#{past_court_date.casa_case.id}/past_court_dates/#{past_court_date.id}.docx"
end
end

shared_examples_for "a past court date with no court details" do
let(:past_court_date) { create(:past_court_date) }

Expand Down