money-rails 1.13.2 → 1.15.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
- data/CHANGELOG.md +28 -0
- data/LICENSE +18 -18
- data/README.md +3 -3
- data/Rakefile +7 -0
- data/config/locales/money.da.yml +4 -0
- data/config/locales/money.en.yml +1 -2
- data/config/locales/money.ru.yml +4 -0
- data/lib/generators/templates/money.rb +23 -0
- data/lib/money-rails/active_model/validator.rb +68 -80
- data/lib/money-rails/active_record/migration_extensions/schema_statements_pg_rails4.rb +4 -3
- data/lib/money-rails/active_record/migration_extensions/table_pg_rails4.rb +2 -1
- data/lib/money-rails/active_record/monetizable.rb +1 -1
- data/lib/money-rails/helpers/action_view_extension.rb +2 -3
- data/lib/money-rails/hooks.rb +7 -1
- data/lib/money-rails/money.rb +5 -0
- data/lib/money-rails/rails_admin.rb +1 -1
- data/lib/money-rails/railtie.rb +1 -1
- data/lib/money-rails/version.rb +1 -1
- data/money-rails.gemspec +9 -2
- data/spec/active_record/monetizable_spec.rb +15 -7
- data/spec/dummy/app/assets/config/manifest.js +0 -0
- data/spec/dummy/db/schema.rb +30 -31
- data/spec/helpers/action_view_extension_spec.rb +12 -0
- data/spec/money_spec.rb +40 -0
- data/spec/mongoid/mongoid_spec.rb +2 -2
- data/spec/mongoid/two_spec.rb +2 -2
- metadata +34 -12
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
|
-
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 5bb5642fbc3d57d55b592911898fc1c0beeb8bb42bb21aeea89c89d1d3dc5464
|
4
|
+
data.tar.gz: 8a5100169dea70dd4bbb8770a2273e59c6b99c555173025bfcc18fb4170fd7d0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: eb7961c60fab97af3a1e867ebcc192fcdc6468bb4239a18f60fb269014f898836051319db22b14c6b7c69b47287f6772be68d29db4e3be6a5d0366b895cea6b4
|
7
|
+
data.tar.gz: 193580effbe3f55093e418f8ca6a0eea54bbfd6a46f07d0f8641a57d450564739716f69d18e743b839f785cc5d75513c234445a9378430f9bc943220598c51ae
|
data/CHANGELOG.md
CHANGED
@@ -1,5 +1,32 @@
|
|
1
1
|
# Changelog
|
2
2
|
|
3
|
+
## 1.14.1
|
4
|
+
|
5
|
+
- Fix invalid_currency error definition
|
6
|
+
- Add Russian translation for errors
|
7
|
+
- Loosen money version to ~> 6.13
|
8
|
+
- Loosen monetize version to ~> 1.9
|
9
|
+
|
10
|
+
## 1.14.0
|
11
|
+
|
12
|
+
- Tweaks to support Ruby 3.0
|
13
|
+
|
14
|
+
## 1.13.4
|
15
|
+
|
16
|
+
- Fix validator race condition
|
17
|
+
- Add Danish translation for errors
|
18
|
+
- Change Money fields to Decimal in Rails Admin
|
19
|
+
- Run hooks after active_record.initialize_database
|
20
|
+
- Add optional currency argument to "#currency_symbol" helper
|
21
|
+
- Rails 6.1 support
|
22
|
+
|
23
|
+
## 1.13.3
|
24
|
+
|
25
|
+
- Add Money#to_hash for JSON serialization
|
26
|
+
- Update initializer template with #locale_backend config
|
27
|
+
- Rollback support for remove_monetize / remove_money DB helpers
|
28
|
+
- Rails 6 support
|
29
|
+
|
3
30
|
## 1.13.2
|
4
31
|
|
5
32
|
- Make validation compatible with Money.locale_backend
|
@@ -19,6 +46,7 @@
|
|
19
46
|
|
20
47
|
- Bump money version to ~> 6.12.0
|
21
48
|
- Bump monetize version to ~> 1.9.0
|
49
|
+
- BREAKING CHANGE: Fix to rounding logic in this version from 1.11.0 can cause breaking changes for those relying on the incorrect behavior. Eee [the issue](https://github.com/RubyMoney/money-rails/issues/608) for details.
|
22
50
|
|
23
51
|
## 1.11.0
|
24
52
|
|
data/LICENSE
CHANGED
@@ -1,22 +1,22 @@
|
|
1
|
-
Copyright (c) 2012 Andreas Loupasakis
|
2
|
-
|
3
1
|
MIT License
|
4
2
|
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
3
|
+
Copyright (c) 2012 Andreas Loupasakis
|
4
|
+
Copyright (c) 2021 Shane Emmons
|
5
|
+
|
6
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
7
|
+
of this software and associated documentation files (the "Software"), to deal
|
8
|
+
in the Software without restriction, including without limitation the rights
|
9
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
10
|
+
copies of the Software, and to permit persons to whom the Software is
|
11
|
+
furnished to do so, subject to the following conditions:
|
12
12
|
|
13
|
-
The above copyright notice and this permission notice shall be
|
14
|
-
|
13
|
+
The above copyright notice and this permission notice shall be included in all
|
14
|
+
copies or substantial portions of the Software.
|
15
15
|
|
16
|
-
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
OF
|
22
|
-
|
16
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
17
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
18
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
19
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
20
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
21
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
22
|
+
SOFTWARE.
|
data/README.md
CHANGED
@@ -116,7 +116,7 @@ end
|
|
116
116
|
|
117
117
|
Notice. Default value of currency field, generated by migration's helper, is USD. To override these defaults, you need change default_currency in money initializer and run migrations.
|
118
118
|
|
119
|
-
The ```add_money``` helper is reversible, so you
|
119
|
+
The ```add_money``` helper is reversible, so you can use it inside ```change```
|
120
120
|
migrations. If you're writing separate ```up``` and ```down``` methods, you
|
121
121
|
can use the ```remove_money``` helper.
|
122
122
|
|
@@ -306,7 +306,7 @@ object using EUR as their currency, instead of the default USD.
|
|
306
306
|
|
307
307
|
By passing the option ```:with_currency``` to the ```monetize``` macro call,
|
308
308
|
with a currency code (symbol or string) or a callable object (object that responds to the method ```call```) that returns a currency code, as its value, you can define a currency in a more granular
|
309
|
-
way. This will you attach the given currency only to the specified monetized model
|
309
|
+
way. This will let you attach the given currency only to the specified monetized model
|
310
310
|
attribute (allowing you to, for example, monetize different attributes of the same model with different currencies.).
|
311
311
|
|
312
312
|
This allows you to override both the model level and the global
|
@@ -587,4 +587,4 @@ If you are testing against mongoid, make sure to have the mongod process running
|
|
587
587
|
|
588
588
|
## License
|
589
589
|
|
590
|
-
MIT License. Copyright
|
590
|
+
MIT License. Copyright 2021 RubyMoney.
|
data/Rakefile
CHANGED
@@ -55,6 +55,13 @@ namespace :spec do
|
|
55
55
|
framework, version = file_name.split(/(\d+)/)
|
56
56
|
major, minor = version.split(//)
|
57
57
|
|
58
|
+
# Ruby 3 exclusions
|
59
|
+
if Gem::Version.new(RUBY_VERSION) >= Gem::Version.new('3.0.0')
|
60
|
+
# Rails 5 does not support ruby-3.0.0 https://github.com/rails/rails/issues/40938#issuecomment-751569171
|
61
|
+
# Mongoid gem does not yet support ruby-3.0.0 https://github.com/mongodb/mongoid#compatibility
|
62
|
+
next if framework == 'mongoid' || (framework == 'rails' && version == "5")
|
63
|
+
end
|
64
|
+
|
58
65
|
frameworks_versions[framework] ||= []
|
59
66
|
frameworks_versions[framework] << file_name
|
60
67
|
|
data/config/locales/money.en.yml
CHANGED
@@ -1,5 +1,4 @@
|
|
1
1
|
en:
|
2
2
|
errors:
|
3
3
|
messages:
|
4
|
-
invalid_currency:
|
5
|
-
|
4
|
+
invalid_currency: has invalid format (must be '100', '5%{decimal}24', or '123%{thousands}456%{decimal}78'). Got %{currency}
|
@@ -83,6 +83,29 @@ MoneyRails.configure do |config|
|
|
83
83
|
# sign_before_symbol: nil
|
84
84
|
# }
|
85
85
|
|
86
|
+
# If you would like to use I18n localization (formatting depends on the
|
87
|
+
# locale):
|
88
|
+
# config.locale_backend = :i18n
|
89
|
+
#
|
90
|
+
# Example (using default localization from rails-i18n):
|
91
|
+
#
|
92
|
+
# I18n.locale = :en
|
93
|
+
# Money.new(10_000_00, 'USD').format # => $10,000.00
|
94
|
+
# I18n.locale = :es
|
95
|
+
# Money.new(10_000_00, 'USD').format # => $10.000,00
|
96
|
+
#
|
97
|
+
# For the legacy behaviour of "per currency" localization (formatting depends
|
98
|
+
# only on currency):
|
99
|
+
# config.locale_backend = :currency
|
100
|
+
#
|
101
|
+
# Example:
|
102
|
+
# Money.new(10_000_00, 'USD').format # => $10,000.00
|
103
|
+
# Money.new(10_000_00, 'EUR').format # => €10.000,00
|
104
|
+
#
|
105
|
+
# In case you don't need localization and would like to use default values
|
106
|
+
# (can be redefined using config.default_format):
|
107
|
+
# config.locale_backend = nil
|
108
|
+
|
86
109
|
# Set default raise_error_on_money_parsing option
|
87
110
|
# It will be raise error if assigned different currency
|
88
111
|
# The default value is false
|
@@ -1,44 +1,60 @@
|
|
1
1
|
module MoneyRails
|
2
2
|
module ActiveModel
|
3
3
|
class MoneyValidator < ::ActiveModel::Validations::NumericalityValidator
|
4
|
-
|
5
|
-
|
6
|
-
|
7
|
-
|
4
|
+
class Details < Struct.new(:raw_value, :thousands_separator, :decimal_mark)
|
5
|
+
def abs_raw_value
|
6
|
+
@abs_raw_value ||= raw_value.to_s.sub(/^\s*-/, "").strip
|
7
|
+
end
|
8
8
|
|
9
|
-
|
9
|
+
def decimal_pieces
|
10
|
+
@decimal_pieces ||= abs_raw_value.split(decimal_mark)
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def validate_each(record, attr, _value)
|
15
|
+
subunit_attr = record.class.monetized_attributes[attr.to_s]
|
16
|
+
currency = record.public_send("currency_for_#{attr}")
|
10
17
|
|
11
18
|
# WARNING: Currently this is only defined in ActiveRecord extension!
|
12
|
-
before_type_cast = :"#{
|
13
|
-
|
19
|
+
before_type_cast = :"#{attr}_money_before_type_cast"
|
20
|
+
raw_value = record.try(before_type_cast)
|
14
21
|
|
15
22
|
# If raw value is nil and changed subunit is nil, then
|
16
23
|
# nil is a assigned value, else we should treat the
|
17
24
|
# subunit value as the one assigned.
|
18
|
-
if
|
19
|
-
subunit_value =
|
20
|
-
|
25
|
+
if raw_value.nil? && record.public_send(subunit_attr)
|
26
|
+
subunit_value = record.public_send(subunit_attr)
|
27
|
+
raw_value = subunit_value.to_f / currency.subunit_to_unit
|
21
28
|
end
|
22
29
|
|
23
|
-
return if options[:allow_nil] &&
|
30
|
+
return if options[:allow_nil] && raw_value.nil?
|
24
31
|
|
25
|
-
# Set this before we modify
|
26
|
-
stringy =
|
32
|
+
# Set this before we modify raw_value below.
|
33
|
+
stringy = raw_value.present? && !raw_value.is_a?(Numeric) && !raw_value.is_a?(Money)
|
27
34
|
|
28
35
|
if stringy
|
36
|
+
# TODO: This is supporting legacy behaviour where a symbol can come from a i18n locale,
|
37
|
+
# however practical implications of that are most likely non-existent
|
38
|
+
symbol = lookup(:symbol, currency) || currency.symbol
|
39
|
+
|
29
40
|
# remove currency symbol
|
30
|
-
|
41
|
+
raw_value = raw_value.to_s.gsub(symbol, "")
|
31
42
|
end
|
32
43
|
|
33
|
-
|
34
|
-
|
44
|
+
# Cache abs_raw_value before normalizing because it's used in
|
45
|
+
# many places and relies on the original raw_value.
|
46
|
+
details = generate_details(raw_value, currency)
|
47
|
+
normalized_raw_value = normalize(details)
|
35
48
|
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
49
|
+
super(record, attr, normalized_raw_value)
|
50
|
+
|
51
|
+
return unless stringy
|
52
|
+
return if record_already_has_error?(record, attr, normalized_raw_value)
|
53
|
+
|
54
|
+
add_error!(record, attr, details) if
|
55
|
+
value_has_too_many_decimal_points(details) ||
|
56
|
+
thousand_separator_after_decimal_mark(details) ||
|
57
|
+
invalid_thousands_separation(details)
|
42
58
|
end
|
43
59
|
|
44
60
|
private
|
@@ -48,70 +64,45 @@ module MoneyRails
|
|
48
64
|
thousands_separator: ','
|
49
65
|
}.freeze
|
50
66
|
|
51
|
-
def
|
52
|
-
|
53
|
-
|
67
|
+
def generate_details(raw_value, currency)
|
68
|
+
thousands_separator = lookup(:thousands_separator, currency)
|
69
|
+
decimal_mark = lookup(:decimal_mark, currency)
|
54
70
|
|
55
|
-
|
56
|
-
[:currency, :decimal_mark, :thousands_separator, :symbol,
|
57
|
-
:abs_raw_value, :decimal_pieces, :pieces_array].each do |var_name|
|
58
|
-
ivar_name = :"@_#{var_name}"
|
59
|
-
remove_instance_variable(ivar_name) if instance_variable_defined?(ivar_name)
|
60
|
-
end
|
71
|
+
Details.new(raw_value, thousands_separator, decimal_mark)
|
61
72
|
end
|
62
73
|
|
63
|
-
def
|
64
|
-
|
74
|
+
def record_already_has_error?(record, attr, raw_value)
|
75
|
+
record.errors.added?(attr, :not_a_number, value: raw_value)
|
65
76
|
end
|
66
77
|
|
67
|
-
def
|
68
|
-
|
69
|
-
|
78
|
+
def add_error!(record, attr, details)
|
79
|
+
attr_name = attr.to_s.tr('.', '_').humanize
|
80
|
+
attr_name = record.class.human_attribute_name(attr, default: attr_name)
|
70
81
|
|
71
|
-
|
72
|
-
|
82
|
+
record.errors.add(attr, :invalid_currency,
|
83
|
+
thousands: details.thousands_separator,
|
84
|
+
decimal: details.decimal_mark,
|
85
|
+
currency: details.abs_raw_value,
|
86
|
+
attribute: attr_name
|
87
|
+
)
|
73
88
|
end
|
74
89
|
|
75
|
-
|
76
|
-
|
77
|
-
def symbol
|
78
|
-
@_symbol ||= lookup(:symbol) || currency.symbol
|
90
|
+
def value_has_too_many_decimal_points(details)
|
91
|
+
![1, 2].include?(details.decimal_pieces.length)
|
79
92
|
end
|
80
93
|
|
81
|
-
def
|
82
|
-
|
94
|
+
def thousand_separator_after_decimal_mark(details)
|
95
|
+
details.thousands_separator.present? &&
|
96
|
+
details.decimal_pieces.length == 2 &&
|
97
|
+
details.decimal_pieces[1].include?(details.thousands_separator)
|
83
98
|
end
|
84
99
|
|
85
|
-
def
|
86
|
-
|
87
|
-
attr_name = @record.class.human_attribute_name(@attr, default: attr_name)
|
100
|
+
def invalid_thousands_separation(details)
|
101
|
+
pieces_array = details.decimal_pieces[0].split(details.thousands_separator.presence)
|
88
102
|
|
89
|
-
@record.errors.add(@attr, :invalid_currency,
|
90
|
-
{ thousands: thousands_separator,
|
91
|
-
decimal: decimal_mark,
|
92
|
-
currency: abs_raw_value,
|
93
|
-
attribute: attr_name })
|
94
|
-
end
|
95
|
-
|
96
|
-
def decimal_pieces
|
97
|
-
@_decimal_pieces ||= abs_raw_value.split(decimal_mark)
|
98
|
-
end
|
99
|
-
|
100
|
-
def value_has_too_many_decimal_points
|
101
|
-
![1, 2].include?(decimal_pieces.length)
|
102
|
-
end
|
103
|
-
|
104
|
-
def pieces_array
|
105
|
-
@_pieces_array ||= decimal_pieces[0].split(thousands_separator.presence)
|
106
|
-
end
|
107
|
-
|
108
|
-
def thousand_separator_after_decimal_mark
|
109
|
-
thousands_separator.present? && decimal_pieces.length == 2 && decimal_pieces[1].include?(thousands_separator)
|
110
|
-
end
|
111
|
-
|
112
|
-
def invalid_thousands_separation
|
113
103
|
return false if pieces_array.length <= 1
|
114
104
|
return true if pieces_array[0].length > 3
|
105
|
+
|
115
106
|
pieces_array[1..-1].any? do |thousands_group|
|
116
107
|
thousands_group.length != 3
|
117
108
|
end
|
@@ -119,18 +110,15 @@ module MoneyRails
|
|
119
110
|
|
120
111
|
# Remove thousands separators, normalize decimal mark,
|
121
112
|
# remove whitespaces and _ (E.g. 99 999 999 or 12_300_200.20)
|
122
|
-
def
|
123
|
-
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
@raw_value = @raw_value.to_s
|
128
|
-
.gsub(thousands_separator, '')
|
129
|
-
.gsub(decimal_mark, '.')
|
113
|
+
def normalize(details)
|
114
|
+
details.raw_value
|
115
|
+
.to_s
|
116
|
+
.gsub(details.thousands_separator, '')
|
117
|
+
.gsub(details.decimal_mark, '.')
|
130
118
|
.gsub(/[\s_]/, '')
|
131
119
|
end
|
132
120
|
|
133
|
-
def lookup(key)
|
121
|
+
def lookup(key, currency)
|
134
122
|
if locale_backend
|
135
123
|
locale_backend.lookup(key, currency) || DEFAULTS[key]
|
136
124
|
else
|
@@ -5,14 +5,15 @@ 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
|
-
|
8
|
+
constraints = opts.pop
|
9
|
+
add_column(*opts, **constraints) if column_present
|
9
10
|
end
|
10
11
|
end
|
11
12
|
|
12
13
|
def remove_monetize(table_name, accessor, options={})
|
13
14
|
[:amount, :currency].each do |attribute|
|
14
|
-
column_present, table_name, column_name,
|
15
|
-
remove_column table_name, column_name if column_present
|
15
|
+
column_present, table_name, column_name, type, _ = OptionsExtractor.extract attribute, table_name, accessor, options
|
16
|
+
remove_column table_name, column_name, type if column_present
|
16
17
|
end
|
17
18
|
end
|
18
19
|
end
|
@@ -5,7 +5,8 @@ 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
|
-
|
8
|
+
constraints = opts.pop
|
9
|
+
column(*opts, **constraints) if column_present
|
9
10
|
end
|
10
11
|
end
|
11
12
|
|
@@ -209,7 +209,7 @@ module MoneyRails
|
|
209
209
|
|
210
210
|
if MoneyRails::Configuration.preserve_user_input
|
211
211
|
value_before_type_cast = instance_variable_get "@#{name}_money_before_type_cast"
|
212
|
-
if errors
|
212
|
+
if errors.has_key?(name.to_sym)
|
213
213
|
result.define_singleton_method(:to_s) { value_before_type_cast }
|
214
214
|
result.define_singleton_method(:format) { |_| value_before_type_cast }
|
215
215
|
end
|
@@ -1,8 +1,7 @@
|
|
1
1
|
module MoneyRails
|
2
2
|
module ActionViewExtension
|
3
|
-
|
4
|
-
|
5
|
-
content_tag(:span, Money.default_currency.symbol, class: "currency_symbol")
|
3
|
+
def currency_symbol(currency = Money.default_currency)
|
4
|
+
content_tag(:span, Money::Currency.find(currency).symbol, class: "currency_symbol")
|
6
5
|
end
|
7
6
|
|
8
7
|
def humanized_money(value, options={})
|
data/lib/money-rails/hooks.rb
CHANGED
@@ -18,7 +18,13 @@ module MoneyRails
|
|
18
18
|
else
|
19
19
|
false
|
20
20
|
end
|
21
|
-
|
21
|
+
|
22
|
+
current_adapter = if ::ActiveRecord::Base.respond_to?(:connection_db_config)
|
23
|
+
::ActiveRecord::Base.connection_db_config.configuration_hash[:adapter]
|
24
|
+
else
|
25
|
+
::ActiveRecord::Base.connection_config[:adapter]
|
26
|
+
end
|
27
|
+
|
22
28
|
postgresql_with_money = rails42 && PG_ADAPTERS.include?(current_adapter)
|
23
29
|
end
|
24
30
|
end
|
data/lib/money-rails/money.rb
CHANGED
@@ -8,7 +8,7 @@ module RailsAdmin
|
|
8
8
|
module Config
|
9
9
|
module Fields
|
10
10
|
module Types
|
11
|
-
class Money < RailsAdmin::Config::Fields::Types::
|
11
|
+
class Money < RailsAdmin::Config::Fields::Types::Decimal
|
12
12
|
RailsAdmin::Config::Fields::Types::register(self)
|
13
13
|
|
14
14
|
register_instance_option :pretty_value do
|
data/lib/money-rails/railtie.rb
CHANGED
data/lib/money-rails/version.rb
CHANGED
data/money-rails.gemspec
CHANGED
@@ -26,8 +26,8 @@ Gem::Specification.new do |s|
|
|
26
26
|
|
27
27
|
s.require_path = "lib"
|
28
28
|
|
29
|
-
s.add_dependency "money", "~> 6.13
|
30
|
-
s.add_dependency "monetize", "~> 1.9
|
29
|
+
s.add_dependency "money", "~> 6.13"
|
30
|
+
s.add_dependency "monetize", "~> 1.9"
|
31
31
|
s.add_dependency "activesupport", ">= 3.0"
|
32
32
|
s.add_dependency "railties", ">= 3.0"
|
33
33
|
s.add_dependency "mime-types", "< 3" if RUBY_VERSION < '2.0' # mime-types > 3 depends on mime-types-data, which doesn't support ruby 1.9
|
@@ -36,4 +36,11 @@ Gem::Specification.new do |s|
|
|
36
36
|
s.add_development_dependency "rspec-rails", "~> 3.0"
|
37
37
|
s.add_development_dependency 'database_cleaner', '~> 1.6.1'
|
38
38
|
s.add_development_dependency 'test-unit', '~> 3.0' if RUBY_VERSION >= '2.2'
|
39
|
+
s.add_development_dependency 'bundler'
|
40
|
+
|
41
|
+
if s.respond_to?(:metadata)
|
42
|
+
s.metadata['changelog_uri'] = 'https://github.com/RubyMoney/money-rails/blob/master/CHANGELOG.md'
|
43
|
+
s.metadata['source_code_uri'] = 'https://github.com/RubyMoney/money-rails/'
|
44
|
+
s.metadata['bug_tracker_uri'] = 'https://github.com/RubyMoney/money-rails/issues'
|
45
|
+
end
|
39
46
|
end
|
@@ -20,6 +20,14 @@ if defined? ActiveRecord
|
|
20
20
|
Service.create(charge_cents: 2000, discount_cents: 120)
|
21
21
|
end
|
22
22
|
|
23
|
+
def update_product(*attributes)
|
24
|
+
if defined?(::ActiveRecord::VERSION) && ::ActiveRecord::VERSION::MAJOR >= 5
|
25
|
+
product.update(*attributes)
|
26
|
+
else
|
27
|
+
product.update_attributes(*attributes)
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
23
31
|
context ".monetized_attributes" do
|
24
32
|
|
25
33
|
class InheritedMonetizeProduct < Product
|
@@ -73,7 +81,7 @@ if defined? ActiveRecord
|
|
73
81
|
end
|
74
82
|
|
75
83
|
it "correctly updates from a Money object using update_attributes" do
|
76
|
-
expect(
|
84
|
+
expect(update_product(price: Money.new(215, "USD"))).to be_truthy
|
77
85
|
expect(product.price_cents).to eq(215)
|
78
86
|
end
|
79
87
|
|
@@ -187,8 +195,8 @@ if defined? ActiveRecord
|
|
187
195
|
end
|
188
196
|
|
189
197
|
it "respects numericality validation when using update_attributes" do
|
190
|
-
expect(
|
191
|
-
expect(
|
198
|
+
expect(update_product(price_cents: "some text")).to be_falsey
|
199
|
+
expect(update_product(price_cents: 2000)).to be_truthy
|
192
200
|
end
|
193
201
|
|
194
202
|
it "uses numericality validation on money attribute" do
|
@@ -237,7 +245,7 @@ if defined? ActiveRecord
|
|
237
245
|
product.price = "12,23.24"
|
238
246
|
expect(product.save).to be_falsey
|
239
247
|
expect(product.errors[:price].size).to eq(1)
|
240
|
-
expect(product.errors[:price].first).to match(/
|
248
|
+
expect(product.errors[:price].first).to match(/has invalid format/)
|
241
249
|
expect(product.errors[:price].first).to match(/Got 12,23.24/)
|
242
250
|
end
|
243
251
|
|
@@ -245,7 +253,7 @@ if defined? ActiveRecord
|
|
245
253
|
product.price = "1.234,56"
|
246
254
|
expect(product.save).to be_falsey
|
247
255
|
expect(product.errors[:price].size).to eq(1)
|
248
|
-
expect(product.errors[:price].first).to match(/
|
256
|
+
expect(product.errors[:price].first).to match(/has invalid format/)
|
249
257
|
expect(product.errors[:price].first).to match(/Got 1.234,56/)
|
250
258
|
end
|
251
259
|
|
@@ -415,8 +423,8 @@ if defined? ActiveRecord
|
|
415
423
|
end
|
416
424
|
|
417
425
|
it "respects numericality validation when using update_attributes on money attribute" do
|
418
|
-
expect(
|
419
|
-
expect(
|
426
|
+
expect(update_product(price: "some text")).to be_falsey
|
427
|
+
expect(update_product(price: Money.new(320, 'USD'))).to be_truthy
|
420
428
|
end
|
421
429
|
|
422
430
|
it "uses i18n currency format when validating" do
|
File without changes
|
data/spec/dummy/db/schema.rb
CHANGED
@@ -1,60 +1,59 @@
|
|
1
|
-
# encoding: UTF-8
|
2
1
|
# This file is auto-generated from the current state of the database. Instead
|
3
2
|
# of editing this file, please use the migrations feature of Active Record to
|
4
3
|
# incrementally modify your database, and then regenerate this schema definition.
|
5
4
|
#
|
6
|
-
#
|
7
|
-
#
|
8
|
-
#
|
9
|
-
# from scratch.
|
10
|
-
#
|
5
|
+
# This file is the source Rails uses to define your schema when running `bin/rails
|
6
|
+
# db:schema:load`. When creating a new database, `bin/rails db:schema:load` tends to
|
7
|
+
# be faster and is potentially less error prone than running all of your
|
8
|
+
# migrations from scratch. Old migrations may fail to apply correctly if those
|
9
|
+
# migrations use external dependencies or application code.
|
11
10
|
#
|
12
11
|
# It's strongly recommended that you check this file into your version control system.
|
13
12
|
|
14
|
-
ActiveRecord::Schema.define(version:
|
13
|
+
ActiveRecord::Schema.define(version: 2015_10_26_220420) do
|
15
14
|
|
16
15
|
create_table "dummy_products", force: :cascade do |t|
|
17
|
-
t.string
|
18
|
-
t.integer
|
16
|
+
t.string "currency"
|
17
|
+
t.integer "price_cents"
|
19
18
|
t.datetime "created_at"
|
20
19
|
t.datetime "updated_at"
|
21
20
|
end
|
22
21
|
|
23
22
|
create_table "products", force: :cascade do |t|
|
24
|
-
t.integer
|
25
|
-
t.integer
|
23
|
+
t.integer "price_cents"
|
24
|
+
t.integer "discount"
|
26
25
|
t.datetime "created_at"
|
27
26
|
t.datetime "updated_at"
|
28
|
-
t.integer
|
29
|
-
t.integer
|
30
|
-
t.integer
|
31
|
-
t.string
|
32
|
-
t.integer
|
33
|
-
t.integer
|
34
|
-
t.integer
|
35
|
-
t.integer
|
36
|
-
t.integer
|
37
|
-
t.integer
|
38
|
-
t.string
|
39
|
-
t.integer
|
40
|
-
t.integer
|
41
|
-
t.string
|
27
|
+
t.integer "bonus_cents"
|
28
|
+
t.integer "optional_price_cents"
|
29
|
+
t.integer "sale_price_amount", default: 0, null: false
|
30
|
+
t.string "sale_price_currency_code"
|
31
|
+
t.integer "price_in_a_range_cents"
|
32
|
+
t.integer "validates_method_amount_cents"
|
33
|
+
t.integer "aliased_cents"
|
34
|
+
t.integer "delivery_fee_cents"
|
35
|
+
t.integer "restock_fee_cents"
|
36
|
+
t.integer "reduced_price_cents"
|
37
|
+
t.string "reduced_price_currency"
|
38
|
+
t.integer "special_price_cents"
|
39
|
+
t.integer "lambda_price_cents"
|
40
|
+
t.string "skip_validation_price_cents"
|
42
41
|
end
|
43
42
|
|
44
43
|
create_table "services", force: :cascade do |t|
|
45
|
-
t.integer
|
46
|
-
t.integer
|
44
|
+
t.integer "charge_cents"
|
45
|
+
t.integer "discount_cents"
|
47
46
|
t.datetime "created_at"
|
48
47
|
t.datetime "updated_at"
|
49
48
|
end
|
50
49
|
|
51
50
|
create_table "transactions", force: :cascade do |t|
|
52
|
-
t.integer
|
53
|
-
t.integer
|
54
|
-
t.string
|
51
|
+
t.integer "amount_cents"
|
52
|
+
t.integer "tax_cents"
|
53
|
+
t.string "currency"
|
55
54
|
t.datetime "created_at"
|
56
55
|
t.datetime "updated_at"
|
57
|
-
t.integer
|
56
|
+
t.integer "optional_amount_cents"
|
58
57
|
end
|
59
58
|
|
60
59
|
end
|
@@ -5,6 +5,18 @@ describe 'MoneyRails::ActionViewExtension', type: :helper do
|
|
5
5
|
subject { helper.currency_symbol }
|
6
6
|
it { is_expected.to be_a String }
|
7
7
|
it { is_expected.to include Money.default_currency.symbol }
|
8
|
+
|
9
|
+
context 'with given currency' do
|
10
|
+
subject { helper.currency_symbol(Money::Currency.find(:brl)) }
|
11
|
+
it { is_expected.to include Money::Currency.find(:brl).symbol }
|
12
|
+
it { is_expected.to include Money::Currency.find(:brl).symbol }
|
13
|
+
end
|
14
|
+
|
15
|
+
context 'with given currency symbol' do
|
16
|
+
subject { helper.currency_symbol(:brl) }
|
17
|
+
it { is_expected.to include Money::Currency.find(:brl).symbol }
|
18
|
+
it { is_expected.to include Money::Currency.find(:brl).symbol }
|
19
|
+
end
|
8
20
|
end
|
9
21
|
|
10
22
|
describe '#humanized_money' do
|
data/spec/money_spec.rb
ADDED
@@ -0,0 +1,40 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'spec_helper'
|
4
|
+
|
5
|
+
describe 'Money overrides' do
|
6
|
+
describe '.default_formatting_rules' do
|
7
|
+
it 'uses defauts set as individual options' do
|
8
|
+
allow(MoneyRails::Configuration).to receive(:symbol).and_return('£')
|
9
|
+
|
10
|
+
expect(Money.default_formatting_rules).to include(symbol: '£')
|
11
|
+
end
|
12
|
+
|
13
|
+
it 'ignores individual options that are nil' do
|
14
|
+
allow(MoneyRails::Configuration).to receive(:symbol).and_return(nil)
|
15
|
+
|
16
|
+
expect(Money.default_formatting_rules.keys).not_to include(:symbol)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'includes default_format options' do
|
20
|
+
allow(MoneyRails::Configuration).to receive(:default_format).and_return(symbol: '£')
|
21
|
+
|
22
|
+
expect(Money.default_formatting_rules).to include(symbol: '£')
|
23
|
+
end
|
24
|
+
|
25
|
+
it 'gives priority to original defaults' do
|
26
|
+
allow(Money).to receive(:orig_default_formatting_rules).and_return(symbol: '£')
|
27
|
+
allow(MoneyRails::Configuration).to receive(:symbol).and_return('€')
|
28
|
+
allow(MoneyRails::Configuration).to receive(:default_format).and_return(symbol: '€')
|
29
|
+
|
30
|
+
expect(Money.default_formatting_rules).to include(symbol: '£')
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
describe '#to_hash' do
|
35
|
+
it 'returns a hash with JSON representation' do
|
36
|
+
expect(Money.new(9_99, 'EUR').to_hash).to eq(cents: 9_99, currency_iso: 'EUR')
|
37
|
+
expect(Money.zero('USD').to_hash).to eq(cents: 0, currency_iso: 'USD')
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -16,7 +16,7 @@ if defined?(Mongoid) && ::Mongoid::VERSION.split('.').first.to_i > 2
|
|
16
16
|
}
|
17
17
|
let(:priceable_from_string_with_hyphen) { Priceable.create(price: '1-2 EUR' )}
|
18
18
|
let(:priceable_from_string_with_unknown_currency) { Priceable.create(price: '1 TLDR') }
|
19
|
-
let(:priceable_with_infinite_precision) { Priceable.create(price: Money.new(BigDecimal
|
19
|
+
let(:priceable_with_infinite_precision) { Priceable.create(price: Money.new(BigDecimal('100.1'), 'EUR')) }
|
20
20
|
let(:priceable_with_hash_field) {
|
21
21
|
Priceable.create(price_hash: {
|
22
22
|
key1: Money.new(100, "EUR"),
|
@@ -91,7 +91,7 @@ if defined?(Mongoid) && ::Mongoid::VERSION.split('.').first.to_i > 2
|
|
91
91
|
end
|
92
92
|
|
93
93
|
it "correctly mongoizes a Money object to a hash of cents and currency" do
|
94
|
-
expect(priceable_with_infinite_precision.price.cents).to eq(BigDecimal
|
94
|
+
expect(priceable_with_infinite_precision.price.cents).to eq(BigDecimal('100.1'))
|
95
95
|
expect(priceable_with_infinite_precision.price.currency).to eq(Money::Currency.find('EUR'))
|
96
96
|
end
|
97
97
|
end
|
data/spec/mongoid/two_spec.rb
CHANGED
@@ -7,7 +7,7 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^2(.*)/
|
|
7
7
|
let(:priceable_from_nil) { Priceable.create(price: nil) }
|
8
8
|
let(:priceable_from_num) { Priceable.create(price: 1) }
|
9
9
|
let(:priceable_from_string) { Priceable.create(price: '1 EUR' )}
|
10
|
-
let(:priceable_with_infinite_precision) { Priceable.create(price: Money.new(BigDecimal
|
10
|
+
let(:priceable_with_infinite_precision) { Priceable.create(price: Money.new(BigDecimal('100.1'), 'EUR')) }
|
11
11
|
let(:priceable_from_string_with_hyphen) { Priceable.create(price: '1-2 EUR' )}
|
12
12
|
|
13
13
|
context "serialize" do
|
@@ -40,7 +40,7 @@ if defined?(Mongoid) && ::Mongoid::VERSION =~ /^2(.*)/
|
|
40
40
|
end
|
41
41
|
|
42
42
|
it "mongoizes correctly a Money object to a hash of cents and currency" do
|
43
|
-
expect(priceable_with_infinite_precision.price.cents).to eq(BigDecimal
|
43
|
+
expect(priceable_with_infinite_precision.price.cents).to eq(BigDecimal('100.1'))
|
44
44
|
expect(priceable_with_infinite_precision.price.currency).to eq(Money::Currency.find('EUR'))
|
45
45
|
end
|
46
46
|
end
|
metadata
CHANGED
@@ -1,16 +1,16 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: money-rails
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.
|
4
|
+
version: 1.15.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Andreas Loupasakis
|
8
8
|
- Shane Emmons
|
9
9
|
- Simone Carletti
|
10
|
-
autorequire:
|
10
|
+
autorequire:
|
11
11
|
bindir: bin
|
12
12
|
cert_chain: []
|
13
|
-
date:
|
13
|
+
date: 2021-09-27 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.13
|
21
|
+
version: '6.13'
|
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.13
|
28
|
+
version: '6.13'
|
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.9
|
35
|
+
version: '1.9'
|
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.9
|
42
|
+
version: '1.9'
|
43
43
|
- !ruby/object:Gem::Dependency
|
44
44
|
name: activesupport
|
45
45
|
requirement: !ruby/object:Gem::Requirement
|
@@ -124,6 +124,20 @@ dependencies:
|
|
124
124
|
- - "~>"
|
125
125
|
- !ruby/object:Gem::Version
|
126
126
|
version: '3.0'
|
127
|
+
- !ruby/object:Gem::Dependency
|
128
|
+
name: bundler
|
129
|
+
requirement: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
type: :development
|
135
|
+
prerelease: false
|
136
|
+
version_requirements: !ruby/object:Gem::Requirement
|
137
|
+
requirements:
|
138
|
+
- - ">="
|
139
|
+
- !ruby/object:Gem::Version
|
140
|
+
version: '0'
|
127
141
|
description: This library provides integration of RubyMoney - Money gem with Rails
|
128
142
|
email:
|
129
143
|
- alup.rubymoney@gmail.com
|
@@ -135,7 +149,9 @@ files:
|
|
135
149
|
- LICENSE
|
136
150
|
- README.md
|
137
151
|
- Rakefile
|
152
|
+
- config/locales/money.da.yml
|
138
153
|
- config/locales/money.en.yml
|
154
|
+
- config/locales/money.ru.yml
|
139
155
|
- lib/generators/money_rails/initializer_generator.rb
|
140
156
|
- lib/generators/templates/money.rb
|
141
157
|
- lib/money-rails.rb
|
@@ -165,6 +181,7 @@ files:
|
|
165
181
|
- spec/configuration_spec.rb
|
166
182
|
- spec/dummy/README.rdoc
|
167
183
|
- spec/dummy/Rakefile
|
184
|
+
- spec/dummy/app/assets/config/manifest.js
|
168
185
|
- spec/dummy/app/assets/javascripts/application.js
|
169
186
|
- spec/dummy/app/assets/stylesheets/application.css
|
170
187
|
- spec/dummy/app/controllers/application_controller.rb
|
@@ -221,6 +238,7 @@ files:
|
|
221
238
|
- spec/dummy/script/rails
|
222
239
|
- spec/helpers/action_view_extension_spec.rb
|
223
240
|
- spec/helpers/form_helper_spec.rb
|
241
|
+
- spec/money_spec.rb
|
224
242
|
- spec/mongoid/mongoid_spec.rb
|
225
243
|
- spec/mongoid/two_spec.rb
|
226
244
|
- spec/spec_helper.rb
|
@@ -229,8 +247,11 @@ files:
|
|
229
247
|
homepage: https://github.com/RubyMoney/money-rails
|
230
248
|
licenses:
|
231
249
|
- MIT
|
232
|
-
metadata:
|
233
|
-
|
250
|
+
metadata:
|
251
|
+
changelog_uri: https://github.com/RubyMoney/money-rails/blob/master/CHANGELOG.md
|
252
|
+
source_code_uri: https://github.com/RubyMoney/money-rails/
|
253
|
+
bug_tracker_uri: https://github.com/RubyMoney/money-rails/issues
|
254
|
+
post_install_message:
|
234
255
|
rdoc_options: []
|
235
256
|
require_paths:
|
236
257
|
- lib
|
@@ -245,9 +266,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
245
266
|
- !ruby/object:Gem::Version
|
246
267
|
version: '0'
|
247
268
|
requirements: []
|
248
|
-
|
249
|
-
|
250
|
-
signing_key:
|
269
|
+
rubygems_version: 3.2.22
|
270
|
+
signing_key:
|
251
271
|
specification_version: 4
|
252
272
|
summary: Money gem integration with Rails
|
253
273
|
test_files:
|
@@ -257,6 +277,7 @@ test_files:
|
|
257
277
|
- spec/configuration_spec.rb
|
258
278
|
- spec/dummy/README.rdoc
|
259
279
|
- spec/dummy/Rakefile
|
280
|
+
- spec/dummy/app/assets/config/manifest.js
|
260
281
|
- spec/dummy/app/assets/javascripts/application.js
|
261
282
|
- spec/dummy/app/assets/stylesheets/application.css
|
262
283
|
- spec/dummy/app/controllers/application_controller.rb
|
@@ -313,6 +334,7 @@ test_files:
|
|
313
334
|
- spec/dummy/script/rails
|
314
335
|
- spec/helpers/action_view_extension_spec.rb
|
315
336
|
- spec/helpers/form_helper_spec.rb
|
337
|
+
- spec/money_spec.rb
|
316
338
|
- spec/mongoid/mongoid_spec.rb
|
317
339
|
- spec/mongoid/two_spec.rb
|
318
340
|
- spec/spec_helper.rb
|