Skip to content

Commit a128610

Browse files
committed
Allow monetizing methods with kwargs
1 parent 5fac874 commit a128610

File tree

3 files changed

+10
-6
lines changed

3 files changed

+10
-6
lines changed

lib/money-rails/active_record/monetizable.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -118,8 +118,8 @@ def monetize(*fields)
118118

119119

120120
# Getter for monetized attribute
121-
define_method name do |*args|
122-
read_monetized name, subunit_name, options, *args
121+
define_method name do |*args, **kwargs|
122+
read_monetized name, subunit_name, options, *args, **kwargs
123123
end
124124

125125
# Setter for monetized attribute
@@ -178,9 +178,9 @@ def track_monetized_attribute(name, value)
178178
end
179179
end
180180

181-
def read_monetized(name, subunit_name, options = {}, *args)
181+
def read_monetized(name, subunit_name, options = {}, *args, **kwargs)
182182
# Get the cents
183-
amount = public_send(subunit_name, *args)
183+
amount = public_send(subunit_name, *args, **kwargs)
184184

185185
return if amount.nil? && options[:allow_nil]
186186
# Get the currency object

spec/active_record/monetizable_spec.rb

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -725,6 +725,10 @@ class SubProduct < Product
725725
expect(transaction.total).to eq(Money.new(3000, :usd))
726726
end
727727

728+
it "constructs the money object from the mapped method value with arguments" do
729+
expect(transaction.total(1, bar: 2)).to eq(Money.new(3003, :usd))
730+
end
731+
728732
it "allows currency column postfix to be blank" do
729733
allow(MoneyRails::Configuration).to receive(:currency_column) { { postfix: nil, column_name: 'currency' } }
730734
expect(dummy_product_with_nil_currency.price.currency).to eq(Money::Currency.find(:gbp))

spec/dummy/app/models/transaction.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ class Transaction < ActiveRecord::Base
77

88
monetize :optional_amount_cents, with_model_currency: :currency, allow_nil: true
99

10-
def total_cents
11-
return amount_cents + tax_cents
10+
def total_cents(foo = 0, bar: 0)
11+
return amount_cents + tax_cents + foo + bar
1212
end
1313
end

0 commit comments

Comments
 (0)