money_attribute 0.14.2 → 0.14.4
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 +14 -0
- data/lib/money_attribute/core_ext.rb +4 -12
- data/lib/money_attribute/form_builder_extension.rb +5 -5
- data/lib/money_attribute/macro.rb +0 -1
- data/lib/money_attribute/railtie.rb +6 -2
- data/lib/money_attribute/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8bac6640e3f2eedae550ac86db8f254b4717785dcc58c817f50284bbd15b242b
|
|
4
|
+
data.tar.gz: fe7e0f2e4c95c83cfdaa1502389f4576737881dc306019717b4171a92637613f
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: a3a2eaeee424f0bca9dd1ead4a5c9dc5223cadfc9c1e492177bb261d1af9dcff4986307b56d74caf0ba09d796bc4553085c0746acbd015be583715e55e9f25d5
|
|
7
|
+
data.tar.gz: 182594cbfe4c9d8a7b4b286d4e0e3aaeb93699858a48b1184b136aef57cfcf89e64d871d4eb0604d0109521daba5cdfb85f15da364806328223c13760c56f6bf
|
data/README.md
CHANGED
|
@@ -380,6 +380,20 @@ MoneyAttribute adds small helpers on `Numeric` and `String`:
|
|
|
380
380
|
|
|
381
381
|
> If you prefer not to extend core classes, use `Mint.money(12, 'USD')` instead.
|
|
382
382
|
|
|
383
|
+
## Form helpers
|
|
384
|
+
|
|
385
|
+
MoneyAttribute adds `money_field` and `money_amount_field` to Rails form builders. `money_field` renders a text input with the locale-formatted money string; `money_amount_field` renders a number input with the raw decimal value.
|
|
386
|
+
|
|
387
|
+
```erb
|
|
388
|
+
<%= form_with model: @product do |form| %>
|
|
389
|
+
<%= form.label :price %>
|
|
390
|
+
<%= form.money_field :price %> <!-- text input, e.g. "$1,234.56" -->
|
|
391
|
+
|
|
392
|
+
<%= form.label :tax %>
|
|
393
|
+
<%= form.money_amount_field :tax %> <!-- number input, e.g. "1234.56" -->
|
|
394
|
+
<% end %>
|
|
395
|
+
```
|
|
396
|
+
|
|
383
397
|
## Roadmap
|
|
384
398
|
|
|
385
399
|
1. **Method-level currency** — lambda-based currency resolution for multi-tenant and instance-level scenarios
|
|
@@ -2,17 +2,11 @@
|
|
|
2
2
|
|
|
3
3
|
# :nodoc
|
|
4
4
|
class Numeric
|
|
5
|
-
def to_money(currency = MoneyAttribute.default_currency)
|
|
6
|
-
::Mint.money(self, currency)
|
|
7
|
-
end
|
|
5
|
+
def to_money(currency = MoneyAttribute.default_currency) = Mint.money(self, currency)
|
|
8
6
|
|
|
9
|
-
def dollars
|
|
10
|
-
::Mint.money(self, 'USD')
|
|
11
|
-
end
|
|
7
|
+
def dollars = Mint.money(self, 'USD')
|
|
12
8
|
|
|
13
|
-
def euros
|
|
14
|
-
::Mint.money(self, 'EUR')
|
|
15
|
-
end
|
|
9
|
+
def euros = Mint.money(self, 'EUR')
|
|
16
10
|
|
|
17
11
|
alias dollar dollars
|
|
18
12
|
alias euro euros
|
|
@@ -20,7 +14,5 @@ end
|
|
|
20
14
|
|
|
21
15
|
# :nodoc
|
|
22
16
|
class String
|
|
23
|
-
def to_money(currency = MoneyAttribute.default_currency)
|
|
24
|
-
::Mint.money(to_r, currency)
|
|
25
|
-
end
|
|
17
|
+
def to_money(currency = MoneyAttribute.default_currency) = Mint.parse(self, currency)
|
|
26
18
|
end
|
|
@@ -4,18 +4,18 @@ module MoneyAttribute
|
|
|
4
4
|
module FormBuilderExtension
|
|
5
5
|
def money_field(method, options = {})
|
|
6
6
|
money = object.public_send(method)
|
|
7
|
-
value = money
|
|
7
|
+
value = money&.to_s(:currency)
|
|
8
8
|
|
|
9
9
|
@template.text_field_tag(field_name(method), value,
|
|
10
10
|
{ id: field_id(method) }.merge(options))
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
-
def
|
|
13
|
+
def money_amount_field(method, options = {})
|
|
14
14
|
money_from_column = object.public_send(method)
|
|
15
|
-
value = money_from_column
|
|
15
|
+
value = money_from_column&.to_d
|
|
16
16
|
|
|
17
|
-
@template.
|
|
18
|
-
|
|
17
|
+
@template.number_field_tag(field_name(method), value,
|
|
18
|
+
{ id: field_id(method) }.merge(options))
|
|
19
19
|
end
|
|
20
20
|
end
|
|
21
21
|
end
|
|
@@ -8,7 +8,6 @@ module MoneyAttribute
|
|
|
8
8
|
def money_attribute(name, currency: MoneyAttribute.default_currency, mapping: nil)
|
|
9
9
|
columns = attribute_names
|
|
10
10
|
currency = ::Mint::Currency.resolve!(currency)
|
|
11
|
-
converter = Converter.new(currency)
|
|
12
11
|
name = name.to_s
|
|
13
12
|
resolved_mapping = mapping || resolve_mapping(name, columns)
|
|
14
13
|
|
|
@@ -49,8 +49,12 @@ module MoneyAttribute
|
|
|
49
49
|
code, subunit, symbol = *currency_data
|
|
50
50
|
end
|
|
51
51
|
::Mint::Currency.register(code:, subunit:, symbol:)
|
|
52
|
-
rescue KeyError
|
|
53
|
-
|
|
52
|
+
rescue KeyError => e
|
|
53
|
+
unless e.message.include?('already registered')
|
|
54
|
+
raise ArgumentError,
|
|
55
|
+
"Invalid currency configuration: #{currency_data.inspect}. " \
|
|
56
|
+
"Each currency must have :currency, :subunit, and :symbol keys. Error: #{e.message}"
|
|
57
|
+
end
|
|
54
58
|
end
|
|
55
59
|
end
|
|
56
60
|
end
|
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: 0.14.
|
|
4
|
+
version: 0.14.4
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Gilson Ferraz
|
|
@@ -37,7 +37,7 @@ dependencies:
|
|
|
37
37
|
- - ">="
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
39
|
version: 7.1.3.2
|
|
40
|
-
description:
|
|
40
|
+
description: Easily add money attributes to your Rails models
|
|
41
41
|
email:
|
|
42
42
|
- gilson@cesar.etc.br
|
|
43
43
|
executables: []
|