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

Add configurable support for using the shopify-money gem #213

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
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
2 changes: 1 addition & 1 deletion lib/double_entry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
require 'active_record/locking_extensions'
require 'active_record/locking_extensions/log_subscriber'
require 'active_support/all'
require 'money'
require 'rails/railtie'

require 'double_entry/version'
Expand All @@ -16,6 +15,7 @@
require 'double_entry/locking'
require 'double_entry/transfer'
require 'double_entry/validation'
require 'double_entry/money'

# Keep track of all the monies!
#
Expand Down
8 changes: 8 additions & 0 deletions lib/double_entry/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,19 @@ module DoubleEntry

class Configuration
attr_accessor :json_metadata
attr_writer :money_adapter

def initialize
@json_metadata = false
end

def money_adapter
@money_adapter ||= begin
require 'money'
::Money
end
end

delegate(
:accounts,
:accounts=,
Expand Down
15 changes: 15 additions & 0 deletions lib/double_entry/money.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# encoding: utf-8
module DoubleEntry
module Money
class << self
attr_writer :adapter

def adapter
@adapter ||= DoubleEntry.config.money_adapter
end

delegate(:new, to: :adapter)
delegate_missing_to(:adapter)
end
end
end
16 changes: 16 additions & 0 deletions spec/double_entry/money_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
RSpec.describe DoubleEntry::Money do
it 'delegates singleton methods to the adapter' do
DoubleEntry::Money.adapter = Class.new do
def self.zero
0
end

def test
12345
end
end
expect(DoubleEntry::Money.new.test).to eq(12345)
expect(DoubleEntry::Money.zero).to eq(0)
DoubleEntry::Money.adapter = ::Money
end
end
3 changes: 2 additions & 1 deletion spec/support/money.rb
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
Money.locale_backend = :i18n
require 'money'
::Money.locale_backend = :i18n