money-rails 1.4.1 → 1.5.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 +28 -1
- data/README.md +11 -13
- data/Rakefile +1 -1
- data/config/locales/money.en.yml +1 -1
- data/lib/generators/templates/money.rb +7 -0
- data/lib/money-rails/active_model/validator.rb +36 -22
- data/lib/money-rails/active_record/migration_extensions/options_extractor.rb +1 -1
- data/lib/money-rails/active_record/migration_extensions/schema_statements_pg_rails4.rb +1 -1
- data/lib/money-rails/active_record/migration_extensions/table_pg_rails4.rb +1 -1
- data/lib/money-rails/active_record/monetizable.rb +30 -14
- data/lib/money-rails/configuration.rb +4 -0
- data/lib/money-rails/money.rb +4 -5
- data/lib/money-rails/version.rb +1 -1
- data/money-rails.gemspec +3 -3
- data/spec/active_record/monetizable_spec.rb +143 -24
- data/spec/configuration_spec.rb +2 -2
- data/spec/dummy/app/models/product.rb +3 -3
- data/spec/dummy/app/models/transaction.rb +2 -0
- data/spec/dummy/config/boot.rb +1 -1
- data/spec/dummy/config/environments/production.rb +1 -1
- data/spec/dummy/config/environments/test.rb +1 -1
- data/spec/dummy/config/locales/it.yml +11 -0
- data/spec/dummy/db/migrate/20151026220420_add_optional_amount_to_transactions.rb +5 -0
- data/spec/dummy/db/schema.rb +2 -1
- data/spec/mongoid/four_spec.rb +7 -5
- data/spec/mongoid/three_spec.rb +6 -5
- data/spec/mongoid/two_spec.rb +5 -4
- data/spec/test_helpers_spec.rb +4 -4
- metadata +25 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 9cdd1a96659e3e98f1e9b94ec3663d0a8aed3ebf
|
4
|
+
data.tar.gz: 24871846cddf01fcd19d237e0d5e76de2cf1ffc8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f7eb9f737b89fb5f8b04bd6658b856c54863aee31bf745972de8b46d3b5b3be9971389eae9c324e94ac015610f2ebee0a9e507e4e458307472cf1842941d4680
|
7
|
+
data.tar.gz: 80ef14207ac392151dec41179a975e5e14218a0fc7b3a2ddcacbe848afcc8df1592b7478b6a7454ad9ccbaf057e21c8b4b83d8cf59dbe4b0ffae0f6b96d80748
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,33 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
-
##
|
3
|
+
## 1.5.0
|
4
|
+
|
5
|
+
- Respect Money.use_i18n when validating.
|
6
|
+
- Include attribute in validation messages like Rails does.
|
7
|
+
- Respect `raise_error_on_money_parsing` before raising a MoneyRails::ActiveRecord::Monetizable::ReadOnlyCurrencyException.
|
8
|
+
|
9
|
+
## 1.4.1
|
10
|
+
|
11
|
+
- validator was looking for monetizable_attributes using a symbol key, when the keys are all strings. Asserted string key values in rspec and changed validator to look for a string key.
|
12
|
+
- make monetized_attribute hash comparison order independent
|
13
|
+
- Isolate class used for the monetized_attributes tests to prevent cross-contamination
|
14
|
+
- add gem tasks
|
15
|
+
|
16
|
+
## 1.4.0
|
17
|
+
|
18
|
+
- Fix validation failing when both superclass and subclass use monetize macros and any of them validates any field
|
19
|
+
- Extract db adapter without open connection on load
|
20
|
+
- Add support for field currency values to be determined by lambda.
|
21
|
+
- Simplify validation options
|
22
|
+
- Test for skipping validations separately from each other
|
23
|
+
- Instead of requiring either the PG version or the non, always require the PG version and only fail to require the non when using PG, that way monetize will always work and money is supported for backwards compat. This way you can have a system with sqlite for dev and pg for production, for instance, and things still work.
|
24
|
+
- Refactor monetized_attributes
|
25
|
+
- updating db/schema.rb
|
26
|
+
- DRYing migration extensions
|
27
|
+
- Testing against latest ruby version
|
28
|
+
- Include postgres specific code also when adaptor = postgis.
|
29
|
+
- chore(add read only exception class)
|
30
|
+
- tiny schema change
|
4
31
|
|
5
32
|
## 1.3.0
|
6
33
|
|
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# RubyMoney - Money-Rails
|
1
|
+
# RubyMoney - Money-Rails
|
2
2
|
|
3
3
|
[![Gem Version](https://badge.fury.io/rb/money-rails.svg)](http://badge.fury.io/rb/money-rails)
|
4
4
|
[![Build Status](https://secure.travis-ci.org/RubyMoney/money-rails.svg?branch=master)](http://travis-ci.org/RubyMoney/money-rails)
|
@@ -268,7 +268,7 @@ end
|
|
268
268
|
Now ```product.discount``` and ```product.bonus``` will return a `Money`
|
269
269
|
object using EUR as their currency, instead of the default USD.
|
270
270
|
|
271
|
-
(This is not
|
271
|
+
(This is not available in Mongoid).
|
272
272
|
|
273
273
|
#### Attribute Currency (:with_currency)
|
274
274
|
|
@@ -299,8 +299,8 @@ currency, whereas ```product.discount.currency_as_string # => EUR ```
|
|
299
299
|
#### Instance Currencies
|
300
300
|
|
301
301
|
All the previous options do not require any extra model fields to hold
|
302
|
-
the currency values. If the currency of a field
|
303
|
-
|
302
|
+
the currency values. If the currency of a field will vary from
|
303
|
+
one model instance to another, then you should add a column called ```currency```
|
304
304
|
to your database table and pass the option ```with_model_currency```
|
305
305
|
to the ```monetize``` macro.
|
306
306
|
|
@@ -317,8 +317,8 @@ class Transaction < ActiveRecord::Base
|
|
317
317
|
# Use model level currency
|
318
318
|
register_currency :gbp
|
319
319
|
|
320
|
-
monetize :amount_cents, with_model_currency: :
|
321
|
-
monetize :tax_cents, with_model_currency: :
|
320
|
+
monetize :amount_cents, with_model_currency: :currency
|
321
|
+
monetize :tax_cents, with_model_currency: :currency
|
322
322
|
|
323
323
|
end
|
324
324
|
|
@@ -456,30 +456,28 @@ So `humanized_money` will ignore `config.default_format = { no_cents_if_whole: f
|
|
456
456
|
### Testing
|
457
457
|
|
458
458
|
If you use Rspec there is an test helper implementation.
|
459
|
-
Just write `require "money-rails/test_helpers"` in spec_helper.rb
|
460
|
-
`include MoneyRails::TestHelpers` inside a describe block you want to
|
461
|
-
use the helper.
|
459
|
+
Just write `require "money-rails/test_helpers"` in spec_helper.rb.
|
462
460
|
|
463
461
|
* the `monetize` matcher
|
464
462
|
|
465
463
|
```ruby
|
466
|
-
monetize(:
|
464
|
+
is_expected.to monetize(:price)
|
467
465
|
```
|
468
466
|
This will ensure that a column called `price_cents` is being monetized.
|
469
467
|
|
470
468
|
```ruby
|
471
|
-
monetize(:
|
469
|
+
is_expected.to monetize(:price).allow_nil
|
472
470
|
```
|
473
471
|
By using `allow_nil` you can specify money attributes that accept nil values.
|
474
472
|
|
475
473
|
```ruby
|
476
|
-
monetize(:
|
474
|
+
is_expected.to monetize(:price).as(:discount_value)
|
477
475
|
```
|
478
476
|
By using `as` chain you can specify the exact name to which a monetized
|
479
477
|
column is being mapped.
|
480
478
|
|
481
479
|
```ruby
|
482
|
-
monetize(:
|
480
|
+
is_expected.to monetize(:price).with_currency(:gbp)
|
483
481
|
```
|
484
482
|
|
485
483
|
By using the `with_currency` chain you can specify the expected currency
|
data/Rakefile
CHANGED
@@ -14,7 +14,7 @@ end
|
|
14
14
|
|
15
15
|
APP_RAKEFILE = File.expand_path("../spec/dummy/Rakefile", __FILE__)
|
16
16
|
|
17
|
-
load 'rails/tasks/engine.rake' if File.
|
17
|
+
load 'rails/tasks/engine.rake' if File.exist?(APP_RAKEFILE)
|
18
18
|
|
19
19
|
require 'rake'
|
20
20
|
require 'rspec/core/rake_task'
|
data/config/locales/money.en.yml
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
en:
|
2
2
|
errors:
|
3
3
|
messages:
|
4
|
-
invalid_currency:
|
4
|
+
invalid_currency: must be a valid currency (eg. '100', '5%{decimal}24', or '123%{thousands}456%{decimal}78'). Got %{currency}
|
5
5
|
|
@@ -67,4 +67,11 @@ MoneyRails.configure do |config|
|
|
67
67
|
# :symbol => nil,
|
68
68
|
# :sign_before_symbol => nil
|
69
69
|
# }
|
70
|
+
|
71
|
+
# Set default raise_error_on_money_parsing option
|
72
|
+
# It will be raise error if assigned different currency
|
73
|
+
# The default value is false
|
74
|
+
#
|
75
|
+
# Example:
|
76
|
+
# config.raise_error_on_money_parsing = false
|
70
77
|
end
|
@@ -1,7 +1,7 @@
|
|
1
1
|
module MoneyRails
|
2
2
|
module ActiveModel
|
3
3
|
class MoneyValidator < ::ActiveModel::Validations::NumericalityValidator
|
4
|
-
def validate_each(record, attr,
|
4
|
+
def validate_each(record, attr, _value)
|
5
5
|
reset_memoized_variables!
|
6
6
|
@record = record
|
7
7
|
@attr = attr
|
@@ -22,19 +22,23 @@ module MoneyRails
|
|
22
22
|
|
23
23
|
return if options[:allow_nil] && @raw_value.nil?
|
24
24
|
|
25
|
-
#
|
26
|
-
|
27
|
-
if raw_value_is_non_numeric
|
28
|
-
# remove currency symbol, and negative sign
|
29
|
-
@raw_value = @raw_value.to_s.strip.gsub(symbol, "")
|
30
|
-
|
31
|
-
add_error and return if value_has_too_many_decimal_points
|
32
|
-
add_error if invalid_thousands_separation
|
25
|
+
# Set this before we modify @raw_value below.
|
26
|
+
stringy = @raw_value.present? && !@raw_value.is_a?(Numeric)
|
33
27
|
|
28
|
+
if stringy
|
29
|
+
# remove currency symbol
|
30
|
+
@raw_value = @raw_value.to_s.gsub(symbol, "")
|
34
31
|
end
|
35
32
|
|
36
33
|
normalize_raw_value!
|
37
34
|
super(@record, @attr, @raw_value)
|
35
|
+
|
36
|
+
if stringy and not @record.errors.added?(@attr, :not_a_number)
|
37
|
+
add_error if
|
38
|
+
value_has_too_many_decimal_points or
|
39
|
+
thousand_separator_after_decimal_mark or
|
40
|
+
invalid_thousands_separation
|
41
|
+
end
|
38
42
|
end
|
39
43
|
|
40
44
|
private
|
@@ -43,7 +47,7 @@ module MoneyRails
|
|
43
47
|
[:currency, :decimal_mark, :thousands_separator, :symbol,
|
44
48
|
:abs_raw_value, :decimal_pieces, :pieces_array].each do |var_name|
|
45
49
|
ivar_name = :"@_#{var_name}"
|
46
|
-
remove_instance_variable(ivar_name) if
|
50
|
+
remove_instance_variable(ivar_name) if instance_variable_defined?(ivar_name)
|
47
51
|
end
|
48
52
|
end
|
49
53
|
|
@@ -52,30 +56,32 @@ module MoneyRails
|
|
52
56
|
end
|
53
57
|
|
54
58
|
def decimal_mark
|
55
|
-
|
59
|
+
character = currency.decimal_mark || '.'
|
60
|
+
@_decimal_mark ||= Money.use_i18n ? I18n.t('number.currency.format.separator', default: character) : character
|
56
61
|
end
|
57
62
|
|
58
63
|
def thousands_separator
|
59
|
-
|
64
|
+
character = currency.thousands_separator || ','
|
65
|
+
@_thousands_separator ||= Money.use_i18n ? I18n.t('number.currency.format.delimiter', default: character) : character
|
60
66
|
end
|
61
67
|
|
62
68
|
def symbol
|
63
|
-
@_symbol ||= I18n.t('number.currency.format.unit', default: currency.symbol)
|
64
|
-
end
|
65
|
-
|
66
|
-
def raw_value_is_non_numeric
|
67
|
-
@raw_value.present? && !@raw_value.is_a?(Numeric)
|
69
|
+
@_symbol ||= Money.use_i18n ? I18n.t('number.currency.format.unit', default: currency.symbol) : currency.symbol
|
68
70
|
end
|
69
71
|
|
70
72
|
def abs_raw_value
|
71
|
-
@_abs_raw_value ||= @raw_value.
|
73
|
+
@_abs_raw_value ||= @raw_value.to_s.sub(/^\s*-/, "").strip
|
72
74
|
end
|
73
75
|
|
74
76
|
def add_error
|
75
|
-
@
|
76
|
-
|
77
|
-
|
78
|
-
|
77
|
+
attr_name = @attr.to_s.tr('.', '_').humanize
|
78
|
+
attr_name = @record.class.human_attribute_name(@attr, default: attr_name)
|
79
|
+
|
80
|
+
@record.errors.add(@attr, :invalid_currency,
|
81
|
+
{ :thousands => thousands_separator,
|
82
|
+
:decimal => decimal_mark,
|
83
|
+
:currency => abs_raw_value,
|
84
|
+
:attribute => attr_name })
|
79
85
|
end
|
80
86
|
|
81
87
|
def decimal_pieces
|
@@ -90,6 +96,10 @@ module MoneyRails
|
|
90
96
|
@_pieces_array ||= decimal_pieces[0].split(thousands_separator.presence)
|
91
97
|
end
|
92
98
|
|
99
|
+
def thousand_separator_after_decimal_mark
|
100
|
+
thousands_separator.present? && decimal_pieces.length == 2 && decimal_pieces[1].include?(thousands_separator)
|
101
|
+
end
|
102
|
+
|
93
103
|
def invalid_thousands_separation
|
94
104
|
return false if pieces_array.length <= 1
|
95
105
|
return true if pieces_array[0].length > 3
|
@@ -101,6 +111,10 @@ module MoneyRails
|
|
101
111
|
# Remove thousands separators, normalize decimal mark,
|
102
112
|
# remove whitespaces and _ (E.g. 99 999 999 or 12_300_200.20)
|
103
113
|
def normalize_raw_value!
|
114
|
+
# Cache abs_raw_value before normalizing because it's used in
|
115
|
+
# many places and relies on the original @raw_value.
|
116
|
+
abs_raw_value
|
117
|
+
|
104
118
|
@raw_value = @raw_value.to_s
|
105
119
|
.gsub(thousands_separator, '')
|
106
120
|
.gsub(decimal_mark, '.')
|
@@ -9,7 +9,7 @@ module MoneyRails
|
|
9
9
|
default[:table_name] = table_name
|
10
10
|
|
11
11
|
excluded_keys = [:amount, :currency, :type, :prefix, :postfix, :present, :column_name, :table_name]
|
12
|
-
default[:options] = default.except
|
12
|
+
default[:options] = default.except(*excluded_keys)
|
13
13
|
|
14
14
|
default.slice(:present, :table_name, :column_name, :type, :options).values
|
15
15
|
end
|
@@ -5,7 +5,7 @@ module MoneyRails
|
|
5
5
|
def add_monetize(table_name, accessor, options={})
|
6
6
|
[:amount, :currency].each do |attribute|
|
7
7
|
column_present, *opts = OptionsExtractor.extract attribute, table_name, accessor, options
|
8
|
-
add_column
|
8
|
+
add_column(*opts) if column_present
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
@@ -5,7 +5,7 @@ module MoneyRails
|
|
5
5
|
def monetize(accessor, options={})
|
6
6
|
[:amount, :currency].each do |attribute|
|
7
7
|
column_present, _, *opts = OptionsExtractor.extract attribute, :no_table, accessor, options
|
8
|
-
column
|
8
|
+
column(*opts) if column_present
|
9
9
|
end
|
10
10
|
end
|
11
11
|
|
@@ -80,15 +80,15 @@ module MoneyRails
|
|
80
80
|
# :numericality => {
|
81
81
|
# :greater_than_or_equal_to => 0,
|
82
82
|
# :less_than_or_equal_to => 100,
|
83
|
-
# :message => "
|
83
|
+
# :message => "must be greater than zero and less than $100"
|
84
84
|
# }
|
85
85
|
#
|
86
86
|
# To disable validation entirely, use :disable_validation, E.g:
|
87
87
|
# monetize :price_in_a_range_cents, :disable_validation => true
|
88
|
-
if validation_enabled = MoneyRails.include_validations && !options[:disable_validation]
|
88
|
+
if (validation_enabled = MoneyRails.include_validations && !options[:disable_validation])
|
89
89
|
|
90
90
|
# This is a validation for the subunit
|
91
|
-
if subunit_numericality = options.fetch(:subunit_numericality, true)
|
91
|
+
if (subunit_numericality = options.fetch(:subunit_numericality, true))
|
92
92
|
validates subunit_name, {
|
93
93
|
:allow_nil => options[:allow_nil],
|
94
94
|
:numericality => subunit_numericality
|
@@ -96,7 +96,7 @@ module MoneyRails
|
|
96
96
|
end
|
97
97
|
|
98
98
|
# Allow only Money objects or Numeric values!
|
99
|
-
if numericality = options.fetch(:numericality, true)
|
99
|
+
if (numericality = options.fetch(:numericality, true))
|
100
100
|
validates name.to_sym, {
|
101
101
|
:allow_nil => options[:allow_nil],
|
102
102
|
'money_rails/active_model/money' => numericality
|
@@ -117,14 +117,26 @@ module MoneyRails
|
|
117
117
|
memoized = instance_variable_get("@#{name}")
|
118
118
|
|
119
119
|
# Dont create a new Money instance if the values haven't been changed.
|
120
|
-
|
120
|
+
if memoized && memoized.cents == amount &&
|
121
121
|
memoized.currency == attr_currency
|
122
|
+
result = memoized
|
123
|
+
else
|
124
|
+
# If amount is NOT nil (or empty string) load the amount in a Money
|
125
|
+
amount = Money.new(amount, attr_currency) unless amount.blank?
|
126
|
+
|
127
|
+
# Cache the value (it may be nil)
|
128
|
+
result = instance_variable_set "@#{name}", amount
|
129
|
+
end
|
122
130
|
|
123
|
-
|
124
|
-
|
131
|
+
if MoneyRails::Configuration.preserve_user_input
|
132
|
+
value_before_type_cast = instance_variable_get "@#{name}_money_before_type_cast"
|
133
|
+
unless errors[name.to_sym].blank?
|
134
|
+
result.define_singleton_method(:to_s) { value_before_type_cast }
|
135
|
+
result.define_singleton_method(:format) { |_| value_before_type_cast }
|
136
|
+
end
|
137
|
+
end
|
125
138
|
|
126
|
-
|
127
|
-
instance_variable_set "@#{name}", amount
|
139
|
+
result
|
128
140
|
end
|
129
141
|
|
130
142
|
define_method "#{name}=" do |value|
|
@@ -173,12 +185,13 @@ module MoneyRails
|
|
173
185
|
money_currency = money.try(:currency)
|
174
186
|
|
175
187
|
# Update currency iso value if there is an instance currency attribute
|
176
|
-
if instance_currency_name.present? && respond_to?("#{instance_currency_name}=")
|
188
|
+
if instance_currency_name.present? && respond_to?("#{instance_currency_name}=") && money_currency
|
177
189
|
public_send("#{instance_currency_name}=", money_currency.try(:iso_code))
|
178
190
|
else
|
179
191
|
current_currency = public_send("currency_for_#{name}")
|
180
192
|
if money_currency && current_currency != money_currency.id
|
181
|
-
raise ReadOnlyCurrencyException.new("Can't change readonly currency '#{current_currency}' to '#{money_currency}' for field '#{name}'")
|
193
|
+
raise ReadOnlyCurrencyException.new("Can't change readonly currency '#{current_currency}' to '#{money_currency}' for field '#{name}'") if MoneyRails.raise_error_on_money_parsing
|
194
|
+
return nil
|
182
195
|
end
|
183
196
|
end
|
184
197
|
|
@@ -196,7 +209,9 @@ module MoneyRails
|
|
196
209
|
end
|
197
210
|
|
198
211
|
define_method "currency_for_#{name}" do
|
199
|
-
|
212
|
+
if MoneyRails::Configuration.currency_column[:postfix].present?
|
213
|
+
instance_currency_name_with_postfix = "#{name}#{MoneyRails::Configuration.currency_column[:postfix]}"
|
214
|
+
end
|
200
215
|
|
201
216
|
if instance_currency_name.present? && respond_to?(instance_currency_name) &&
|
202
217
|
Money::Currency.find(public_send(instance_currency_name))
|
@@ -206,8 +221,9 @@ module MoneyRails
|
|
206
221
|
Money::Currency.find(field_currency_name.call(self))
|
207
222
|
elsif field_currency_name
|
208
223
|
Money::Currency.find(field_currency_name)
|
209
|
-
elsif
|
210
|
-
|
224
|
+
elsif instance_currency_name_with_postfix.present? &&
|
225
|
+
respond_to?(instance_currency_name_with_postfix) &&
|
226
|
+
Money::Currency.find(public_send(instance_currency_name_with_postfix))
|
211
227
|
|
212
228
|
Money::Currency.find(public_send(instance_currency_name_with_postfix))
|
213
229
|
elsif self.class.respond_to?(:currency)
|
@@ -97,5 +97,9 @@ module MoneyRails
|
|
97
97
|
# is going to be converted to a Money object.
|
98
98
|
mattr_accessor :raise_error_on_money_parsing
|
99
99
|
@@raise_error_on_money_parsing = false
|
100
|
+
|
101
|
+
#Configure whether to maintain invalid user input after validations
|
102
|
+
mattr_accessor :preserve_user_input
|
103
|
+
@@preserve_user_input = false
|
100
104
|
end
|
101
105
|
end
|
data/lib/money-rails/money.rb
CHANGED
@@ -2,8 +2,9 @@ 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
|
+
alias_method :orig_format, :format
|
5
6
|
|
6
|
-
def
|
7
|
+
def format(*rules)
|
7
8
|
rules = normalize_formatting_rules(rules)
|
8
9
|
|
9
10
|
# Apply global defaults for money only for non-nil values
|
@@ -11,7 +12,7 @@ class Money
|
|
11
12
|
no_cents_if_whole: MoneyRails::Configuration.no_cents_if_whole,
|
12
13
|
symbol: MoneyRails::Configuration.symbol,
|
13
14
|
sign_before_symbol: MoneyRails::Configuration.sign_before_symbol
|
14
|
-
}.reject { |
|
15
|
+
}.reject { |_k, v| v.nil? }
|
15
16
|
|
16
17
|
rules.reverse_merge!(defaults)
|
17
18
|
|
@@ -19,9 +20,7 @@ class Money
|
|
19
20
|
rules.reverse_merge!(MoneyRails::Configuration.default_format)
|
20
21
|
end
|
21
22
|
|
22
|
-
|
23
|
+
orig_format(rules)
|
23
24
|
end
|
24
25
|
|
25
|
-
alias_method_chain :format, :settings
|
26
|
-
|
27
26
|
end
|
data/lib/money-rails/version.rb
CHANGED
data/money-rails.gemspec
CHANGED
@@ -26,13 +26,13 @@ Gem::Specification.new do |s|
|
|
26
26
|
|
27
27
|
s.require_path = "lib"
|
28
28
|
|
29
|
-
s.add_dependency "money", "~> 6.
|
30
|
-
s.add_dependency "monetize", "~> 1.
|
29
|
+
s.add_dependency "money", "~> 6.6"
|
30
|
+
s.add_dependency "monetize", "~> 1.3.0"
|
31
31
|
s.add_dependency "activesupport", ">= 3.0"
|
32
32
|
s.add_dependency "railties", ">= 3.0"
|
33
33
|
|
34
34
|
s.add_development_dependency "rails", ">= 3.0"
|
35
35
|
s.add_development_dependency "rspec-rails", "~> 3.0"
|
36
36
|
s.add_development_dependency 'database_cleaner', ['>= 0.8.0', '< 1.4.0']
|
37
|
-
s.add_development_dependency 'test-unit', '~> 3.0' if RUBY_VERSION
|
37
|
+
s.add_development_dependency 'test-unit', '~> 3.0' if RUBY_VERSION >= '2.2'
|
38
38
|
end
|
@@ -1,3 +1,5 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
1
3
|
require 'spec_helper'
|
2
4
|
|
3
5
|
class Sub < Product; end
|
@@ -75,19 +77,12 @@ if defined? ActiveRecord
|
|
75
77
|
expect(product.price_cents).to eq(215)
|
76
78
|
end
|
77
79
|
|
78
|
-
it "should raise error if can't change currency" do
|
79
|
-
product = Product.new
|
80
|
-
expect {
|
81
|
-
product.price = Money.new(10, "RUB")
|
82
|
-
}.to raise_error(MoneyRails::ActiveRecord::Monetizable::ReadOnlyCurrencyException, "Can't change readonly currency 'USD' to 'RUB' for field 'price'")
|
83
|
-
end
|
84
|
-
|
85
80
|
it "raises an error if trying to create two attributes with the same name" do
|
86
81
|
expect do
|
87
82
|
class Product
|
88
83
|
monetize :discount, as: :price
|
89
84
|
end
|
90
|
-
end.to raise_error
|
85
|
+
end.to raise_error ArgumentError
|
91
86
|
end
|
92
87
|
|
93
88
|
it "allows subclass to redefine attribute with the same name" do
|
@@ -130,7 +125,13 @@ if defined? ActiveRecord
|
|
130
125
|
after { MoneyRails.raise_error_on_money_parsing = false }
|
131
126
|
|
132
127
|
it "raises exception when a String value with hyphen is assigned" do
|
133
|
-
expect { product.accessor_price = "10-235" }.to raise_error
|
128
|
+
expect { product.accessor_price = "10-235" }.to raise_error ArgumentError
|
129
|
+
end
|
130
|
+
|
131
|
+
it "raises an exception if it can't change currency" do
|
132
|
+
expect {
|
133
|
+
Product.new.price = Money.new(10, "RUB")
|
134
|
+
}.to raise_error(MoneyRails::ActiveRecord::Monetizable::ReadOnlyCurrencyException, "Can't change readonly currency 'USD' to 'RUB' for field 'price'")
|
134
135
|
end
|
135
136
|
end
|
136
137
|
|
@@ -138,6 +139,10 @@ if defined? ActiveRecord
|
|
138
139
|
it "does not raise exception when a String value with hyphen is assigned" do
|
139
140
|
expect { product.accessor_price = "10-235" }.not_to raise_error
|
140
141
|
end
|
142
|
+
|
143
|
+
it "does not raise exception if it can't change currency" do
|
144
|
+
expect { Product.new.price = Money.new(10, "RUB") }.not_to raise_error
|
145
|
+
end
|
141
146
|
end
|
142
147
|
|
143
148
|
it "respects numericality validation when using update_attributes" do
|
@@ -170,24 +175,33 @@ if defined? ActiveRecord
|
|
170
175
|
it "fails validation with the proper error message if money value is invalid decimal" do
|
171
176
|
product.price = "12.23.24"
|
172
177
|
expect(product.save).to be_falsey
|
173
|
-
expect(product.errors[:price].
|
174
|
-
expect(product.errors[:price].first).to match(/
|
178
|
+
expect(product.errors[:price].size).to eq(1)
|
179
|
+
expect(product.errors[:price].first).to match(/not a number/)
|
175
180
|
end
|
176
181
|
|
177
182
|
it "fails validation with the proper error message if money value is nothing but periods" do
|
178
183
|
product.price = "..."
|
179
184
|
expect(product.save).to be_falsey
|
180
|
-
expect(product.errors[:price].
|
181
|
-
expect(product.errors[:price].first).to match(/
|
185
|
+
expect(product.errors[:price].size).to eq(1)
|
186
|
+
expect(product.errors[:price].first).to match(/not a number/)
|
182
187
|
end
|
183
188
|
|
184
189
|
it "fails validation with the proper error message if money value has invalid thousands part" do
|
185
190
|
product.price = "12,23.24"
|
186
191
|
expect(product.save).to be_falsey
|
187
|
-
expect(product.errors[:price].
|
192
|
+
expect(product.errors[:price].size).to eq(1)
|
193
|
+
expect(product.errors[:price].first).to match(/must be a valid/)
|
188
194
|
expect(product.errors[:price].first).to match(/Got 12,23.24/)
|
189
195
|
end
|
190
196
|
|
197
|
+
it "fails validation with the proper error message if money value has thousand char after decimal mark" do
|
198
|
+
product.price = "1.234,56"
|
199
|
+
expect(product.save).to be_falsey
|
200
|
+
expect(product.errors[:price].size).to eq(1)
|
201
|
+
expect(product.errors[:price].first).to match(/must be a valid/)
|
202
|
+
expect(product.errors[:price].first).to match(/Got 1.234,56/)
|
203
|
+
end
|
204
|
+
|
191
205
|
it "allows an empty string as the thousands separator" do
|
192
206
|
begin
|
193
207
|
I18n.locale = 'en-US'
|
@@ -217,15 +231,18 @@ if defined? ActiveRecord
|
|
217
231
|
it "fails validation with the proper error message using numericality validations" do
|
218
232
|
product.price_in_a_range = "-12"
|
219
233
|
expect(product.valid?).to be_falsey
|
220
|
-
expect(product.errors[:price_in_a_range].
|
234
|
+
expect(product.errors[:price_in_a_range].size).to eq(1)
|
235
|
+
expect(product.errors[:price_in_a_range].first).to match(/must be greater than zero and less than \$100/)
|
221
236
|
|
222
237
|
product.price_in_a_range = Money.new(-1200, "USD")
|
223
238
|
expect(product.valid?).to be_falsey
|
224
|
-
expect(product.errors[:price_in_a_range].
|
239
|
+
expect(product.errors[:price_in_a_range].size).to eq(1)
|
240
|
+
expect(product.errors[:price_in_a_range].first).to match(/must be greater than zero and less than \$100/)
|
225
241
|
|
226
242
|
product.price_in_a_range = "0"
|
227
243
|
expect(product.valid?).to be_falsey
|
228
|
-
expect(product.errors[:price_in_a_range].
|
244
|
+
expect(product.errors[:price_in_a_range].size).to eq(1)
|
245
|
+
expect(product.errors[:price_in_a_range].first).to match(/must be greater than zero and less than \$100/)
|
229
246
|
|
230
247
|
product.price_in_a_range = "12"
|
231
248
|
expect(product.valid?).to be_truthy
|
@@ -235,11 +252,13 @@ if defined? ActiveRecord
|
|
235
252
|
|
236
253
|
product.price_in_a_range = "101"
|
237
254
|
expect(product.valid?).to be_falsey
|
238
|
-
expect(product.errors[:price_in_a_range].
|
255
|
+
expect(product.errors[:price_in_a_range].size).to eq(1)
|
256
|
+
expect(product.errors[:price_in_a_range].first).to match(/must be greater than zero and less than \$100/)
|
239
257
|
|
240
258
|
product.price_in_a_range = Money.new(10100, "USD")
|
241
259
|
expect(product.valid?).to be_falsey
|
242
|
-
expect(product.errors[:price_in_a_range].
|
260
|
+
expect(product.errors[:price_in_a_range].size).to eq(1)
|
261
|
+
expect(product.errors[:price_in_a_range].first).to match(/must be greater than zero and less than \$100/)
|
243
262
|
end
|
244
263
|
|
245
264
|
it "fails validation if linked attribute changed" do
|
@@ -254,15 +273,18 @@ if defined? ActiveRecord
|
|
254
273
|
it "fails validation with the proper error message using validates :money" do
|
255
274
|
product.validates_method_amount = "-12"
|
256
275
|
expect(product.valid?).to be_falsey
|
257
|
-
expect(product.errors[:validates_method_amount].
|
276
|
+
expect(product.errors[:validates_method_amount].size).to eq(1)
|
277
|
+
expect(product.errors[:validates_method_amount].first).to match(/must be greater than zero and less than \$100/)
|
258
278
|
|
259
279
|
product.validates_method_amount = Money.new(-1200, "USD")
|
260
280
|
expect(product.valid?).to be_falsey
|
261
|
-
expect(product.errors[:validates_method_amount].
|
281
|
+
expect(product.errors[:validates_method_amount].size).to eq(1)
|
282
|
+
expect(product.errors[:validates_method_amount].first).to match(/must be greater than zero and less than \$100/)
|
262
283
|
|
263
284
|
product.validates_method_amount = "0"
|
264
285
|
expect(product.valid?).to be_falsey
|
265
|
-
expect(product.errors[:validates_method_amount].
|
286
|
+
expect(product.errors[:validates_method_amount].size).to eq(1)
|
287
|
+
expect(product.errors[:validates_method_amount].first).to match(/must be greater than zero and less than \$100/)
|
266
288
|
|
267
289
|
product.validates_method_amount = "12"
|
268
290
|
expect(product.valid?).to be_truthy
|
@@ -272,20 +294,24 @@ if defined? ActiveRecord
|
|
272
294
|
|
273
295
|
product.validates_method_amount = "101"
|
274
296
|
expect(product.valid?).to be_falsey
|
275
|
-
expect(product.errors[:validates_method_amount].
|
297
|
+
expect(product.errors[:validates_method_amount].size).to eq(1)
|
298
|
+
expect(product.errors[:validates_method_amount].first).to match(/must be greater than zero and less than \$100/)
|
276
299
|
|
277
300
|
product.validates_method_amount = Money.new(10100, "USD")
|
278
301
|
expect(product.valid?).to be_falsey
|
279
|
-
expect(product.errors[:validates_method_amount].
|
302
|
+
expect(product.errors[:validates_method_amount].size).to eq(1)
|
303
|
+
expect(product.errors[:validates_method_amount].first).to match(/must be greater than zero and less than \$100/)
|
280
304
|
end
|
281
305
|
|
282
306
|
it "fails validation with the proper error message on the cents field " do
|
283
307
|
product.price_in_a_range = "-12"
|
284
308
|
expect(product.valid?).to be_falsey
|
309
|
+
expect(product.errors[:price_in_a_range_cents].size).to eq(1)
|
285
310
|
expect(product.errors[:price_in_a_range_cents].first).to match(/greater than 0/)
|
286
311
|
|
287
312
|
product.price_in_a_range = "0"
|
288
313
|
expect(product.valid?).to be_falsey
|
314
|
+
expect(product.errors[:price_in_a_range_cents].size).to eq(1)
|
289
315
|
expect(product.errors[:price_in_a_range_cents].first).to match(/greater than 0/)
|
290
316
|
|
291
317
|
product.price_in_a_range = "12"
|
@@ -293,24 +319,29 @@ if defined? ActiveRecord
|
|
293
319
|
|
294
320
|
product.price_in_a_range = "101"
|
295
321
|
expect(product.valid?).to be_falsey
|
322
|
+
expect(product.errors[:price_in_a_range_cents].size).to eq(1)
|
296
323
|
expect(product.errors[:price_in_a_range_cents].first).to match(/less than or equal to 10000/)
|
297
324
|
end
|
298
325
|
|
299
326
|
it "fails validation when a non number string is given" do
|
300
327
|
product = Product.create(:price_in_a_range => "asd")
|
301
328
|
expect(product.valid?).to be_falsey
|
329
|
+
expect(product.errors[:price_in_a_range].size).to eq(1)
|
302
330
|
expect(product.errors[:price_in_a_range].first).to match(/greater than zero/)
|
303
331
|
|
304
332
|
product = Product.create(:price_in_a_range => "asd23")
|
305
333
|
expect(product.valid?).to be_falsey
|
334
|
+
expect(product.errors[:price_in_a_range].size).to eq(1)
|
306
335
|
expect(product.errors[:price_in_a_range].first).to match(/greater than zero/)
|
307
336
|
|
308
337
|
product = Product.create(:price => "asd")
|
309
338
|
expect(product.valid?).to be_falsey
|
339
|
+
expect(product.errors[:price].size).to eq(1)
|
310
340
|
expect(product.errors[:price].first).to match(/is not a number/)
|
311
341
|
|
312
342
|
product = Product.create(:price => "asd23")
|
313
343
|
expect(product.valid?).to be_falsey
|
344
|
+
expect(product.errors[:price].size).to eq(1)
|
314
345
|
expect(product.errors[:price].first).to match(/is not a number/)
|
315
346
|
end
|
316
347
|
|
@@ -336,6 +367,21 @@ if defined? ActiveRecord
|
|
336
367
|
expect(product.save).to be_truthy
|
337
368
|
end
|
338
369
|
|
370
|
+
describe "with preserve_user_input set" do
|
371
|
+
around(:each) do |example|
|
372
|
+
old_value = MoneyRails::Configuration.preserve_user_input
|
373
|
+
MoneyRails::Configuration.preserve_user_input = true
|
374
|
+
example.run
|
375
|
+
MoneyRails::Configuration.preserve_user_input = false
|
376
|
+
end
|
377
|
+
|
378
|
+
it "preserves user input if validation fails" do
|
379
|
+
product.price = "14,0"
|
380
|
+
expect(product.save).to be_falsy
|
381
|
+
expect(product.price.to_s).to eq("14,0")
|
382
|
+
end
|
383
|
+
end
|
384
|
+
|
339
385
|
it "respects numericality validation when using update_attributes on money attribute" do
|
340
386
|
expect(product.update_attributes(:price => "some text")).to be_falsey
|
341
387
|
expect(product.update_attributes(:price => Money.new(320, 'USD'))).to be_truthy
|
@@ -639,6 +685,79 @@ if defined? ActiveRecord
|
|
639
685
|
expect(transaction.total).to eq(Money.new(3000, :usd))
|
640
686
|
end
|
641
687
|
|
688
|
+
it "allows currency column postfix to be blank" do
|
689
|
+
allow(MoneyRails::Configuration).to receive(:currency_column) { { postfix: nil, column_name: 'currency' } }
|
690
|
+
expect(dummy_product_with_nil_currency.price.currency).to eq(Money::Currency.find(:gbp))
|
691
|
+
end
|
692
|
+
|
693
|
+
context "and field with allow_nil: true" do
|
694
|
+
it "doesn't set currency to nil when setting the field to nil" do
|
695
|
+
t = Transaction.new(:amount_cents => 2500, :currency => "CAD")
|
696
|
+
t.optional_amount = nil
|
697
|
+
expect(t.currency).to eq("CAD")
|
698
|
+
end
|
699
|
+
end
|
700
|
+
|
701
|
+
context "and an Italian locale" do
|
702
|
+
around(:each) do |example|
|
703
|
+
I18n.with_locale(:it) do
|
704
|
+
example.run
|
705
|
+
end
|
706
|
+
end
|
707
|
+
|
708
|
+
context "when use_i18n is true" do
|
709
|
+
it "validates with the locale's decimal mark" do
|
710
|
+
transaction.amount = "123,45"
|
711
|
+
expect(transaction.valid?).to be_truthy
|
712
|
+
end
|
713
|
+
|
714
|
+
it "does not validate with the currency's decimal mark" do
|
715
|
+
transaction.amount = "123.45"
|
716
|
+
expect(transaction.valid?).to be_falsey
|
717
|
+
end
|
718
|
+
|
719
|
+
it "validates with the locale's currency symbol" do
|
720
|
+
transaction.amount = "€123"
|
721
|
+
expect(transaction.valid?).to be_truthy
|
722
|
+
end
|
723
|
+
|
724
|
+
it "does not validate with the transaction's currency symbol" do
|
725
|
+
transaction.amount = "$123.45"
|
726
|
+
expect(transaction.valid?).to be_falsey
|
727
|
+
end
|
728
|
+
end
|
729
|
+
|
730
|
+
context "when use_i18n is false" do
|
731
|
+
around(:each) do |example|
|
732
|
+
begin
|
733
|
+
Money.use_i18n = false
|
734
|
+
example.run
|
735
|
+
ensure
|
736
|
+
Money.use_i18n = true
|
737
|
+
end
|
738
|
+
end
|
739
|
+
|
740
|
+
it "does not validate with the locale's decimal mark" do
|
741
|
+
transaction.amount = "123,45"
|
742
|
+
expect(transaction.valid?).to be_falsey
|
743
|
+
end
|
744
|
+
|
745
|
+
it "validates with the currency's decimal mark" do
|
746
|
+
transaction.amount = "123.45"
|
747
|
+
expect(transaction.valid?).to be_truthy
|
748
|
+
end
|
749
|
+
|
750
|
+
it "does not validate with the locale's currency symbol" do
|
751
|
+
transaction.amount = "€123"
|
752
|
+
expect(transaction.valid?).to be_falsey
|
753
|
+
end
|
754
|
+
|
755
|
+
it "validates with the transaction's currency symbol" do
|
756
|
+
transaction.amount = "$123"
|
757
|
+
expect(transaction.valid?).to be_truthy
|
758
|
+
end
|
759
|
+
end
|
760
|
+
end
|
642
761
|
end
|
643
762
|
end
|
644
763
|
|
data/spec/configuration_spec.rb
CHANGED
@@ -13,8 +13,8 @@ describe "configuration" do
|
|
13
13
|
end
|
14
14
|
|
15
15
|
it "adds exchange rates given in config initializer" do
|
16
|
-
expect(Money.us_dollar(100).
|
17
|
-
expect(Money.ca_dollar(100).
|
16
|
+
expect(Money.us_dollar(100).bank.get_rate('USD', 'CAD')).to eq(1.24515)
|
17
|
+
expect(Money.ca_dollar(100).bank.get_rate('CAD', 'USD')).to eq(0.803115)
|
18
18
|
end
|
19
19
|
|
20
20
|
it "sets no_cents_if_whole value for formatted output globally" do
|
@@ -28,14 +28,14 @@ class Product < ActiveRecord::Base
|
|
28
28
|
:numericality => {
|
29
29
|
:greater_than => 0,
|
30
30
|
:less_than_or_equal_to => 100,
|
31
|
-
:message => "
|
31
|
+
:message => "must be greater than zero and less than $100"
|
32
32
|
}
|
33
33
|
|
34
34
|
# Skip validations separately from each other
|
35
35
|
monetize :skip_validation_price_cents, subunit_numericality: false, numericality: false, allow_nil: true
|
36
36
|
|
37
37
|
# Override default currency (EUR) with a specific one (CAD) for this field only, from a lambda
|
38
|
-
monetize :lambda_price_cents, with_currency: ->(
|
38
|
+
monetize :lambda_price_cents, with_currency: ->(_product) { Rails.configuration.lambda_test }, allow_nil: true
|
39
39
|
|
40
40
|
attr_accessor :accessor_price_cents
|
41
41
|
monetize :accessor_price_cents, disable_validation: true
|
@@ -45,7 +45,7 @@ class Product < ActiveRecord::Base
|
|
45
45
|
validates :validates_method_amount, :money => {
|
46
46
|
:greater_than => 0,
|
47
47
|
:less_than_or_equal_to => ->(product) { product.optional_price.to_f },
|
48
|
-
:message => '
|
48
|
+
:message => 'must be greater than zero and less than $100',
|
49
49
|
}, allow_nil: true
|
50
50
|
|
51
51
|
alias_attribute :renamed_cents, :aliased_cents
|
data/spec/dummy/config/boot.rb
CHANGED
@@ -8,6 +8,6 @@ unless ENV['BUNDLE_GEMFILE']
|
|
8
8
|
ENV['BUNDLE_GEMFILE'] ||= gemfile
|
9
9
|
end
|
10
10
|
|
11
|
-
require 'bundler/setup' if File.
|
11
|
+
require 'bundler/setup' if File.exist?(ENV['BUNDLE_GEMFILE'])
|
12
12
|
|
13
13
|
$:.unshift File.expand_path('../../../../lib', __FILE__)
|
@@ -15,7 +15,7 @@ Dummy::Application.configure do
|
|
15
15
|
config.action_controller.perform_caching = true
|
16
16
|
|
17
17
|
# Disable Rails's static asset server (Apache or nginx will already do this)
|
18
|
-
config.
|
18
|
+
config.serve_static_files = false
|
19
19
|
|
20
20
|
# Compress JavaScripts and CSS
|
21
21
|
config.assets.compress = true
|
@@ -13,7 +13,7 @@ Dummy::Application.configure do
|
|
13
13
|
config.eager_load = false
|
14
14
|
|
15
15
|
# Configure static asset server for tests with Cache-Control for performance
|
16
|
-
config.
|
16
|
+
config.serve_static_files = true
|
17
17
|
config.static_cache_control = "public, max-age=3600"
|
18
18
|
|
19
19
|
# Show full error reports and disable caching
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -11,7 +11,7 @@
|
|
11
11
|
#
|
12
12
|
# It's strongly recommended that you check this file into your version control system.
|
13
13
|
|
14
|
-
ActiveRecord::Schema.define(version:
|
14
|
+
ActiveRecord::Schema.define(version: 20151026220420) do
|
15
15
|
|
16
16
|
create_table "dummy_products", force: :cascade do |t|
|
17
17
|
t.string "currency"
|
@@ -54,6 +54,7 @@ ActiveRecord::Schema.define(version: 20150303222230) do
|
|
54
54
|
t.string "currency"
|
55
55
|
t.datetime "created_at"
|
56
56
|
t.datetime "updated_at"
|
57
|
+
t.integer "optional_amount_cents"
|
57
58
|
end
|
58
59
|
|
59
60
|
end
|
data/spec/mongoid/four_spec.rb
CHANGED
@@ -41,11 +41,11 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^4(.*)/
|
|
41
41
|
after { MoneyRails.raise_error_on_money_parsing = false }
|
42
42
|
|
43
43
|
it "raises exception if the mongoized value is a String with a hyphen" do
|
44
|
-
expect { priceable_from_string_with_hyphen }.to raise_error
|
44
|
+
expect { priceable_from_string_with_hyphen }.to raise_error ArgumentError
|
45
45
|
end
|
46
46
|
|
47
47
|
it "raises exception if the mongoized value is a String with an unknown currency" do
|
48
|
-
expect { priceable_from_string_with_unknown_currency }.to raise_error
|
48
|
+
expect { priceable_from_string_with_unknown_currency }.to raise_error ArgumentError
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
@@ -96,9 +96,11 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^4(.*)/
|
|
96
96
|
subject { Priceable.first.price }
|
97
97
|
it { is_expected.to be_an_instance_of(Money) }
|
98
98
|
it { is_expected.to eq(Money.new(100, 'EUR')) }
|
99
|
-
|
100
|
-
|
101
|
-
|
99
|
+
|
100
|
+
it "returns 0 cents in default_currency if a nil value was stored" do
|
101
|
+
nil_priceable = Priceable.create(price: nil)
|
102
|
+
expect(nil_priceable.price.cents).to eq(0)
|
103
|
+
expect(nil_priceable.price.currency).to eq(Money.default_currency)
|
102
104
|
end
|
103
105
|
it 'returns nil if an unknown value was stored' do
|
104
106
|
zero_priceable = Priceable.create(:price => [])
|
data/spec/mongoid/three_spec.rb
CHANGED
@@ -41,11 +41,11 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^3(.*)/
|
|
41
41
|
after { MoneyRails.raise_error_on_money_parsing = false }
|
42
42
|
|
43
43
|
it "raises exception if the mongoized value is a String with a hyphen" do
|
44
|
-
expect { priceable_from_string_with_hyphen }.to raise_error
|
44
|
+
expect { priceable_from_string_with_hyphen }.to raise_error ArgumentError
|
45
45
|
end
|
46
46
|
|
47
47
|
it "raises exception if the mongoized value is a String with an unknown currency" do
|
48
|
-
expect { priceable_from_string_with_unknown_currency }.to raise_error
|
48
|
+
expect { priceable_from_string_with_unknown_currency }.to raise_error ArgumentError
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
@@ -96,9 +96,10 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^3(.*)/
|
|
96
96
|
subject { Priceable.first.price }
|
97
97
|
it { is_expected.to be_an_instance_of(Money) }
|
98
98
|
it { is_expected.to eq(Money.new(100, 'EUR')) }
|
99
|
-
it "returns
|
100
|
-
nil_priceable = Priceable.create(:
|
101
|
-
expect(nil_priceable.price).to
|
99
|
+
it "returns 0 cents in default_currency if a nil value was stored" do
|
100
|
+
nil_priceable = Priceable.create(price: nil)
|
101
|
+
expect(nil_priceable.price.cents).to eq(0)
|
102
|
+
expect(nil_priceable.price.currency).to eq(Money.default_currency)
|
102
103
|
end
|
103
104
|
it 'returns nil if an unknown value was stored' do
|
104
105
|
zero_priceable = Priceable.create(:price => [])
|
data/spec/mongoid/two_spec.rb
CHANGED
@@ -45,7 +45,7 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^2(.*)/
|
|
45
45
|
after { MoneyRails.raise_error_on_money_parsing = false }
|
46
46
|
|
47
47
|
it "raises exception if the mongoized value is a String with a hyphen" do
|
48
|
-
expect { priceable_from_string_with_hyphen }.to raise_error
|
48
|
+
expect { priceable_from_string_with_hyphen }.to raise_error ArgumentError
|
49
49
|
end
|
50
50
|
end
|
51
51
|
|
@@ -60,9 +60,10 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^2(.*)/
|
|
60
60
|
subject { priceable.price }
|
61
61
|
it { is_expected.to be_an_instance_of(Money) }
|
62
62
|
it { is_expected.to eq(Money.new(100, 'EUR')) }
|
63
|
-
it "returns
|
64
|
-
nil_priceable = Priceable.create(:
|
65
|
-
expect(nil_priceable.price).to
|
63
|
+
it "returns 0 cents in default_currency if a nil value was stored" do
|
64
|
+
nil_priceable = Priceable.create(price: nil)
|
65
|
+
expect(nil_priceable.price.cents).to eq(0)
|
66
|
+
expect(nil_priceable.price.currency).to eq(Money.default_currency)
|
66
67
|
end
|
67
68
|
it 'returns nil if an unknown value was stored' do
|
68
69
|
zero_priceable = Priceable.create(:price => [])
|
data/spec/test_helpers_spec.rb
CHANGED
@@ -18,7 +18,7 @@ if defined? ActiveRecord
|
|
18
18
|
shared_context "monetize matcher" do
|
19
19
|
|
20
20
|
it "matches model attribute without a '_cents' suffix by default" do
|
21
|
-
is_expected.to monetize(:
|
21
|
+
is_expected.to monetize(:price)
|
22
22
|
end
|
23
23
|
|
24
24
|
it "matches model attribute specified by :as chain" do
|
@@ -34,7 +34,7 @@ if defined? ActiveRecord
|
|
34
34
|
end
|
35
35
|
|
36
36
|
it "matches model attribute with currency specified by :with_currency chain" do
|
37
|
-
is_expected.to monetize(:
|
37
|
+
is_expected.to monetize(:bonus).with_currency(:gbp)
|
38
38
|
end
|
39
39
|
|
40
40
|
it "does not match non existed attribute" do
|
@@ -42,11 +42,11 @@ if defined? ActiveRecord
|
|
42
42
|
end
|
43
43
|
|
44
44
|
it "does not match wrong currency iso" do
|
45
|
-
is_expected.not_to monetize(:
|
45
|
+
is_expected.not_to monetize(:bonus).with_currency(:usd)
|
46
46
|
end
|
47
47
|
|
48
48
|
it "does not match wrong money attribute name" do
|
49
|
-
is_expected.not_to monetize(:
|
49
|
+
is_expected.not_to monetize(:bonus).as(:bonussss)
|
50
50
|
end
|
51
51
|
end
|
52
52
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: money-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.5.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andreas Loupasakis
|
@@ -10,7 +10,7 @@ authors:
|
|
10
10
|
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2016-01-15 00:00:00.000000000 Z
|
14
14
|
dependencies:
|
15
15
|
- !ruby/object:Gem::Dependency
|
16
16
|
name: money
|
@@ -18,28 +18,28 @@ dependencies:
|
|
18
18
|
requirements:
|
19
19
|
- - "~>"
|
20
20
|
- !ruby/object:Gem::Version
|
21
|
-
version: 6.
|
21
|
+
version: '6.6'
|
22
22
|
type: :runtime
|
23
23
|
prerelease: false
|
24
24
|
version_requirements: !ruby/object:Gem::Requirement
|
25
25
|
requirements:
|
26
26
|
- - "~>"
|
27
27
|
- !ruby/object:Gem::Version
|
28
|
-
version: 6.
|
28
|
+
version: '6.6'
|
29
29
|
- !ruby/object:Gem::Dependency
|
30
30
|
name: monetize
|
31
31
|
requirement: !ruby/object:Gem::Requirement
|
32
32
|
requirements:
|
33
33
|
- - "~>"
|
34
34
|
- !ruby/object:Gem::Version
|
35
|
-
version: 1.
|
35
|
+
version: 1.3.0
|
36
36
|
type: :runtime
|
37
37
|
prerelease: false
|
38
38
|
version_requirements: !ruby/object:Gem::Requirement
|
39
39
|
requirements:
|
40
40
|
- - "~>"
|
41
41
|
- !ruby/object:Gem::Version
|
42
|
-
version: 1.
|
42
|
+
version: 1.3.0
|
43
43
|
- !ruby/object:Gem::Dependency
|
44
44
|
name: activesupport
|
45
45
|
requirement: !ruby/object:Gem::Requirement
|
@@ -116,6 +116,20 @@ dependencies:
|
|
116
116
|
- - "<"
|
117
117
|
- !ruby/object:Gem::Version
|
118
118
|
version: 1.4.0
|
119
|
+
- !ruby/object:Gem::Dependency
|
120
|
+
name: test-unit
|
121
|
+
requirement: !ruby/object:Gem::Requirement
|
122
|
+
requirements:
|
123
|
+
- - "~>"
|
124
|
+
- !ruby/object:Gem::Version
|
125
|
+
version: '3.0'
|
126
|
+
type: :development
|
127
|
+
prerelease: false
|
128
|
+
version_requirements: !ruby/object:Gem::Requirement
|
129
|
+
requirements:
|
130
|
+
- - "~>"
|
131
|
+
- !ruby/object:Gem::Version
|
132
|
+
version: '3.0'
|
119
133
|
description: This library provides integration of RubyMoney - Money gem with Rails
|
120
134
|
email:
|
121
135
|
- alup.rubymoney@gmail.com
|
@@ -183,6 +197,7 @@ files:
|
|
183
197
|
- spec/dummy/config/locales/en-GB.yml
|
184
198
|
- spec/dummy/config/locales/en-US.yml
|
185
199
|
- spec/dummy/config/locales/en.yml
|
200
|
+
- spec/dummy/config/locales/it.yml
|
186
201
|
- spec/dummy/config/mongoid.yml
|
187
202
|
- spec/dummy/config/routes.rb
|
188
203
|
- spec/dummy/db/migrate/20120331190108_create_products.rb
|
@@ -200,6 +215,7 @@ files:
|
|
200
215
|
- spec/dummy/db/migrate/20150213234410_add_special_price_to_products.rb
|
201
216
|
- spec/dummy/db/migrate/20150217222612_add_lambda_price_to_products.rb
|
202
217
|
- spec/dummy/db/migrate/20150303222230_add_skip_validation_price_cents_to_products.rb
|
218
|
+
- spec/dummy/db/migrate/20151026220420_add_optional_amount_to_transactions.rb
|
203
219
|
- spec/dummy/db/schema.rb
|
204
220
|
- spec/dummy/db/structure.sql
|
205
221
|
- spec/dummy/public/404.html
|
@@ -235,7 +251,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
235
251
|
version: '0'
|
236
252
|
requirements: []
|
237
253
|
rubyforge_project:
|
238
|
-
rubygems_version: 2.
|
254
|
+
rubygems_version: 2.4.5.1
|
239
255
|
signing_key:
|
240
256
|
specification_version: 4
|
241
257
|
summary: Money gem integration with Rails
|
@@ -274,6 +290,7 @@ test_files:
|
|
274
290
|
- spec/dummy/config/locales/en-GB.yml
|
275
291
|
- spec/dummy/config/locales/en-US.yml
|
276
292
|
- spec/dummy/config/locales/en.yml
|
293
|
+
- spec/dummy/config/locales/it.yml
|
277
294
|
- spec/dummy/config/mongoid.yml
|
278
295
|
- spec/dummy/config/routes.rb
|
279
296
|
- spec/dummy/db/migrate/20120331190108_create_products.rb
|
@@ -291,6 +308,7 @@ test_files:
|
|
291
308
|
- spec/dummy/db/migrate/20150213234410_add_special_price_to_products.rb
|
292
309
|
- spec/dummy/db/migrate/20150217222612_add_lambda_price_to_products.rb
|
293
310
|
- spec/dummy/db/migrate/20150303222230_add_skip_validation_price_cents_to_products.rb
|
311
|
+
- spec/dummy/db/migrate/20151026220420_add_optional_amount_to_transactions.rb
|
294
312
|
- spec/dummy/db/schema.rb
|
295
313
|
- spec/dummy/db/structure.sql
|
296
314
|
- spec/dummy/public/404.html
|