Skip to content

Commit 816de56

Browse files
authored
Merge branch 'main' into fix-ci-badge
2 parents fcb1f86 + 50ae4d3 commit 816de56

File tree

5 files changed

+26
-8
lines changed

5 files changed

+26
-8
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
- Allow monetizing methods with kwargs
6+
37
## 1.15.0
48

59
- Bump money version to ~> 6.16

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
MIT License
22

33
Copyright (c) 2012 Andreas Loupasakis
4-
Copyright (c) 2023 Shane Emmons
4+
Copyright (c) 2024 Shane Emmons
55

66
Permission is hereby granted, free of charge, to any person obtaining a copy
77
of this software and associated documentation files (the "Software"), to deal

lib/money-rails/active_record/monetizable.rb

+15-5
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,19 @@ def track_monetized_attribute(name, value)
178178
end
179179
end
180180

181-
def read_monetized(name, subunit_name, options = {}, *args)
182-
# Get the cents
183-
amount = public_send(subunit_name, *args)
181+
def read_monetized(name, subunit_name, options = nil, *args, **kwargs)
182+
# Ruby 2.x compatibility
183+
if options.nil?
184+
options = kwargs
185+
kwargs = {}
186+
end
187+
188+
if kwargs.any?
189+
amount = public_send(subunit_name, *args, **kwargs)
190+
else
191+
# Ruby 2.x does not allow empty kwargs
192+
amount = public_send(subunit_name, *args)
193+
end
184194

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

spec/active_record/monetizable_spec.rb

+4
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

+2-2
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+
amount_cents + tax_cents + foo + bar
1212
end
1313
end

0 commit comments

Comments
 (0)