Skip to content

Commit b32228e

Browse files
authored
Merge branch 'main' into fix-monetized-attributes-class-discrepancy
2 parents 42a5024 + 7192ce1 commit b32228e

File tree

6 files changed

+34
-9
lines changed

6 files changed

+34
-9
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

README.md

+8-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# RubyMoney - Money-Rails
22

33
[![Gem Version](https://badge.fury.io/rb/money-rails.svg)](http://badge.fury.io/rb/money-rails)
4-
[![Build Status](https://secure.travis-ci.org/RubyMoney/money-rails.svg?branch=master)](http://travis-ci.org/RubyMoney/money-rails)
4+
[![Ruby](https://github.com/RubyMoney/money-rails/actions/workflows/ruby.yml/badge.svg)](https://github.com/RubyMoney/money-rails/actions/workflows/ruby.yml)
55
[![License](http://img.shields.io/:license-mit-green.svg?style=flat)](http://opensource.org/licenses/MIT)
66

77
## Introduction
@@ -474,6 +474,12 @@ MoneyRails.configure do |config|
474474
# symbol: nil,
475475
# sign_before_symbol: nil
476476
# }
477+
478+
# Set whether an error should be raised when parsing money values
479+
# This includes assigning to a monetized field with the wrong currency
480+
# Default value is false
481+
#
482+
# config.raise_error_on_money_parsing = true
477483
end
478484
```
479485

@@ -493,6 +499,7 @@ end
493499
* `amount_column`: Provide values for the amount column (holding the fractional part of a money object).
494500
* `currency_column`: Provide default values or even disable (`present: false`) the currency column.
495501
* `rounding_mode`: Set `Money.rounding_mode` to one of the BigDecimal constants.
502+
* `raise_error_on_money_parsing`: Set whether errors should be raised when parsing money values
496503

497504
### Helpers
498505

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)