Skip to content

Commit

Permalink
Add ReportTemplate model
Browse files Browse the repository at this point in the history
Store future template in binary format in databse along with optional
configuration for the code using that template.
  • Loading branch information
fumimowdan committed Oct 5, 2023
1 parent ce0abd6 commit 94da036
Show file tree
Hide file tree
Showing 8 changed files with 144 additions and 1 deletion.
4 changes: 4 additions & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -106,3 +106,7 @@ RSpec/DescribeClass:
RSpec/AnyInstance:
Exclude:
- 'spec/requests/submission_spec.rb'

Rails/UniqueValidationWithoutIndex:
Exclude:
- 'app/models/report_template.rb'
22 changes: 22 additions & 0 deletions app/models/report_template.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# == Schema Information
#
# Table name: report_templates
#
# id :bigint not null, primary key
# config :jsonb
# file :binary
# filename :string
# report_class :string
# created_at :datetime not null
# updated_at :datetime not null
#
class ReportTemplate < ApplicationRecord
validates(:file, presence: true)
validates(:filename, presence: true)
validates(:report_class, presence: true, uniqueness: true)
validates(:config, presence: true)

validate do |record|
HomeOfficeReportConfigValidator.new(record).validate
end
end
12 changes: 12 additions & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -48,3 +48,15 @@ en:
success: 'User was successfully removed.'
omniauth_callbacks:
no_account: 'You have not yet an account!'

activerecord:
errors:
models:
report_template:
attributes:
file:
invalid: "File parsing error"
config:
missing_header_mappings: "config.header_mappings must be present"
missing_worksheet_name: "config.worksheet_name must be present"
invalid_worksheet_name: "config.worksheet_name not present in file"
14 changes: 14 additions & 0 deletions db/migrate/20231004134346_create_report_templates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class CreateReportTemplates < ActiveRecord::Migration[7.0]
def change
create_table :report_templates do |t|
t.string :report_class
t.string :filename
t.binary :file
t.jsonb :config

t.timestamps
end

add_index :report_templates, :report_class
end
end
12 changes: 11 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions spec/factories/report_templates.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
# == Schema Information
#
# Table name: report_templates
#
# id :bigint not null, primary key
# config :jsonb
# file :binary
# filename :string
# report_class :string
# created_at :datetime not null
# updated_at :datetime not null
#
FactoryBot.define do
factory :report_template do
file { StringIO.new("") }
filename { "file.xlsx" }
report_class { "SomeClass" }
config do
{
"some" => true,
}
end

factory :home_office_report_template do
file { Rails.root.join("spec/fixtures/test_homeoffice_template.xlsx").read }
filename { "test_homeoffice_template.xlsx" }
report_class { "Reports::HomeOffice" }
config do
{
"worksheet_name" => "Data",
"header_mappings" => {
"Column A" => %w[urn],
"bar" => %w[applicants.given_name applicants.family_name],
},
}
end
end
end
end
Binary file added spec/fixtures/test_homeoffice_template.xlsx
Binary file not shown.
42 changes: 42 additions & 0 deletions spec/models/report_template_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
# == Schema Information
#
# Table name: report_templates
#
# id :bigint not null, primary key
# config :jsonb
# file :binary
# filename :string
# report_class :string
# created_at :datetime not null
# updated_at :datetime not null
#
require "rails_helper"

RSpec.describe ReportTemplate do
describe "validations" do
context "common report template validation" do
subject(:report_template) { build(:report_template) }

it { expect(report_template).to validate_presence_of(:file) }
it { expect(report_template).to validate_presence_of(:filename) }
it { expect(report_template).to validate_presence_of(:report_class) }
it { expect(report_template).to validate_uniqueness_of(:report_class) }
it { expect(report_template).to validate_presence_of(:config) }
end

context "homeoffice report validation" do
subject(:report_template) { build(:home_office_report_template) }

let(:validator) { HomeOfficeReportConfigValidator.new(report_template) }

before do
allow(HomeOfficeReportConfigValidator).to receive(:new).and_return(validator)
report_template.valid?
end

it { expect(report_template).to be_valid }

it { expect(HomeOfficeReportConfigValidator).to have_received(:new).with(report_template) }
end
end
end

0 comments on commit 94da036

Please sign in to comment.