money_attribute 0.14.2 → 0.14.5

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: cd68138109c717791722388f0465c0bbd5296ffe2f30f2cb0521a74585f72483
4
+ data.tar.gz: 35b179441b1a9e8ef462fffcb0f9c4079929e3a1da6f775e82c67586dd952f2f
5
5
  SHA512:
6
- metadata.gz: fa848eaea24fd01e8ddfdef4bd6f8b16d4051f59804f94125f56fbc67c0f43106a316a61fa8d56aef0f6c1d5f8ba336160ed9e384f1e92c3428f9c59e5344138
7
- data.tar.gz: e39def5126ef32bb6920b9e24b94a895ff2da0711c4c389c1accf2a3d82ddcf4b6df1020f5ba2e6eca7420f3579822e2b725b486d667c1f3b65d893b6827dca8
6
+ metadata.gz: '058e831db4a2bf193eda3117e83271c7b8ce7fa0e1be8a24dcd810e988cf7afd24fb7ed05f752a3f2368888f8051e9c117cd7311338a87a011cec80c783d2c12'
7
+ data.tar.gz: 24346e562f17164b1c1a478eca95b3e4eb38e023112a80bc5e626c6efffaf679c86ebda193fdac5f721f23304c66fcb4c4c54e68cdcb64f990bfaf88f7d9b5ca
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
@@ -10,6 +10,8 @@ module MoneyAttribute
10
10
  end
11
11
  end
12
12
 
13
+ # Class-level caches — written during Rails boot (single-threaded),
14
+ # read-only during request handling. Safe without synchronization.
13
15
  def self.config
14
16
  @config ||= Configuration.new
15
17
  end
@@ -10,7 +10,8 @@ module MoneyAttribute
10
10
  case amount
11
11
  when Mint::Money, NilClass then amount
12
12
  when Numeric then Mint::Money.from(amount, currency)
13
- else Mint.parse(amount, currency)
13
+ when String then Mint.parse(amount, currency)
14
+ else raise ArgumentError, "Cannot convert #{amount.inspect} (#{amount.class}) to Money"
14
15
  end
15
16
  end
16
17
  alias call parse
@@ -2,25 +2,14 @@
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
+ remove_method :to_money
8
6
 
9
- def dollars
10
- ::Mint.money(self, 'USD')
11
- end
12
-
13
- def euros
14
- ::Mint.money(self, 'EUR')
15
- end
16
-
17
- alias dollar dollars
18
- alias euro euros
7
+ def to_money(currency = MoneyAttribute.default_currency) = Mint.money(self, currency)
19
8
  end
20
9
 
21
10
  # :nodoc
22
11
  class String
23
- def to_money(currency = MoneyAttribute.default_currency)
24
- ::Mint.money(to_r, currency)
25
- end
12
+ remove_method :to_money
13
+
14
+ def to_money(currency = MoneyAttribute.default_currency) = Mint.parse(self, currency)
26
15
  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_fs(: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.5'
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.5
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: []