money-rails 1.10.0 → 1.11.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -0
- data/README.md +33 -26
- data/Rakefile +6 -6
- data/lib/generators/templates/money.rb +12 -12
- data/lib/money-rails/active_model/validator.rb +11 -6
- data/lib/money-rails/active_record/monetizable.rb +12 -12
- data/lib/money-rails/configuration.rb +2 -2
- data/lib/money-rails/helpers/action_view_extension.rb +12 -12
- data/lib/money-rails/money.rb +14 -16
- data/lib/money-rails/mongoid/money.rb +2 -2
- data/lib/money-rails/mongoid/two.rb +2 -2
- data/lib/money-rails/test_helpers.rb +20 -6
- data/lib/money-rails/version.rb +1 -1
- data/money-rails.gemspec +1 -1
- data/spec/active_record/monetizable_spec.rb +46 -40
- data/spec/configuration_spec.rb +2 -2
- data/spec/dummy/app/models/dummy_product.rb +1 -1
- data/spec/dummy/app/models/priceable.rb +2 -2
- data/spec/dummy/app/models/product.rb +24 -22
- data/spec/dummy/app/models/service.rb +1 -1
- data/spec/dummy/app/models/transaction.rb +4 -4
- data/spec/dummy/app/views/layouts/application.html.erb +1 -1
- data/spec/dummy/config/initializers/money.rb +9 -9
- data/spec/dummy/config/initializers/session_store.rb +1 -1
- data/spec/dummy/config/initializers/wrap_parameters.rb +1 -1
- data/spec/dummy/db/migrate/20120712202655_add_sale_price_cents_to_product.rb +1 -1
- data/spec/helpers/action_view_extension_spec.rb +4 -4
- data/spec/helpers/form_helper_spec.rb +4 -4
- data/spec/mongoid/five_spec.rb +13 -13
- data/spec/mongoid/four_spec.rb +14 -14
- data/spec/mongoid/three_spec.rb +14 -14
- data/spec/mongoid/two_spec.rb +7 -7
- data/spec/test_helpers_spec.rb +11 -3
- metadata +4 -4
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 37b04a7be15864c3d863599f9459d060fb4438ee
|
4
|
+
data.tar.gz: 60663ea37c3f404e17af412b7243d7dfc1844c72
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c0c1209bf5dbbae3363bd3ef19bd5dd0e4685357a874f8074e0a1948b40ebe55da05248aa90c335cdea61b5af0ab1a125239b1a275cbccff0ea0876a7d880de6
|
7
|
+
data.tar.gz: 110bd949e2b5077755d4f736e91e46167d6fc7e89ffadfd2f78cd6777b0730def9c9c2c811697a4eac059678a6ca73fe7c2220c0b058aeed250ac5aa8cdc75f0
|
data/CHANGELOG.md
CHANGED
data/README.md
CHANGED
@@ -76,7 +76,7 @@ money attribute, then you can provide an ```as``` argument with a string
|
|
76
76
|
value to the ```monetize``` macro:
|
77
77
|
|
78
78
|
```ruby
|
79
|
-
monetize :discount_subunit, :
|
79
|
+
monetize :discount_subunit, as: "discount"
|
80
80
|
```
|
81
81
|
|
82
82
|
Now the model objects will have a ```discount``` attribute which is a `Money`
|
@@ -135,7 +135,7 @@ monetized field, you can use the `:allow_nil` parameter:
|
|
135
135
|
|
136
136
|
```ruby
|
137
137
|
# in Product model
|
138
|
-
monetize :optional_price_cents, :
|
138
|
+
monetize :optional_price_cents, allow_nil: true
|
139
139
|
|
140
140
|
# in Migration
|
141
141
|
def change
|
@@ -166,10 +166,10 @@ You can also pass along
|
|
166
166
|
such as this:
|
167
167
|
|
168
168
|
```ruby
|
169
|
-
monetize :price_in_a_range_cents, :
|
170
|
-
:
|
171
|
-
:
|
172
|
-
:
|
169
|
+
monetize :price_in_a_range_cents, allow_nil: true,
|
170
|
+
numericality: {
|
171
|
+
greater_than_or_equal_to: 0,
|
172
|
+
less_than_or_equal_to: 10000
|
173
173
|
}
|
174
174
|
```
|
175
175
|
|
@@ -177,14 +177,14 @@ Or, if you prefer, you can skip validations entirely for the attribute. This is
|
|
177
177
|
are aggregate methods and you wish to avoid executing them on every record save.
|
178
178
|
|
179
179
|
```ruby
|
180
|
-
monetize :price_in_a_range_cents, :
|
180
|
+
monetize :price_in_a_range_cents, disable_validation: true
|
181
181
|
```
|
182
182
|
|
183
183
|
You can also skip validations independently from each other by simply passing `false`
|
184
184
|
to the validation you are willing to skip, like this:
|
185
185
|
|
186
186
|
```ruby
|
187
|
-
monetize :price_in_a_range_cents, :
|
187
|
+
monetize :price_in_a_range_cents, numericality: false
|
188
188
|
```
|
189
189
|
|
190
190
|
### Mongoid 2.x and 3.x
|
@@ -214,14 +214,14 @@ obj.save
|
|
214
214
|
# => true
|
215
215
|
|
216
216
|
obj
|
217
|
-
# => #<Product _id: 4fe865699671383656000001, _type: nil, price: {:
|
217
|
+
# => #<Product _id: 4fe865699671383656000001, _type: nil, price: {cents: 100, currency_iso: "EUR"}>
|
218
218
|
|
219
219
|
obj.price
|
220
220
|
#=> #<Money cents:100 currency:EUR>
|
221
221
|
|
222
222
|
## You can access the money hash too:
|
223
223
|
obj[:price]
|
224
|
-
# => {:
|
224
|
+
# => {cents: 100, currency_iso: "EUR"}
|
225
225
|
```
|
226
226
|
|
227
227
|
The usual options on `field` as `index`, `default`, ..., are available.
|
@@ -292,7 +292,7 @@ class Product < ActiveRecord::Base
|
|
292
292
|
# Use EUR as model level currency
|
293
293
|
register_currency :eur
|
294
294
|
|
295
|
-
monetize :discount_subunit, :
|
295
|
+
monetize :discount_subunit, as: "discount"
|
296
296
|
monetize :bonus_cents
|
297
297
|
|
298
298
|
end
|
@@ -320,8 +320,8 @@ class Product < ActiveRecord::Base
|
|
320
320
|
# Use EUR as the model level currency
|
321
321
|
register_currency :eur
|
322
322
|
|
323
|
-
monetize :discount_subunit, :
|
324
|
-
monetize :bonus_cents, :
|
323
|
+
monetize :discount_subunit, as: "discount"
|
324
|
+
monetize :bonus_cents, with_currency: :gbp
|
325
325
|
|
326
326
|
end
|
327
327
|
```
|
@@ -357,7 +357,7 @@ end
|
|
357
357
|
|
358
358
|
# Now instantiating with a specific currency overrides
|
359
359
|
# the model and global currencies
|
360
|
-
t = Transaction.new(:
|
360
|
+
t = Transaction.new(amount_cents: 2500, currency: "CAD")
|
361
361
|
t.amount == Money.new(2500, "CAD") # true
|
362
362
|
```
|
363
363
|
|
@@ -413,15 +413,15 @@ MoneyRails.configure do |config|
|
|
413
413
|
#
|
414
414
|
# Example:
|
415
415
|
# config.register_currency = {
|
416
|
-
# :
|
417
|
-
# :
|
418
|
-
# :
|
419
|
-
# :
|
420
|
-
# :
|
421
|
-
# :
|
422
|
-
# :
|
423
|
-
# :
|
424
|
-
# :
|
416
|
+
# priority: 1,
|
417
|
+
# iso_code: "EU4",
|
418
|
+
# name: "Euro with subunit of 4 digits",
|
419
|
+
# symbol: "€",
|
420
|
+
# symbol_first: true,
|
421
|
+
# subunit: "Subcent",
|
422
|
+
# subunit_to_unit: 10000,
|
423
|
+
# thousands_separator: ".",
|
424
|
+
# decimal_mark: ","
|
425
425
|
# }
|
426
426
|
|
427
427
|
# Specify a rounding mode
|
@@ -444,9 +444,9 @@ MoneyRails.configure do |config|
|
|
444
444
|
# Example:
|
445
445
|
#
|
446
446
|
# config.default_format = {
|
447
|
-
# :
|
448
|
-
# :
|
449
|
-
# :
|
447
|
+
# no_cents_if_whole: nil,
|
448
|
+
# symbol: nil,
|
449
|
+
# sign_before_symbol: nil
|
450
450
|
# }
|
451
451
|
end
|
452
452
|
```
|
@@ -516,6 +516,13 @@ is_expected.to monetize(:price).with_currency(:gbp)
|
|
516
516
|
By using the `with_currency` chain you can specify the expected currency
|
517
517
|
for the chosen money attribute. (You can also combine all the chains.)
|
518
518
|
|
519
|
+
```ruby
|
520
|
+
is_expected.to monetize(:price).with_model_currency(:currency)
|
521
|
+
```
|
522
|
+
|
523
|
+
By using the `with_model_currency` chain you can specify the attribute that
|
524
|
+
contains the currency to be used for the chosen money attribute.
|
525
|
+
|
519
526
|
For examples on using the test_helpers look at
|
520
527
|
[test_helpers_spec.rb](https://github.com/RubyMoney/money-rails/blob/master/spec/test_helpers_spec.rb)
|
521
528
|
|
data/Rakefile
CHANGED
@@ -21,9 +21,9 @@ require 'rspec/core/rake_task'
|
|
21
21
|
|
22
22
|
RSpec::Core::RakeTask.new
|
23
23
|
|
24
|
-
task :
|
25
|
-
task :
|
26
|
-
task :
|
24
|
+
task default: "spec:all"
|
25
|
+
task test: :spec
|
26
|
+
task spec: :prepare_test_env
|
27
27
|
|
28
28
|
desc "Prepare money-rails engine test environment"
|
29
29
|
task :prepare_test_env do
|
@@ -79,13 +79,13 @@ namespace :spec do
|
|
79
79
|
task(:rails3) { run_with_gemfile 'gemfiles/rails3.gemfile' }
|
80
80
|
|
81
81
|
desc "Run Tests against mongoid 2 & 3 & 4, 5"
|
82
|
-
task :
|
82
|
+
task mongoid: [:mongoid2, :mongoid3, :mongoid4, :mongoid5]
|
83
83
|
|
84
84
|
desc "Run Tests against rails 3 & 4 & 4.1 & 4.2 & 5.0"
|
85
|
-
task :
|
85
|
+
task rails: [:rails3, :rails4, :rails41, :rails42, :rails50, :rails51]
|
86
86
|
|
87
87
|
desc "Run Tests against all ORMs"
|
88
|
-
task :
|
88
|
+
task all: [:rails, :mongoid]
|
89
89
|
|
90
90
|
end
|
91
91
|
|
@@ -47,15 +47,15 @@ MoneyRails.configure do |config|
|
|
47
47
|
#
|
48
48
|
# Example:
|
49
49
|
# config.register_currency = {
|
50
|
-
# :
|
51
|
-
# :
|
52
|
-
# :
|
53
|
-
# :
|
54
|
-
# :
|
55
|
-
# :
|
56
|
-
# :
|
57
|
-
# :
|
58
|
-
# :
|
50
|
+
# priority: 1,
|
51
|
+
# iso_code: "EU4",
|
52
|
+
# name: "Euro with subunit of 4 digits",
|
53
|
+
# symbol: "€",
|
54
|
+
# symbol_first: true,
|
55
|
+
# subunit: "Subcent",
|
56
|
+
# subunit_to_unit: 10000,
|
57
|
+
# thousands_separator: ".",
|
58
|
+
# decimal_mark: ","
|
59
59
|
# }
|
60
60
|
|
61
61
|
# Specify a rounding mode
|
@@ -78,9 +78,9 @@ MoneyRails.configure do |config|
|
|
78
78
|
# Example:
|
79
79
|
#
|
80
80
|
# config.default_format = {
|
81
|
-
# :
|
82
|
-
# :
|
83
|
-
# :
|
81
|
+
# no_cents_if_whole: nil,
|
82
|
+
# symbol: nil,
|
83
|
+
# sign_before_symbol: nil
|
84
84
|
# }
|
85
85
|
|
86
86
|
# Set default raise_error_on_money_parsing option
|
@@ -23,7 +23,7 @@ module MoneyRails
|
|
23
23
|
return if options[:allow_nil] && @raw_value.nil?
|
24
24
|
|
25
25
|
# Set this before we modify @raw_value below.
|
26
|
-
stringy = @raw_value.present? && !@raw_value.is_a?(Numeric)
|
26
|
+
stringy = @raw_value.present? && !@raw_value.is_a?(Numeric) && !@raw_value.is_a?(Money)
|
27
27
|
|
28
28
|
if stringy
|
29
29
|
# remove currency symbol
|
@@ -33,7 +33,7 @@ module MoneyRails
|
|
33
33
|
normalize_raw_value!
|
34
34
|
super(@record, @attr, @raw_value)
|
35
35
|
|
36
|
-
if stringy and
|
36
|
+
if stringy and record_does_not_have_error?
|
37
37
|
add_error if
|
38
38
|
value_has_too_many_decimal_points or
|
39
39
|
thousand_separator_after_decimal_mark or
|
@@ -43,6 +43,11 @@ module MoneyRails
|
|
43
43
|
|
44
44
|
private
|
45
45
|
|
46
|
+
def record_does_not_have_error?
|
47
|
+
return true unless @record.errors.has_key?(@attr)
|
48
|
+
!@record.errors.added?(@attr, :not_a_number)
|
49
|
+
end
|
50
|
+
|
46
51
|
def reset_memoized_variables!
|
47
52
|
[:currency, :decimal_mark, :thousands_separator, :symbol,
|
48
53
|
:abs_raw_value, :decimal_pieces, :pieces_array].each do |var_name|
|
@@ -78,10 +83,10 @@ module MoneyRails
|
|
78
83
|
attr_name = @record.class.human_attribute_name(@attr, default: attr_name)
|
79
84
|
|
80
85
|
@record.errors.add(@attr, :invalid_currency,
|
81
|
-
{ :
|
82
|
-
:
|
83
|
-
:
|
84
|
-
:
|
86
|
+
{ thousands: thousands_separator,
|
87
|
+
decimal: decimal_mark,
|
88
|
+
currency: abs_raw_value,
|
89
|
+
attribute: attr_name })
|
85
90
|
end
|
86
91
|
|
87
92
|
def decimal_pieces
|
@@ -84,33 +84,33 @@ module MoneyRails
|
|
84
84
|
# All the options which are available for Rails numericality
|
85
85
|
# validation, are also available for both types.
|
86
86
|
# E.g.
|
87
|
-
# monetize :price_in_a_range_cents, :
|
88
|
-
# :
|
89
|
-
# :
|
90
|
-
# :
|
87
|
+
# monetize :price_in_a_range_cents, allow_nil: true,
|
88
|
+
# subunit_numericality: {
|
89
|
+
# greater_than_or_equal_to: 0,
|
90
|
+
# less_than_or_equal_to: 10000,
|
91
91
|
# },
|
92
|
-
# :
|
93
|
-
# :
|
94
|
-
# :
|
95
|
-
# :
|
92
|
+
# numericality: {
|
93
|
+
# greater_than_or_equal_to: 0,
|
94
|
+
# less_than_or_equal_to: 100,
|
95
|
+
# message: "must be greater than zero and less than $100"
|
96
96
|
# }
|
97
97
|
#
|
98
98
|
# To disable validation entirely, use :disable_validation, E.g:
|
99
|
-
# monetize :price_in_a_range_cents, :
|
99
|
+
# monetize :price_in_a_range_cents, disable_validation: true
|
100
100
|
if (validation_enabled = MoneyRails.include_validations && !options[:disable_validation])
|
101
101
|
|
102
102
|
# This is a validation for the subunit
|
103
103
|
if (subunit_numericality = options.fetch(:subunit_numericality, true))
|
104
104
|
validates subunit_name, {
|
105
|
-
:
|
106
|
-
:
|
105
|
+
allow_nil: options[:allow_nil],
|
106
|
+
numericality: subunit_numericality
|
107
107
|
}
|
108
108
|
end
|
109
109
|
|
110
110
|
# Allow only Money objects or Numeric values!
|
111
111
|
if (numericality = options.fetch(:numericality, true))
|
112
112
|
validates name.to_sym, {
|
113
|
-
:
|
113
|
+
allow_nil: options[:allow_nil],
|
114
114
|
'money_rails/active_model/money' => numericality
|
115
115
|
}
|
116
116
|
end
|
@@ -59,10 +59,10 @@ module MoneyRails
|
|
59
59
|
# MoneyRails.configure do |config|
|
60
60
|
# config.default_bank = EuCentralBank.new
|
61
61
|
# end
|
62
|
-
delegate :default_bank=, :default_bank, :
|
62
|
+
delegate :default_bank=, :default_bank, to: :Money
|
63
63
|
|
64
64
|
# Provide exchange rates
|
65
|
-
delegate :add_rate, :
|
65
|
+
delegate :add_rate, to: :Money
|
66
66
|
|
67
67
|
# Use (by default) validation of numericality for each monetized field.
|
68
68
|
mattr_accessor :include_validations
|
@@ -2,18 +2,18 @@ module MoneyRails
|
|
2
2
|
module ActionViewExtension
|
3
3
|
|
4
4
|
def currency_symbol
|
5
|
-
content_tag(:span, Money.default_currency.symbol, :
|
5
|
+
content_tag(:span, Money.default_currency.symbol, class: "currency_symbol")
|
6
6
|
end
|
7
7
|
|
8
8
|
def humanized_money(value, options={})
|
9
9
|
if !options || !options.is_a?(Hash)
|
10
|
-
warn "humanized_money now takes a hash of formatting options, please specify { :
|
11
|
-
options = { :
|
10
|
+
warn "humanized_money now takes a hash of formatting options, please specify { symbol: true }"
|
11
|
+
options = { symbol: options }
|
12
12
|
end
|
13
13
|
|
14
14
|
options = {
|
15
|
-
:
|
16
|
-
:
|
15
|
+
no_cents_if_whole: MoneyRails::Configuration.no_cents_if_whole.nil? ? true : MoneyRails::Configuration.no_cents_if_whole,
|
16
|
+
symbol: false
|
17
17
|
}.merge(options)
|
18
18
|
options.delete(:symbol) if options[:disambiguate]
|
19
19
|
|
@@ -27,26 +27,26 @@ module MoneyRails
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def humanized_money_with_symbol(value, options={})
|
30
|
-
humanized_money(value, options.merge(:
|
30
|
+
humanized_money(value, options.merge(symbol: true))
|
31
31
|
end
|
32
32
|
|
33
33
|
def money_without_cents(value, options={})
|
34
34
|
if !options || !options.is_a?(Hash)
|
35
|
-
warn "money_without_cents now takes a hash of formatting options, please specify { :
|
36
|
-
options = { :
|
35
|
+
warn "money_without_cents now takes a hash of formatting options, please specify { symbol: true }"
|
36
|
+
options = { symbol: options }
|
37
37
|
end
|
38
38
|
|
39
39
|
options = {
|
40
|
-
:
|
41
|
-
:
|
42
|
-
:
|
40
|
+
no_cents: true,
|
41
|
+
no_cents_if_whole: false,
|
42
|
+
symbol: false
|
43
43
|
}.merge(options)
|
44
44
|
|
45
45
|
humanized_money(value, options)
|
46
46
|
end
|
47
47
|
|
48
48
|
def money_without_cents_and_with_symbol(value)
|
49
|
-
money_without_cents(value, :
|
49
|
+
money_without_cents(value, symbol: true)
|
50
50
|
end
|
51
51
|
end
|
52
52
|
end
|
data/lib/money-rails/money.rb
CHANGED
@@ -2,25 +2,23 @@ require "active_support/core_ext/module/aliasing.rb"
|
|
2
2
|
require "active_support/core_ext/hash/reverse_merge.rb"
|
3
3
|
|
4
4
|
class Money
|
5
|
-
|
5
|
+
class <<self
|
6
|
+
alias_method :orig_default_formatting_rules, :default_formatting_rules
|
6
7
|
|
7
|
-
|
8
|
-
|
8
|
+
def default_formatting_rules
|
9
|
+
rules = orig_default_formatting_rules || {}
|
10
|
+
defaults = {
|
11
|
+
no_cents_if_whole: MoneyRails::Configuration.no_cents_if_whole,
|
12
|
+
symbol: MoneyRails::Configuration.symbol,
|
13
|
+
sign_before_symbol: MoneyRails::Configuration.sign_before_symbol
|
14
|
+
}.reject { |_k, v| v.nil? }
|
9
15
|
|
10
|
-
|
11
|
-
defaults = {
|
12
|
-
no_cents_if_whole: MoneyRails::Configuration.no_cents_if_whole,
|
13
|
-
symbol: MoneyRails::Configuration.symbol,
|
14
|
-
sign_before_symbol: MoneyRails::Configuration.sign_before_symbol
|
15
|
-
}.reject { |_k, v| v.nil? }
|
16
|
+
rules.reverse_merge!(defaults)
|
16
17
|
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
rules
|
18
|
+
unless MoneyRails::Configuration.default_format.nil?
|
19
|
+
rules.reverse_merge!(MoneyRails::Configuration.default_format)
|
20
|
+
end
|
21
|
+
rules
|
21
22
|
end
|
22
|
-
|
23
|
-
orig_format(rules)
|
24
23
|
end
|
25
|
-
|
26
24
|
end
|
@@ -3,8 +3,8 @@ class Money
|
|
3
3
|
# Converts an object of this instance into a database friendly value.
|
4
4
|
def mongoize
|
5
5
|
{
|
6
|
-
:
|
7
|
-
:
|
6
|
+
cents: cents.mongoize.to_f,
|
7
|
+
currency_iso: currency.iso_code.mongoize
|
8
8
|
}
|
9
9
|
end
|
10
10
|
|