money_attribute 1.2.0 → 1.3.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 +4 -4
- data/README.md +166 -72
- data/Rakefile +31 -6
- data/lib/generators/money_attribute/initializer_generator.rb +10 -0
- data/lib/money_attribute/attribute_spec.rb +9 -3
- data/lib/money_attribute/attribute_spec_registry.rb +63 -5
- data/lib/money_attribute/column_type_validations.rb +47 -0
- data/lib/money_attribute/converter.rb +18 -3
- data/lib/money_attribute/core_ext/numeric.rb +10 -1
- data/lib/money_attribute/core_ext/string.rb +12 -2
- data/lib/money_attribute/form_builder_extension.rb +48 -3
- data/lib/money_attribute/macro.rb +96 -7
- data/lib/money_attribute/migration_extensions/helper.rb +61 -1
- data/lib/money_attribute/migration_extensions/schema_statements.rb +89 -9
- data/lib/money_attribute/migration_extensions/table_definition.rb +68 -11
- data/lib/money_attribute/money_amount.rb +30 -2
- data/lib/money_attribute/query/amount_condition.rb +139 -2
- data/lib/money_attribute/query/amount_order.rb +9 -3
- data/lib/money_attribute/query/currency_condition.rb +8 -2
- data/lib/money_attribute/query/helpers.rb +28 -5
- data/lib/money_attribute/query/pick.rb +11 -8
- data/lib/money_attribute/query/pluck.rb +11 -15
- data/lib/money_attribute/query/sum.rb +15 -3
- data/lib/money_attribute/query.rb +16 -11
- data/lib/money_attribute/railtie.rb +40 -0
- data/lib/money_attribute/type.rb +6 -2
- data/lib/money_attribute/version.rb +2 -1
- data/lib/money_attribute.rb +1 -0
- metadata +5 -4
|
@@ -1,6 +1,21 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
module MoneyAttribute
|
|
4
|
+
# Rails engine integration.
|
|
5
|
+
#
|
|
6
|
+
# On boot, includes the migration and form-builder extensions into the
|
|
7
|
+
# corresponding Rails classes, wires Mint's locale backend to Rails i18n,
|
|
8
|
+
# and registers custom currencies declared in the initializer.
|
|
9
|
+
#
|
|
10
|
+
# @example Generated initializer
|
|
11
|
+
# MoneyAttribute.configure do |config|
|
|
12
|
+
# config.default_currency = 'BRL'
|
|
13
|
+
# config.added_currencies = [
|
|
14
|
+
# { currency: 'BTB', subunit: 8, symbol: '₿' }
|
|
15
|
+
# ]
|
|
16
|
+
# end
|
|
17
|
+
#
|
|
18
|
+
# @api private
|
|
4
19
|
class Railtie < ::Rails::Railtie
|
|
5
20
|
generators do
|
|
6
21
|
require 'generators/money_attribute/initializer_generator'
|
|
@@ -21,17 +36,27 @@ module MoneyAttribute
|
|
|
21
36
|
end
|
|
22
37
|
|
|
23
38
|
# Configures Mint to use the Rails locale currency format.
|
|
39
|
+
#
|
|
40
|
+
# @return [void]
|
|
41
|
+
# @api private
|
|
24
42
|
def self.setup_locale_backend!
|
|
25
43
|
::Mint.locale_backend = method(:build_locale_format).to_proc
|
|
26
44
|
end
|
|
27
45
|
|
|
28
46
|
# Builds the locale-aware currency formatting hash.
|
|
47
|
+
#
|
|
48
|
+
# @return [Hash] the +:decimal+, +:thousand+, and +:format+ keys for Mint
|
|
49
|
+
# @api private
|
|
29
50
|
def self.build_locale_format
|
|
30
51
|
fmt = I18n.t('number.currency.format', default: {})
|
|
31
52
|
{ decimal: fmt[:separator], thousand: fmt[:delimiter], format: build_format(fmt) }
|
|
32
53
|
end
|
|
33
54
|
|
|
34
55
|
# Builds the final currency format string or hash for Mint.
|
|
56
|
+
#
|
|
57
|
+
# @param fmt [Hash] the Rails currency format settings
|
|
58
|
+
# @return [String, Hash] a single format string or a per-sign format hash
|
|
59
|
+
# @api private
|
|
35
60
|
def self.build_format(fmt)
|
|
36
61
|
if %i[positive negative zero].any? { |k| fmt.key?(k) }
|
|
37
62
|
build_hash_format(fmt)
|
|
@@ -41,6 +66,10 @@ module MoneyAttribute
|
|
|
41
66
|
end
|
|
42
67
|
|
|
43
68
|
# Builds a per-sign currency format hash.
|
|
69
|
+
#
|
|
70
|
+
# @param fmt [Hash] the Rails currency format settings
|
|
71
|
+
# @return [Hash] the +:positive+, +:negative+, and +:zero+ format strings
|
|
72
|
+
# @api private
|
|
44
73
|
def self.build_hash_format(fmt)
|
|
45
74
|
{
|
|
46
75
|
positive: translate_format(fmt[:positive] || fmt[:format]),
|
|
@@ -50,11 +79,22 @@ module MoneyAttribute
|
|
|
50
79
|
end
|
|
51
80
|
|
|
52
81
|
# Translates Rails currency placeholders into Mint placeholders.
|
|
82
|
+
#
|
|
83
|
+
# @param str [String, nil] the Rails format string
|
|
84
|
+
# @return [String] the translated format string
|
|
85
|
+
# @api private
|
|
53
86
|
def self.translate_format(str)
|
|
54
87
|
str.to_s.gsub('%n', '%<amount>f').gsub('%u', '%<symbol>s')
|
|
55
88
|
end
|
|
56
89
|
|
|
57
90
|
# Registers custom currencies configured by the application.
|
|
91
|
+
#
|
|
92
|
+
# Accepts hashes with +:currency+, +:subunit+, +:symbol+ keys or the
|
|
93
|
+
# matching positional array form. Already-registered currencies are skipped.
|
|
94
|
+
#
|
|
95
|
+
# @return [void]
|
|
96
|
+
# @raise [ArgumentError] if a currency hash is missing a required key
|
|
97
|
+
# @api private
|
|
58
98
|
def self.register_custom_currencies!
|
|
59
99
|
Array(MoneyAttribute.config.added_currencies).each do |currency_data|
|
|
60
100
|
if currency_data.respond_to?(:values_at)
|
data/lib/money_attribute/type.rb
CHANGED
|
@@ -7,7 +7,11 @@ module MoneyAttribute
|
|
|
7
7
|
#
|
|
8
8
|
# @param value [String, Numeric, Mint::Money, nil] the input value
|
|
9
9
|
# @return [Mint::Money, Numeric, nil] a Money value for strings, otherwise delegates to super
|
|
10
|
-
def cast(value)
|
|
10
|
+
def cast(value)
|
|
11
|
+
return Money.parse(value, default_currency: MoneyAttribute.default_currency) if value.is_a?(String)
|
|
12
|
+
|
|
13
|
+
super
|
|
14
|
+
end
|
|
11
15
|
|
|
12
16
|
# Validates that the value is compatible with the fixed currency type.
|
|
13
17
|
#
|
|
@@ -50,7 +54,7 @@ module MoneyAttribute
|
|
|
50
54
|
#
|
|
51
55
|
# @param value [BigDecimal, nil] the raw database value
|
|
52
56
|
# @return [Mint::Money, nil]
|
|
53
|
-
def deserialize(value) = value
|
|
57
|
+
def deserialize(value) = value&.to_money(MoneyAttribute.default_currency)
|
|
54
58
|
|
|
55
59
|
# Serializes a +Mint::Money+ value into a decimal.
|
|
56
60
|
#
|
data/lib/money_attribute.rb
CHANGED
|
@@ -15,6 +15,7 @@ require 'money_attribute/core_ext/numeric'
|
|
|
15
15
|
require 'money_attribute/core_ext/string'
|
|
16
16
|
require 'money_attribute/configuration'
|
|
17
17
|
require 'money_attribute/current'
|
|
18
|
+
require 'money_attribute/column_type_validations'
|
|
18
19
|
require 'money_attribute/attribute_spec'
|
|
19
20
|
require 'money_attribute/attribute_spec_registry'
|
|
20
21
|
require 'money_attribute/macro'
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: money_attribute
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 1.
|
|
4
|
+
version: 1.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gilson Ferraz
|
|
@@ -15,14 +15,14 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version: '2.
|
|
18
|
+
version: '2.1'
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - ">="
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version: '2.
|
|
25
|
+
version: '2.1'
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: rails
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -52,6 +52,7 @@ files:
|
|
|
52
52
|
- lib/money_attribute.rb
|
|
53
53
|
- lib/money_attribute/attribute_spec.rb
|
|
54
54
|
- lib/money_attribute/attribute_spec_registry.rb
|
|
55
|
+
- lib/money_attribute/column_type_validations.rb
|
|
55
56
|
- lib/money_attribute/configuration.rb
|
|
56
57
|
- lib/money_attribute/converter.rb
|
|
57
58
|
- lib/money_attribute/core_ext/numeric.rb
|
|
@@ -99,7 +100,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
99
100
|
- !ruby/object:Gem::Version
|
|
100
101
|
version: '0'
|
|
101
102
|
requirements: []
|
|
102
|
-
rubygems_version: 4.0.
|
|
103
|
+
rubygems_version: 4.0.18
|
|
103
104
|
specification_version: 4
|
|
104
105
|
summary: Money attributes for ActiveRecord
|
|
105
106
|
test_files: []
|