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 +4 -4
- data/README.md +20 -20
- data/lib/money_attribute/migration_extensions/helper.rb +5 -1
- data/lib/money_attribute/migration_extensions/schema_statements.rb +2 -2
- data/lib/money_attribute/migration_extensions/table_definition.rb +2 -2
- data/lib/money_attribute/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 47ae4d496ececb42ec6ab60e278b3b523518c057074ead9610d90395ae4c5cb7
|
|
4
|
+
data.tar.gz: 3f26f8a306a4d64cf982ca0d079cdf7738dc10b3acaed2ccb2036b67755b83e9
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
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.
|
|
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 `
|
|
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.
|
|
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.
|
|
104
|
-
t.
|
|
105
|
-
t.
|
|
106
|
-
t.
|
|
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
|
-
|
|
115
|
-
|
|
116
|
-
|
|
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.
|
|
126
|
-
| `t.
|
|
127
|
-
| `t.
|
|
128
|
-
| `t.
|
|
129
|
-
| `t.
|
|
130
|
-
| `t.
|
|
131
|
-
| `t.
|
|
132
|
-
| `t.
|
|
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.
|
|
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.
|
|
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 =
|
|
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
|
|
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
|
|
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
|
|
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
|
|
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)
|