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

Placement reports #4768

Merged
merged 8 commits into from
Jun 2, 2023
Merged
Show file tree
Hide file tree
Changes from 6 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
15 changes: 15 additions & 0 deletions app/controllers/placement_reports_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
class PlacementReportsController < ApplicationController
after_action :verify_authorized

def index
authorize :application, :see_reports_page?
placement_report = PlacementExportCsvService.new(casa_org: current_organization).perform

respond_to do |format|
format.csv do
send_data placement_report,
filename: "placement-report-#{Time.current.strftime("%Y-%m-%d")}.csv"
end
end
end
end
8 changes: 8 additions & 0 deletions app/decorators/placement_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,12 @@ def formatted_date
def placement_info
["Started At: #{formatted_date}", "Placement Type: #{placement_type&.name}"].compact.join(" - ")
end

def placement_started_at
I18n.l(placement.placement_started_at, format: :full, default: nil)
end

def created_at
I18n.l(placement.created_at, format: :full, default: nil)
end
end
1 change: 1 addition & 0 deletions app/models/casa_org.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class CasaOrg < ApplicationRecord
has_many :mileage_rates, dependent: :destroy
has_many :case_assignments, through: :users, source: :casa_cases
has_many :languages, dependent: :destroy
has_many :placements, through: :casa_cases
has_one_attached :logo
has_one_attached :court_report_template

Expand Down
37 changes: 37 additions & 0 deletions app/services/placement_export_csv_service.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
require "csv"

class PlacementExportCsvService
attr_reader :placements

def initialize(casa_org:)
@casa_org = casa_org
end

def perform
placements = fetch_placements

CSV.generate(headers: true) do |csv|
csv << full_data.keys.map(&:to_s).map(&:titleize)
placements.decorate.each do |placement|
csv << full_data(placement).values
end
end
end

private

def full_data(placement = nil)
{
casa_org: placement&.id,
casa_case_number: placement&.casa_case&.case_number,
placement_type_id: placement&.placement_type_id,
placement_started_at: placement&.placement_started_at,
created_at: placement&.created_at,
creator_name: placement&.creator&.display_name
}
end

def fetch_placements
@casa_org.placements
end
end
10 changes: 10 additions & 0 deletions app/views/reports/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,16 @@
<% end %>
<% end %>
</li>
<li>
<%= form_with url: placement_reports_path(format: :csv), method: :get do |f| %>
<%= button_tag(
data: { disable_with: "Downloading Placements Report" },
class: "btn-sm main-btn primary-btn btn-hover report-form-submit"
) do %>
<i class="lni lni-check-box mr-10"></i> Placements Report
<% end %>
<% end %>
</li>
</ul>
</div>

Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,6 +99,7 @@
resources :missing_data_reports, only: %i[index]
resources :learning_hours_reports, only: %i[index]
resources :followup_reports, only: :index
resources :placement_reports, only: :index

resources :supervisors, except: %i[destroy show], concerns: %i[with_datatable] do
member do
Expand Down
15 changes: 15 additions & 0 deletions spec/services/placement_export_csv_service_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
require "rails_helper"
require "factory_bot_rails"
Copy link
Collaborator

Choose a reason for hiding this comment

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

You can remove this line.


RSpec.describe PlacementExportCsvService do
it "creates a Placements CSV with placement headers" do
casa_org = create(:casa_org, name: "Fake Name", display_name: "Fake Display Name")
placement_type = create(:placement_type, casa_org: casa_org)
creator = create(:user)
placement = create(:placement, creator: creator, placement_type: placement_type) # rubocop:disable Lint/UselessAssignment

csv_headers = "Casa Org,Casa Case Number,Placement Type,Placement Started At,Created At,Creator Name\n"
result = PlacementExportCsvService.new(casa_org: casa_org).perform
expect(result).to start_with(csv_headers)
end
end