Skip to content

Commit

Permalink
Refactor Reports::Base to be generic
Browse files Browse the repository at this point in the history
Base defaults to csv but allow other file extension to be created
  • Loading branch information
fumimowdan committed Oct 5, 2023
1 parent 2698a15 commit ce0abd6
Show file tree
Hide file tree
Showing 12 changed files with 45 additions and 35 deletions.
2 changes: 1 addition & 1 deletion app/models/reports/applications.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
module Reports
class Applications < Base
def csv
def generate
CSV.generate do |csv|
csv << header
rows.find_each(batch_size: 50) { |row| csv << columns(row) }
Expand Down
14 changes: 12 additions & 2 deletions app/models/reports/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,23 @@ def initialize(**kwargs)

attr_reader :name, :kwargs

class << self
def file_ext(value)
@file_ext = value
end

def get_file_ext
@file_ext || "csv"
end
end

def filename
current_time = Time.zone.now.strftime("%Y%m%d-%H%M%S")

"#{name}-#{current_time}.csv"
"#{name}-#{current_time}.#{self.class.get_file_ext}"
end

def csv; end
def generate; end

def post_generation_hook; end
end
Expand Down
2 changes: 1 addition & 1 deletion app/models/reports/payroll.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Reports
class Payroll < Base
def csv
def generate
CSV.generate do |csv|
csv << header
rows.each { |row| csv << row }
Expand Down
2 changes: 1 addition & 1 deletion app/models/reports/qa_report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def initialize(...)
@name = [@name, status].join("-")
end

def csv
def generate
CSV.generate do |csv|
csv << header
rows.each { |row| csv << row }
Expand Down
2 changes: 1 addition & 1 deletion app/models/reports/standing_data.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module Reports
class StandingData < Base
def csv
def generate
CSV.generate do |csv|
csv << header
rows.each { |row| csv << row }
Expand Down
2 changes: 1 addition & 1 deletion app/services/report.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def initialize(report_id, **kwargs)
delegate :post_generation_hook, to: :report

def data
@data ||= report.csv
@data ||= report.generate
end

private
Expand Down
2 changes: 1 addition & 1 deletion spec/features/admin_console/reports_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def then_the_standing_data_csv_report_is_downloaded
end

def then_the_home_office_csv_report_is_downloaded
expect(page.response_headers["Content-Type"]).to match(/text\/csv/)
expect(page.response_headers["Content-Type"]).to match(/application\/vnd.openxmlformats-officedocument.spreadsheetml.sheet/)
expect(page.response_headers["Content-Disposition"]).to include "attachment"
expect(page.response_headers["Content-Disposition"]).to match(/filename="reports-home-office.*/)
end
Expand Down
16 changes: 8 additions & 8 deletions spec/models/reports/applications_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,17 +66,17 @@ module Reports
].join(",")
end

it { expect(report.csv).to include(header) }
it { expect(report.generate).to include(header) }
end

context "returns all applications" do
it { expect(report.csv).to include(application.urn) }
it { expect(report.csv).to include(initial.urn) }
it { expect(report.csv).to include(home_office.urn) }
it { expect(report.csv).to include(school.urn) }
it { expect(report.csv).to include(banking.urn) }
it { expect(report.csv).to include(paid.urn) }
it { expect(report.csv).to include(rejected.urn) }
it { expect(report.generate).to include(application.urn) }
it { expect(report.generate).to include(initial.urn) }
it { expect(report.generate).to include(home_office.urn) }
it { expect(report.generate).to include(school.urn) }
it { expect(report.generate).to include(banking.urn) }
it { expect(report.generate).to include(paid.urn) }
it { expect(report.generate).to include(rejected.urn) }
end
end
end
Expand Down
12 changes: 6 additions & 6 deletions spec/models/reports/payroll_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,26 +21,26 @@ module Reports
end
end

describe "#csv" do
describe "#generate" do
let(:progress) { build(:application_progress, banking_approval_completed_at: Time.zone.now) }

it "returns applicants who have banking details approved" do
app = create(:application, application_progress: progress)

expect(report.csv).to include(app.applicant.family_name)
expect(report.generate).to include(app.applicant.family_name)
end

it "does not return applicants who have not the bankind details approved" do
progress.banking_approval_completed_at = nil
app = create(:application, application_progress: progress)

