-
Notifications
You must be signed in to change notification settings - Fork 35
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fixes #870
- Loading branch information
Showing
16 changed files
with
237 additions
and
8 deletions.
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
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,27 @@ | ||
# frozen_string_literal: true | ||
|
||
class InvoiceRefTemplate | ||
TEMPLATES = { | ||
'$id': proc { invoice.id } | ||
}.freeze | ||
|
||
def self.call(invoice, template) | ||
new(invoice, template).call | ||
end | ||
|
||
attr_reader :invoice, :template | ||
|
||
def initialize(invoice, template) | ||
@invoice = invoice | ||
@template = template | ||
end | ||
|
||
def call | ||
result = template | ||
TEMPLATES.each do |key, value_block| | ||
value = instance_exec(&value_block).to_s | ||
result = result.gsub(key.to_s, value) | ||
end | ||
result | ||
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
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,6 @@ | ||
class AddAccountInvoiceReference < ActiveRecord::Migration[5.2] | ||
def change | ||
add_column :accounts, :customer_invoice_ref_template, :string, default: '$id', null: false | ||
add_column :accounts, :vendor_invoice_ref_template, :string, default: '$id', null: false | ||
end | ||
end |
15 changes: 15 additions & 0 deletions
15
db/secondbase/migrate/20210218095038_add_invoice_reference.rb
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,15 @@ | ||
class AddInvoiceReference < ActiveRecord::Migration[5.2] | ||
def up | ||
add_column 'billing.invoices', :reference, :string | ||
|
||
execute <<-SQL | ||
UPDATE billing.invoices SET reference = id::varchar | ||
SQL | ||
|
||
add_index 'billing.invoices', :reference | ||
end | ||
|
||
def down | ||
remove_column 'billing.invoices', :reference | ||
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
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
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,47 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe InvoiceRefTemplate, '.call' do | ||
subject do | ||
InvoiceRefTemplate.call(invoice, template) | ||
end | ||
|
||
let!(:contractor) { FactoryBot.create(:vendor) } | ||
let!(:account) { FactoryBot.create(:account, account_attrs) } | ||
let(:account_attrs) do | ||
{ contractor: contractor } | ||
end | ||
let(:invoice) { FactoryBot.create(:invoice, :vendor, :manual, invoice_attrs) } | ||
let(:invoice_attrs) do | ||
{ account: account } | ||
end | ||
|
||
context 'when template is "$id"' do | ||
let(:template) { '$id' } | ||
|
||
it { is_expected.to eq invoice.id.to_s } | ||
end | ||
|
||
context 'when template is "test_$id"' do | ||
let(:template) { 'test_$id' } | ||
|
||
it { is_expected.to eq "test_#{invoice.id}" } | ||
end | ||
|
||
context 'when template is "$id_test"' do | ||
let(:template) { '$id_test' } | ||
|
||
it { is_expected.to eq "#{invoice.id}_test" } | ||
end | ||
|
||
context 'when template is "$id_test_$id"' do | ||
let(:template) { '$id_test_$id' } | ||
|
||
it { is_expected.to eq "#{invoice.id}_test_#{invoice.id}" } | ||
end | ||
|
||
context 'when template is "test"' do | ||
let(:template) { 'test' } | ||
|
||
it { is_expected.to eq 'test' } | ||
end | ||
end |
Oops, something went wrong.