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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 4f4bf2077392ed177043cba461aa559d7b468c640a83f3b61ca614bc34293cef
4
- data.tar.gz: 4ed16b8fcc2d9496709928d2498e861102b98f1b2fd1d08059e5dc2a6a5d817a
3
+ metadata.gz: 8bac6640e3f2eedae550ac86db8f254b4717785dcc58c817f50284bbd15b242b
4
+ data.tar.gz: fe7e0f2e4c95c83cfdaa1502389f4576737881dc306019717b4171a92637613f
5
5
  SHA512:
6
- metadata.gz: fa848eaea24fd01e8ddfdef4bd6f8b16d4051f59804f94125f56fbc67c0f43106a316a61fa8d56aef0f6c1d5f8ba336160ed9e384f1e92c3428f9c59e5344138
7
- data.tar.gz: e39def5126ef32bb6920b9e24b94a895ff2da0711c4c389c1accf2a3d82ddcf4b6df1020f5ba2e6eca7420f3579822e2b725b486d667c1f3b65d893b6827dca8
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 ? money.to_s : nil
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 money_amount(method, options = {})
13
+ def money_amount_field(method, options = {})
14
14
  money_from_column = object.public_send(method)
15
- value = money_from_column ? money_from_column.to_s : nil
15
+ value = money_from_column&.to_d
16
16
 
17
- @template.text_field_tag(field_name(method), value,
18
- { id: field_id(method) }.merge(options))
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
- nil
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
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MoneyAttribute
4
- VERSION = '0.14.2'
4
+ VERSION = '0.14.4'
5
5
  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.2
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: []