expect(report.csv).not_to include(app.urn)
expect(report.generate).not_to include(app.urn)
end

it "returns the data in CSV format" do
application = create(:application, application_progress: progress)

expect(report.csv).to include([
expect(report.generate).to include([
"", # No.
"Prof", # TITLE
application.applicant.given_name, # FORENAME
Expand Down Expand Up @@ -116,12 +116,12 @@ module Reports
RIGHT_TO_WORK_CONFIRM_STATUS
].join(",")

expect(report.csv).to include(expected_header)
expect(report.generate).to include(expected_header)
end

context "includes applications from the csv before invoking `post_generation_hook`" do
let(:app) { create(:application, application_progress: progress) }
let(:csv) { report.csv }
let(:csv) { report.generate }

before { app }

Expand Down
6 changes: 3 additions & 3 deletions spec/models/reports/qa_report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ module Reports
it "returns the data in CSV format" do
application = create(:application)

expect(report.csv).to include([
expect(report.generate).to include([
application.urn,
application.applicant.full_name,
application.applicant.date_of_birth.strftime("%d/%m/%Y"),
Expand All @@ -50,7 +50,7 @@ module Reports
it "returns the data including rejection reasons in CSV format" do
application = create(:application, application_progress: build(:application_progress, rejection_completed_at: Time.zone.now, status: :rejected, rejection_reason: :request_to_re_submit, rejection_details: "Some details"))

expect(report.csv).to include([
expect(report.generate).to include([
application.urn,
application.applicant.full_name,
application.applicant.date_of_birth.strftime("%d/%m/%Y"),
Expand Down Expand Up @@ -85,7 +85,7 @@ module Reports
]
expected_header += ["Rejection Reason", "Rejection Details"] if status == "rejected"

expect(report.csv).to include(expected_header.join(","))
expect(report.generate).to include(expected_header.join(","))
end
end
end
Expand Down
16 changes: 8 additions & 8 deletions spec/models/reports/standing_data_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,41 +20,41 @@ module Reports
end
end

describe "#csv" do
describe "#generate" do
let(:progress) { build(:application_progress, school_checks_completed_at: Time.zone.now, banking_approval_completed_at: nil) }

it "returns applicants who have completed initial checks but not home office checks" do
app = create(:application, application_progress: progress)

expect(report.csv).to include(app.urn)
expect(report.generate).to include(app.urn)
end

it "does not return rejected applicants" do
progress.rejection_completed_at = Time.zone.now
app = create(:application, application_progress: progress)

expect(report.csv).not_to include(app.urn)
expect(report.generate).not_to include(app.urn)
end

it "does not return applicants who have not completed initial checks" do
progress.school_checks_completed_at = nil
app = create(:application, application_progress: progress)

expect(report.csv).not_to include(app.urn)
expect(report.generate).not_to include(app.urn)
end

it "does not return applicants who have completed home office checks" do
progress.banking_approval_completed_at = Time.zone.now
app = create(:application, application_progress: progress)

expect(report.csv).not_to include(app.urn)
expect(report.generate).not_to include(app.urn)
end

# rubocop:disable RSpec/ExampleLength
it "returns the data in CSV format" do
application = create(:application, application_progress: progress)

expect(report.csv).to include([
expect(report.generate).to include([
application.urn,
application.applicant.given_name,
application.applicant.middle_name,
Expand All @@ -80,12 +80,12 @@ module Reports
Student_loan
].join(",")

expect(report.csv).to include(expected_header)
expect(report.generate).to include(expected_header)
end

context "includes applications from the csv before invoking `post_generation_hook`" do
let(:app) { create(:application, application_progress: progress) }
let(:csv) { report.csv }
let(:csv) { report.generate }

before { app }

Expand Down
4 changes: 2 additions & 2 deletions spec/services/report_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@
end

it { expect(Reports::QaReport).to have_received(:new).with(status:) }
it { expect(report).to have_received(:csv) }
it { expect(report).to have_received(:generate) }
end

context "other report data" do
Expand All @@ -69,7 +69,7 @@
end

it { expect(Reports::HomeOffice).to have_received(:new) }
it { expect(report).to have_received(:csv) }
it { expect(report).to have_received(:generate) }
end
# rubocop:enable RSpec/VerifiedDoubles
end
Expand Down

0 comments on commit ce0abd6

Please sign in to comment.