easy_rails_money 0.0.6 → 0.0.7
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.
- data/CHANGELOG.md +8 -0
- data/lib/easy_rails_money/active_record/migration/schema_statements.rb +14 -9
- data/lib/easy_rails_money/active_record/migration/table.rb +6 -6
- data/lib/easy_rails_money/active_record/migration/table_definition.rb +6 -6
- data/lib/easy_rails_money/version.rb +1 -1
- data/spec/active_record/migration_spec.rb +31 -2
- metadata +3 -3
data/CHANGELOG.md
CHANGED
@@ -38,4 +38,12 @@
|
|
38
38
|
EasyRailsMoney::ActiveRecord::MoneyDsl.single_currency?
|
39
39
|
is dependent on the database adapter being used, which sucks.
|
40
40
|
can test on other database adapters or handle a generic error
|
41
|
+
|
42
|
+
## 0.0.7
|
43
|
+
- money and currency column can take options
|
44
|
+
activerecord DSL for columns can take options for
|
45
|
+
not-null constraints and default. all options are
|
46
|
+
passed forward to activerecord
|
41
47
|
|
48
|
+
add_column and change_table do not work for now
|
49
|
+
with this syntax
|
@@ -1,3 +1,5 @@
|
|
1
|
+
require 'active_support/core_ext/array/extract_options'
|
2
|
+
|
1
3
|
module EasyRailsMoney
|
2
4
|
module ActiveRecord
|
3
5
|
module Migration
|
@@ -22,10 +24,11 @@ module EasyRailsMoney
|
|
22
24
|
# @see EasyRailsMoney::ActiveRecord::Migration::Table#money
|
23
25
|
# @see EasyRailsMoney::ActiveRecord::Migration::TableDefinition#currency
|
24
26
|
# @see EasyRailsMoney::ActiveRecord::Migration::SchemaStatements#remove_money
|
25
|
-
def add_money table_name, *
|
26
|
-
|
27
|
-
|
28
|
-
add_column table_name, "#{name}
|
27
|
+
def add_money table_name, *args
|
28
|
+
options = args.extract_options!
|
29
|
+
Array(args).each do |name|
|
30
|
+
add_column table_name, "#{name}_money", :integer, options
|
31
|
+
add_column table_name, "#{name}_currency", :string, options unless has_currency_column?(table_name)
|
29
32
|
end
|
30
33
|
end
|
31
34
|
|
@@ -44,12 +47,13 @@ module EasyRailsMoney
|
|
44
47
|
# @see EasyRailsMoney::ActiveRecord::Migration::SchemaStatements#add_money
|
45
48
|
# @see EasyRailsMoney::ActiveRecord::Migration::TableDefinition#currency
|
46
49
|
# @note multiple remove_money calls are not supported in one migration. because at this point the schema is different from the migration defined
|
47
|
-
def remove_money table_name, *
|
48
|
-
|
50
|
+
def remove_money table_name, *args
|
51
|
+
options = args.extract_options!
|
52
|
+
Array(args).each do |name|
|
49
53
|
remove_column table_name, "#{name}_money"
|
50
54
|
remove_column table_name, "#{name}_currency"
|
51
55
|
end
|
52
|
-
remove_currency table_name unless has_money_columns?(table_name)
|
56
|
+
remove_currency table_name, options unless has_money_columns?(table_name)
|
53
57
|
end
|
54
58
|
|
55
59
|
# Removes the common currency column
|
@@ -64,14 +68,15 @@ module EasyRailsMoney
|
|
64
68
|
# column is prefixed by '_money' and the currency column by '_currency'
|
65
69
|
#
|
66
70
|
# @param table_name [Symbol|String]
|
71
|
+
# @param options [Hash] optional
|
67
72
|
#
|
68
73
|
# @see EasyRailsMoney::ActiveRecord::Migration::Table#remove_currency
|
69
74
|
# @see EasyRailsMoney::ActiveRecord::Migration::SchemaStatements#remove_money
|
70
75
|
# @see EasyRailsMoney::ActiveRecord::Migration::TableDefinition#currency
|
71
|
-
def remove_currency table_name
|
76
|
+
def remove_currency table_name, options={}
|
72
77
|
remove_column table_name, :currency
|
73
78
|
money_columns(table_name) do |money_column|
|
74
|
-
add_column table_name, "#{money_column}_currency", :string
|
79
|
+
add_column table_name, "#{money_column}_currency", :string, options
|
75
80
|
end
|
76
81
|
end
|
77
82
|
|
@@ -24,10 +24,10 @@ module EasyRailsMoney
|
|
24
24
|
# @see EasyRailsMoney::ActiveRecord::Migration::TableDefinition#money
|
25
25
|
# @see EasyRailsMoney::ActiveRecord::Migration::TableDefinition#currency
|
26
26
|
# @see EasyRailsMoney::ActiveRecord::Migration::SchemaStatements#remove_money
|
27
|
-
def money(
|
28
|
-
column_names.each do |name|
|
29
|
-
column "#{name}_money", :integer
|
30
|
-
column "#{name}_currency", :string unless has_currency_column?
|
27
|
+
def money(column_names, options={})
|
28
|
+
Array(column_names).each do |name|
|
29
|
+
column "#{name}_money", :integer, options
|
30
|
+
column "#{name}_currency", :string, options unless has_currency_column?
|
31
31
|
end
|
32
32
|
end
|
33
33
|
|
@@ -60,9 +60,9 @@ module EasyRailsMoney
|
|
60
60
|
#
|
61
61
|
# Add a common currency column and remove the individual
|
62
62
|
# currrency columns if they exist
|
63
|
-
def currency
|
63
|
+
def currency options = {}
|
64
64
|
remove_currency_columns
|
65
|
-
column :currency, :string
|
65
|
+
column :currency, :string, options
|
66
66
|
end
|
67
67
|
|
68
68
|
# Removes the common currency column
|
@@ -14,9 +14,9 @@ module EasyRailsMoney
|
|
14
14
|
# @return is not important and can change
|
15
15
|
#
|
16
16
|
# @see EasyRailsMoney::ActiveRecord::Migration::SchemaStatements#remove_currency
|
17
|
-
def currency
|
17
|
+
def currency options={}
|
18
18
|
remove_currency_columns
|
19
|
-
column :currency, :string
|
19
|
+
column :currency, :string, options
|
20
20
|
end
|
21
21
|
|
22
22
|
# Creates one or two columns to represent a Money object in the named table
|
@@ -38,11 +38,11 @@ module EasyRailsMoney
|
|
38
38
|
# @see EasyRailsMoney::ActiveRecord::Migration::Table#money
|
39
39
|
# @see EasyRailsMoney::ActiveRecord::Migration::TableDefinition#currency
|
40
40
|
# @see EasyRailsMoney::ActiveRecord::Migration::SchemaStatements#remove_money
|
41
|
-
def money(
|
42
|
-
column_names.each do |name|
|
43
|
-
column "#{name}_money", :integer
|
41
|
+
def money(column_names, options={})
|
42
|
+
Array(column_names).each do |name|
|
43
|
+
column "#{name}_money", :integer, options
|
44
44
|
unless columns.select { |x| x.name == "currency" }.any?
|
45
|
-
column "#{name}_currency", :string
|
45
|
+
column "#{name}_currency", :string, options
|
46
46
|
end
|
47
47
|
end
|
48
48
|
end
|
@@ -147,6 +147,18 @@ module CreateTableDefinition
|
|
147
147
|
end
|
148
148
|
end
|
149
149
|
end
|
150
|
+
|
151
|
+
class CreateLoanWithConstraint < ActiveRecord::Migration
|
152
|
+
def change
|
153
|
+
suppress_messages do
|
154
|
+
create_table :loans, :force => true do |t|
|
155
|
+
t.string :name
|
156
|
+
t.currency :null => false
|
157
|
+
t.money :principal, :null => false
|
158
|
+
end
|
159
|
+
end
|
160
|
+
end
|
161
|
+
end
|
150
162
|
|
151
163
|
class CreateLoanWithCurrencySpecifiedFirst < ActiveRecord::Migration
|
152
164
|
def change
|
@@ -232,6 +244,16 @@ EOF
|
|
232
244
|
end
|
233
245
|
EOF
|
234
246
|
end
|
247
|
+
|
248
|
+
let(:schema_with_constraint) do
|
249
|
+
<<-EOF.strip_spaces
|
250
|
+
create_table "loans", :force => true do |t|
|
251
|
+
t.string "name"
|
252
|
+
t.integer "principal_money", :null => false
|
253
|
+
t.string "currency", :null => false
|
254
|
+
end
|
255
|
+
EOF
|
256
|
+
end
|
235
257
|
|
236
258
|
context "and testing schema statements", :schema_statements do
|
237
259
|
context "which have one currency column for each money column" do
|
@@ -286,12 +308,19 @@ EOF
|
|
286
308
|
end # context "schema_statements"
|
287
309
|
|
288
310
|
context "and testing table statements", :table_statements do
|
311
|
+
|
312
|
+
describe "#money" do
|
313
|
+
it "can create a schema with not-null constraints on columns", :constraint do
|
314
|
+
expect { migrate CreateTableDefinition::CreateLoanWithConstraint }.to change { dump_schema }.from("").to(schema_with_constraint)
|
315
|
+
end
|
316
|
+
end
|
317
|
+
|
289
318
|
context "which have one currency column for each money column" do
|
290
319
|
before(:each) do
|
291
320
|
migrate CreateTableDefinition::CreateLoanAndMoney
|
292
321
|
end
|
293
322
|
|
294
|
-
describe "#
|
323
|
+
describe "#money" do
|
295
324
|
it "creates two columns for each money attribute. one to store the lower denomination as an integer and the currency as a string" do
|
296
325
|
expect(dump_schema).to eq schema_with_principal
|
297
326
|
end
|
@@ -305,7 +334,7 @@ EOF
|
|
305
334
|
end
|
306
335
|
|
307
336
|
context "which has a single currency", :single_currency do
|
308
|
-
describe "#
|
337
|
+
describe "#money" do
|
309
338
|
context "and tests that order of statements does not matter" do
|
310
339
|
it "creates money and common currency cilumns when currency column is specified last" do
|
311
340
|
migrate CreateTableDefinition::CreateLoanWithCurrency
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: easy_rails_money
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.7
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -242,7 +242,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
242
242
|
version: '0'
|
243
243
|
segments:
|
244
244
|
- 0
|
245
|
-
hash: -
|
245
|
+
hash: -1659709329796999212
|
246
246
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
247
247
|
none: false
|
248
248
|
requirements:
|
@@ -251,7 +251,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
251
251
|
version: '0'
|
252
252
|
segments:
|
253
253
|
- 0
|
254
|
-
hash: -
|
254
|
+
hash: -1659709329796999212
|
255
255
|
requirements: []
|
256
256
|
rubyforge_project:
|
257
257
|
rubygems_version: 1.8.24
|