-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Store future template in binary format in databse along with optional configuration for the code using that template.
- Loading branch information
1 parent
ce0abd6
commit 94da036
Showing
8 changed files
with
144 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |