money_attribute 0.12.0 → 0.13.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 306761efbc2f4f645f3e975ea830ff26901bc4ba5fac664346f5d7b5720050b8
4
- data.tar.gz: 13dfc1b02f256f22ac1027700164ae57cee60bc77e1b233c28549b20988adaf7
3
+ metadata.gz: 47ae4d496ececb42ec6ab60e278b3b523518c057074ead9610d90395ae4c5cb7
4
+ data.tar.gz: 3f26f8a306a4d64cf982ca0d079cdf7738dc10b3acaed2ccb2036b67755b83e9
5
5
  SHA512:
6
- metadata.gz: 3d39e999c298070baf49359ed226f9582defac099e4da3b342f3b367df81c658575e973095a0c8de4a19d8c7116af778d38160de6dbb263fd050a6a36bb3f0a9
7
- data.tar.gz: 02c399705be0e9ac78c3f9dd1bbe9603055d6702df60a43e12e57fb0fa16ecfd4212d129a91294bdb4323f487030def2bc3fe52a7484552b93c4f2e81b6a71ed
6
+ metadata.gz: d2ef7b83fee1736f8944ebfcec5348b7c69a9b15f018b8080689c30dd4387aa2cfa10b4a459bf3ff36a117f2c22a7ce1e98909e2e6a5d3e56eada694c46b0080
7
+ data.tar.gz: 8e1da01132bb2309f0c5fc10ec00648a035c761e6ca3f3fa7ac43f3ed7e19d324831f8142d939225aab7c273308507c428a115509b911e2c1944cbfc12ae704e
data/README.md CHANGED
@@ -27,7 +27,7 @@ class CreateProducts < ActiveRecord::Migration[8.1]
27
27
  def change
28
28
  create_table :products do |t|
29
29
  t.string :name
30
- t.money :price
30
+ t.money_attribute :price
31
31
  t.timestamps
32
32
  end
33
33
  end
@@ -91,19 +91,19 @@ The generator creates `config/initializers/money_attribute.rb`.
91
91
 
92
92
  ## Migration helpers
93
93
 
94
- MoneyAttribute adds `add_money` / `remove_money` for existing tables and `t.money` / `t.remove_money` for `create_table` / `change_table` blocks.
94
+ MoneyAttribute adds `add_money_attribute` / `remove_money_attribute` for existing tables and `t.money_attribute` / `t.remove_money_attribute` for `create_table` / `change_table` blocks.
95
95
 
96
- By default `t.money :price` creates a `decimal(16,4)` amount column and a `string` currency column — both nullable, no default. Pass `amount: { type: :integer }` to store subunits instead, or `currency: false` to skip the currency column entirely.
96
+ By default `t.money_attribute :price` creates a `decimal(16,4)` amount column and a `string` currency column — both nullable, no default. Pass `amount: { type: :integer }` to store subunits instead, or `currency: false` to skip the currency column entirely.
97
97
 
98
98
  ```ruby
99
99
  class CreateProducts < ActiveRecord::Migration[8.1]
100
100
  def change
101
101
  create_table :products do |t|
102
102
  t.string :name
103
- t.money :price # price (decimal) + price_currency (string)
104
- t.money :price_amount # price_amount + price_currency (strips _amount suffix)
105
- t.money :fee, currency: false # single column, no currency
106
- t.money :tax, amount: { type: :bigint } # bigint amount + currency
103
+ t.money_attribute :price # price (decimal) + price_currency (string)
104
+ t.money_attribute :price_amount # price_amount + price_currency (strips _amount suffix)
105
+ t.money_attribute :fee, currency: false # single column, no currency
106
+ t.money_attribute :tax, amount: { type: :bigint } # bigint amount + currency
107
107
  t.timestamps
108
108
  end
109
109
  end
@@ -111,9 +111,9 @@ end
111
111
 
112
112
  class AddPriceToProducts < ActiveRecord::Migration[8.1]
113
113
  def change
114
- add_money :products, :price # add price + price_currency
115
- add_money :products, :discount, amount: { type: :integer }
116
- remove_money :products, :obsolete_fee # reversible in change
114
+ add_money_attribute :products, :price # add price + price_currency
115
+ add_money_attribute :products, :discount, amount: { type: :integer }
116
+ remove_money_attribute :products, :obsolete_fee # reversible in change
117
117
  end
118
118
  end
119
119
  ```
@@ -122,20 +122,20 @@ end
122
122
 
123
123
  | Migration call | Columns created | Model declaration |
124
124
  |---|---|---|
125
- | `t.money :price` | `price` decimal + `price_currency` string | `money_attribute :price` |
126
- | `t.money :price_amount` | `price_amount` decimal + `price_currency` string | `money_attribute :price` |
127
- | `t.money :price, currency: false` | `price` decimal | `money_attribute :price` |
128
- | `t.money :price, amount: { type: :integer }` | `price` integer + `price_currency` string | `money_attribute :price` |
129
- | `t.money :price, amount: { column: :a }, currency: { column: :c }` | `a` + `c` | `money_attribute :price, mapping: { amount: :a, currency: :c }` |
130
- | `t.money :price, currency: { limit: 3 }` | `price` decimal + `price_currency` string(3) | `money_attribute :price` |
131
- | `t.money :price, amount: { precision: 14, scale: 2, null: false }, currency: { limit: 3, default: 'USD' }` | `price` decimal(14,2) NOT NULL + `price_currency` string(3) DEFAULT 'USD' | `money_attribute :price` |
132
- | `t.remove_money :price` | Removes `price` + `price_currency` | `money_attribute :price` |
125
+ | `t.money_attribute :price` | `price` decimal + `price_currency` string | `money_attribute :price` |
126
+ | `t.money_attribute :price_amount` | `price_amount` decimal + `price_currency` string | `money_attribute :price` |
127
+ | `t.money_attribute :price, currency: false` | `price` decimal | `money_attribute :price` |
128
+ | `t.money_attribute :price, amount: { type: :integer }` | `price` integer + `price_currency` string | `money_attribute :price` |
129
+ | `t.money_attribute :price, amount: { column: :a }, currency: { column: :c }` | `a` + `c` | `money_attribute :price, mapping: { amount: :a, currency: :c }` |
130
+ | `t.money_attribute :price, currency: { limit: 3 }` | `price` decimal + `price_currency` string(3) | `money_attribute :price` |
131
+ | `t.money_attribute :price, amount: { precision: 14, scale: 2, null: false }, currency: { limit: 3, default: 'USD' }` | `price` decimal(14,2) NOT NULL + `price_currency` string(3) DEFAULT 'USD' | `money_attribute :price` |
132
+ | `t.remove_money_attribute :price` | Removes `price` + `price_currency` | `money_attribute :price` |
133
133
 
134
134
  Inside `change_table`:
135
135
 
136
136
  ```ruby
137
137
  change_table :products do |t|
138
- t.remove_money :obsolete_fee # removes obsolete_fee + obsolete_fee_currency
138
+ t.remove_money_attribute :obsolete_fee # removes obsolete_fee + obsolete_fee_currency
139
139
  end
140
140
  ```
141
141
 
@@ -201,7 +201,7 @@ If none of those keys are set, `format` is used as a plain string (simple format
201
201
 
202
202
  | | Fixed currency (single column) | Multi-currency (amount + currency) |
203
203
  |---|---|---|
204
- | **Migration** | `t.money :price` (or `t.decimal :price`) | `t.money :price` (or `t.decimal :price_amount` + `t.string :price_currency`) |
204
+ | **Migration** | `t.money_attribute :price` (or `t.decimal :price`) | `t.money_attribute :price` (or `t.decimal :price_amount` + `t.string :price_currency`) |
205
205
  | **Model** | `money_attribute :price, currency: 'USD'` | `money_attribute :price` |
206
206
  | **When to use** | Column always holds the same currency | Each row can hold a different currency |
207
207
  | **Column type** | `decimal`, `integer`, or `bigint` | `decimal`, `integer`, or `bigint` for amount; `string` for currency |
@@ -34,7 +34,11 @@ module MoneyAttribute
34
34
  end
35
35
 
36
36
  stripped = name.end_with?('_amount') ? name.sub(/_amount$/, '') : name
37
- default_currency_col = "#{stripped}_currency"
37
+ default_currency_col = if name == 'amount' && !(options[:currency].is_a?(Hash) && options[:currency][:column])
38
+ 'currency'
39
+ else
40
+ "#{stripped}_currency"
41
+ end
38
42
 
39
43
  if options.key?(:currency) && options[:currency].is_a?(Hash)
40
44
  currency_col = options[:currency][:column]&.to_s || default_currency_col
@@ -7,14 +7,14 @@ module MoneyAttribute
7
7
  module SchemaStatements
8
8
  include Helper
9
9
 
10
- def add_money(table_name, accessor, options = {})
10
+ def add_money_attribute(table_name, accessor, options = {})
11
11
  amount_col, currency_col, amount_opts, currency_opts = parse_money_args(accessor, options)
12
12
 
13
13
  add_column(table_name, amount_col, amount_opts[:type], **amount_opts.except(:type))
14
14
  add_column(table_name, currency_col, :string, **currency_opts) if currency_col
15
15
  end
16
16
 
17
- def remove_money(table_name, accessor, options = {})
17
+ def remove_money_attribute(table_name, accessor, options = {})
18
18
  amount_col, currency_col, = parse_money_args(accessor, options)
19
19
 
20
20
  remove_column(table_name, amount_col)
@@ -7,14 +7,14 @@ module MoneyAttribute
7
7
  module TableDefinition
8
8
  include Helper
9
9
 
10
- def money(accessor, options = {})
10
+ def money_attribute(accessor, options = {})
11
11
  amount_col, currency_col, amount_opts, currency_opts = parse_money_args(accessor, options)
12
12
 
13
13
  column(amount_col, amount_opts[:type], **amount_opts.except(:type))
14
14
  column(currency_col, :string, **currency_opts) if currency_col
15
15
  end
16
16
 
17
- def remove_money(accessor, options = {})
17
+ def remove_money_attribute(accessor, options = {})
18
18
  amount_col, currency_col, = parse_money_args(accessor, options)
19
19
 
20
20
  remove_column(amount_col)
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module MoneyAttribute
4
- VERSION = '0.12.0'
4
+ VERSION = '0.13.0'
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.12.0
4
+ version: 0.13.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Gilson Ferraz