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

Make the invoice number formattable #99

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 13 additions & 1 deletion app/models/spree/printables/invoice/base_view.rb
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def shipping_methods

def number
if use_sequential_number?
Spree::PrintInvoice::Config.next_number
formatted_number
else
printable.number
end
Expand All @@ -66,6 +66,18 @@ def after_save_actions

private

def formatted_number
if (Object.const_get('::Spree::PrintInvoice::NumberFormatter') rescue false)

Choose a reason for hiding this comment

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

Avoid using rescue in its modifier form.

Copy link

@mkoentopf mkoentopf Sep 1, 2019

Choose a reason for hiding this comment

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

Or better use defined?(::Spree::PrintInvoice::NumberFormatter). So you do not need a rescue.

::Spree::PrintInvoice::NumberFormatter.new(next_number).to_s
else
next_number
end
end

def next_number
Spree::PrintInvoice::Config.next_number
end

def increase_invoice_number!
Spree::PrintInvoice::Config.increase_invoice_number!
end
Expand Down
27 changes: 24 additions & 3 deletions spec/models/spree/printables/invoice/base_view_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -68,14 +68,17 @@
end

describe '#number' do
before do
allow(Spree::PrintInvoice::Config).to receive(:next_number) { 77 }
end

context 'when using sequential numbers' do
before do
allow(Spree::PrintInvoice::Config).to receive(:use_sequential_number?) { true }
end

it 'calls next_number on Spree::PrintInvoice::Config' do
expect(Spree::PrintInvoice::Config).to receive(:next_number)
base_view.number
it 'returns the next number without additional formatting' do
expect(base_view.number).to eq(77)
end
end

Expand All @@ -89,5 +92,23 @@
base_view.number
end
end

context 'when using a NumberFormatter' do
before do
class Spree::PrintInvoice::NumberFormatter
def initialize(number)
@number = number
end

def to_s
"MY-NICE-INVOICE-#{@number}"
end
end
end

it 'returns a nicely formatted number' do
expect(base_view.number).to eq("MY-NICE-INVOICE-77")

Choose a reason for hiding this comment

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

Prefer single-quoted strings when you don't need string interpolation or special symbols.

end
end
end
end