immigrant 0.1.4 → 0.1.5
Sign up to get free protection for your applications and to get access to all the features.
- data/README.rdoc +1 -1
- data/lib/immigrant.rb +24 -4
- data/test/helper.rb +1 -0
- data/test/immigrant_test.rb +53 -35
- metadata +46 -67
data/README.rdoc
CHANGED
data/lib/immigrant.rb
CHANGED
@@ -40,7 +40,7 @@ module Immigrant
|
|
40
40
|
|
41
41
|
def model_classes
|
42
42
|
classes = []
|
43
|
-
ActiveRecord::Base.
|
43
|
+
ActiveRecord::Base.descendants.each do |model|
|
44
44
|
classes << model.name.constantize
|
45
45
|
end
|
46
46
|
classes
|
@@ -101,18 +101,19 @@ module Immigrant
|
|
101
101
|
reflection.klass.table_name, klass.table_name,
|
102
102
|
:column => reflection.send(fk_method).to_s,
|
103
103
|
:primary_key => klass.primary_key.to_s,
|
104
|
-
:dependent => [:delete, :delete_all].include?(reflection.options[:dependent]) && reflection
|
104
|
+
:dependent => [:delete, :delete_all].include?(reflection.options[:dependent]) && !qualified_reflection?(reflection, klass) ? :delete : nil
|
105
105
|
)
|
106
106
|
when :has_and_belongs_to_many
|
107
|
+
join_table = (reflection.respond_to?(:join_table) ? reflection.join_table : reflection.options[:join_table]).to_s
|
107
108
|
[
|
108
109
|
Foreigner::ConnectionAdapters::ForeignKeyDefinition.new(
|
109
|
-
|
110
|
+
join_table, klass.table_name,
|
110
111
|
:column => reflection.send(fk_method).to_s,
|
111
112
|
:primary_key => klass.primary_key.to_s,
|
112
113
|
:dependent => nil
|
113
114
|
),
|
114
115
|
Foreigner::ConnectionAdapters::ForeignKeyDefinition.new(
|
115
|
-
|
116
|
+
join_table, reflection.klass.table_name,
|
116
117
|
:column => reflection.association_foreign_key.to_s,
|
117
118
|
:primary_key => reflection.klass.primary_key.to_s,
|
118
119
|
:dependent => nil
|
@@ -125,6 +126,25 @@ module Immigrant
|
|
125
126
|
}.flatten
|
126
127
|
end
|
127
128
|
|
129
|
+
def qualified_reflection?(reflection, klass)
|
130
|
+
if ActiveRecord::VERSION::STRING < '4.'
|
131
|
+
reflection.options[:conditions].present?
|
132
|
+
else
|
133
|
+
scope = reflection.scope
|
134
|
+
if scope.nil?
|
135
|
+
false
|
136
|
+
elsif scope.respond_to?(:options)
|
137
|
+
scope.options[:where].present?
|
138
|
+
else
|
139
|
+
klass.instance_exec(*([nil]*scope.arity), &scope).where_values.present?
|
140
|
+
end
|
141
|
+
end
|
142
|
+
rescue
|
143
|
+
# if there's an error evaluating the scope block or whatever, just
|
144
|
+
# err on the side of caution and assume there are conditions
|
145
|
+
true
|
146
|
+
end
|
147
|
+
|
128
148
|
end
|
129
149
|
end
|
130
150
|
|
data/test/helper.rb
CHANGED
data/test/immigrant_test.rb
CHANGED
@@ -13,14 +13,31 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
13
13
|
@connection ||= MockConnection.new
|
14
14
|
end
|
15
15
|
end
|
16
|
+
|
17
|
+
if ActiveRecord::VERSION::STRING >= '4.'
|
18
|
+
# support old 3.x syntax for the sake of concise tests
|
19
|
+
[:belongs_to, :has_one, :has_many, :has_and_belongs_to_many].each do |method|
|
20
|
+
instance_eval <<-CODE
|
21
|
+
def self.#{method}(assoc, options = {})
|
22
|
+
args = [assoc]
|
23
|
+
scope = options.extract!(:conditions, :order)
|
24
|
+
if scope
|
25
|
+
args.push lambda{ where(scope[:conditions]).order(scope[:order]) }
|
26
|
+
end
|
27
|
+
args.push options
|
28
|
+
super *args
|
29
|
+
end
|
30
|
+
CODE
|
31
|
+
end
|
32
|
+
end
|
16
33
|
end
|
17
|
-
|
34
|
+
|
18
35
|
class MockConnection
|
19
36
|
def supports_primary_key? # AR <3.2
|
20
37
|
true
|
21
38
|
end
|
22
39
|
def primary_key(table)
|
23
|
-
table !~ /s_.*s\z/ ? 'id' : nil
|
40
|
+
table.to_s !~ /s_.*s\z/ ? 'id' : nil
|
24
41
|
end
|
25
42
|
end
|
26
43
|
|
@@ -32,16 +49,15 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
32
49
|
subclasses.replace([])
|
33
50
|
end
|
34
51
|
|
35
|
-
|
36
52
|
# basic scenarios
|
37
|
-
|
53
|
+
|
38
54
|
test 'belongs_to should generate a foreign key' do
|
39
55
|
class Author < MockModel; end
|
40
56
|
class Book < MockModel
|
41
57
|
belongs_to :guy, :class_name => 'Author', :foreign_key => 'author_id'
|
42
58
|
end
|
43
59
|
|
44
|
-
keys = Immigrant.infer_keys([]
|
60
|
+
keys = Immigrant.infer_keys([]).first
|
45
61
|
assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
|
46
62
|
assert_equal(
|
47
63
|
[foreign_key_definition(
|
@@ -58,7 +74,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
58
74
|
end
|
59
75
|
class Book < MockModel; end
|
60
76
|
|
61
|
-
keys = Immigrant.infer_keys([]
|
77
|
+
keys = Immigrant.infer_keys([]).first
|
62
78
|
assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
|
63
79
|
assert_equal(
|
64
80
|
[foreign_key_definition(
|
@@ -75,7 +91,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
75
91
|
end
|
76
92
|
class Book < MockModel; end
|
77
93
|
|
78
|
-
keys = Immigrant.infer_keys([]
|
94
|
+
keys = Immigrant.infer_keys([]).first
|
79
95
|
assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
|
80
96
|
assert_equal(
|
81
97
|
[foreign_key_definition(
|
@@ -92,7 +108,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
92
108
|
end
|
93
109
|
class Book < MockModel; end
|
94
110
|
|
95
|
-
keys = Immigrant.infer_keys([]
|
111
|
+
keys = Immigrant.infer_keys([]).first
|
96
112
|
assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
|
97
113
|
assert_equal(
|
98
114
|
[foreign_key_definition(
|
@@ -109,7 +125,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
109
125
|
end
|
110
126
|
class Book < MockModel; end
|
111
127
|
|
112
|
-
keys = Immigrant.infer_keys([]
|
128
|
+
keys = Immigrant.infer_keys([]).first
|
113
129
|
assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
|
114
130
|
assert_equal(
|
115
131
|
[foreign_key_definition(
|
@@ -126,7 +142,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
126
142
|
end
|
127
143
|
class Fan < MockModel; end
|
128
144
|
|
129
|
-
keys = Immigrant.infer_keys([]
|
145
|
+
keys = Immigrant.infer_keys([]).first
|
130
146
|
assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
|
131
147
|
assert_equal(
|
132
148
|
[foreign_key_definition(
|
@@ -143,19 +159,19 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
143
159
|
|
144
160
|
test 'has_and_belongs_to_many should respect the join_table' do
|
145
161
|
class Author < MockModel
|
146
|
-
has_and_belongs_to_many :fans, :join_table => :
|
162
|
+
has_and_belongs_to_many :fans, :join_table => :lols_wuts
|
147
163
|
end
|
148
164
|
class Fan < MockModel; end
|
149
165
|
|
150
|
-
keys = Immigrant.infer_keys([]
|
166
|
+
keys = Immigrant.infer_keys([]).first
|
151
167
|
assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
|
152
168
|
assert_equal(
|
153
169
|
[foreign_key_definition(
|
154
|
-
'
|
170
|
+
'lols_wuts', 'authors',
|
155
171
|
:column => 'author_id', :primary_key => 'id', :dependent => nil
|
156
172
|
),
|
157
173
|
foreign_key_definition(
|
158
|
-
'
|
174
|
+
'lols_wuts', 'fans',
|
159
175
|
:column => 'fan_id', :primary_key => 'id', :dependent => nil
|
160
176
|
)],
|
161
177
|
keys
|
@@ -171,7 +187,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
171
187
|
class Book < MockModel; end
|
172
188
|
class Article < MockModel; end
|
173
189
|
|
174
|
-
keys = Immigrant.infer_keys([]
|
190
|
+
keys = Immigrant.infer_keys([]).first
|
175
191
|
assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
|
176
192
|
assert_equal(
|
177
193
|
[foreign_key_definition(
|
@@ -188,7 +204,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
188
204
|
|
189
205
|
|
190
206
|
# (no) duplication
|
191
|
-
|
207
|
+
|
192
208
|
test 'STI should not generate duplicate foreign keys' do
|
193
209
|
class Company < MockModel; end
|
194
210
|
class Employee < MockModel
|
@@ -197,7 +213,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
197
213
|
class Manager < Employee; end
|
198
214
|
|
199
215
|
assert(Manager.reflections.present?)
|
200
|
-
keys = Immigrant.infer_keys([]
|
216
|
+
keys = Immigrant.infer_keys([]).first
|
201
217
|
assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
|
202
218
|
assert_equal(
|
203
219
|
[foreign_key_definition(
|
@@ -216,7 +232,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
216
232
|
belongs_to :author
|
217
233
|
end
|
218
234
|
|
219
|
-
keys = Immigrant.infer_keys([]
|
235
|
+
keys = Immigrant.infer_keys([]).first
|
220
236
|
assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
|
221
237
|
assert_equal(
|
222
238
|
[foreign_key_definition(
|
@@ -235,7 +251,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
235
251
|
end
|
236
252
|
class Book < MockModel; end
|
237
253
|
|
238
|
-
keys = Immigrant.infer_keys([]
|
254
|
+
keys = Immigrant.infer_keys([]).first
|
239
255
|
assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
|
240
256
|
assert_equal(
|
241
257
|
[foreign_key_definition(
|
@@ -270,23 +286,25 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
270
286
|
class Book < MockModel; end
|
271
287
|
class Article < MockModel; end
|
272
288
|
|
273
|
-
keys = Immigrant.infer_keys(database_keys
|
289
|
+
keys = Immigrant.infer_keys(database_keys).first
|
274
290
|
assert_equal([], keys)
|
275
291
|
end
|
276
292
|
|
277
|
-
|
278
|
-
|
279
|
-
|
280
|
-
|
281
|
-
|
282
|
-
|
283
|
-
|
284
|
-
|
285
|
-
|
286
|
-
|
293
|
+
if ActiveRecord::VERSION::STRING < '4.'
|
294
|
+
test 'finder_sql associations should not generate foreign keys' do
|
295
|
+
class Author < MockModel
|
296
|
+
has_many :books, :finder_sql => <<-SQL
|
297
|
+
SELECT *
|
298
|
+
FROM books
|
299
|
+
WHERE author_id = \#{id}
|
300
|
+
ORDER BY RANDOM() LIMIT 5'
|
301
|
+
SQL
|
302
|
+
end
|
303
|
+
class Book < MockModel; end
|
287
304
|
|
288
|
-
|
289
|
-
|
305
|
+
keys = Immigrant.infer_keys([]).first
|
306
|
+
assert_equal([], keys)
|
307
|
+
end
|
290
308
|
end
|
291
309
|
|
292
310
|
test 'polymorphic associations should not generate foreign keys' do
|
@@ -300,7 +318,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
300
318
|
has_many :properties, :as => :owner
|
301
319
|
end
|
302
320
|
|
303
|
-
keys = Immigrant.infer_keys([]
|
321
|
+
keys = Immigrant.infer_keys([]).first
|
304
322
|
assert_equal([], keys)
|
305
323
|
end
|
306
324
|
|
@@ -318,7 +336,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
318
336
|
has_many :authors, :through => :authors_fans
|
319
337
|
end
|
320
338
|
|
321
|
-
keys = Immigrant.infer_keys([]
|
339
|
+
keys = Immigrant.infer_keys([]).first
|
322
340
|
assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
|
323
341
|
assert_equal(
|
324
342
|
[foreign_key_definition(
|
@@ -340,7 +358,7 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
340
358
|
belongs_to :invalid
|
341
359
|
end
|
342
360
|
|
343
|
-
keys = Immigrant.infer_keys([]
|
361
|
+
keys = Immigrant.infer_keys([]).first
|
344
362
|
assert_nothing_raised { keys.map { |key| key.to_ruby(:add) } }
|
345
363
|
assert_equal(
|
346
364
|
[foreign_key_definition(
|
metadata
CHANGED
@@ -1,63 +1,56 @@
|
|
1
|
-
--- !ruby/object:Gem::Specification
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
2
|
name: immigrant
|
3
|
-
version: !ruby/object:Gem::Version
|
4
|
-
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.5
|
5
5
|
prerelease:
|
6
|
-
segments:
|
7
|
-
- 0
|
8
|
-
- 1
|
9
|
-
- 4
|
10
|
-
version: 0.1.4
|
11
6
|
platform: ruby
|
12
|
-
authors:
|
7
|
+
authors:
|
13
8
|
- Jon Jensen
|
14
9
|
autorequire:
|
15
10
|
bindir: bin
|
16
11
|
cert_chain: []
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
dependencies:
|
21
|
-
- !ruby/object:Gem::Dependency
|
12
|
+
date: 2013-08-08 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
22
15
|
name: activerecord
|
23
|
-
|
24
|
-
requirement: &id001 !ruby/object:Gem::Requirement
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
25
17
|
none: false
|
26
|
-
requirements:
|
27
|
-
- -
|
28
|
-
- !ruby/object:Gem::Version
|
29
|
-
|
30
|
-
segments:
|
31
|
-
- 3
|
32
|
-
- 0
|
33
|
-
version: "3.0"
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
34
22
|
type: :runtime
|
35
|
-
version_requirements: *id001
|
36
|
-
- !ruby/object:Gem::Dependency
|
37
|
-
name: foreigner
|
38
23
|
prerelease: false
|
39
|
-
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '3.0'
|
30
|
+
- !ruby/object:Gem::Dependency
|
31
|
+
name: foreigner
|
32
|
+
requirement: !ruby/object:Gem::Requirement
|
40
33
|
none: false
|
41
|
-
requirements:
|
42
|
-
- -
|
43
|
-
- !ruby/object:Gem::Version
|
44
|
-
hash: 29
|
45
|
-
segments:
|
46
|
-
- 1
|
47
|
-
- 2
|
48
|
-
- 1
|
34
|
+
requirements:
|
35
|
+
- - ! '>='
|
36
|
+
- !ruby/object:Gem::Version
|
49
37
|
version: 1.2.1
|
50
38
|
type: :runtime
|
51
|
-
|
52
|
-
|
39
|
+
prerelease: false
|
40
|
+
version_requirements: !ruby/object:Gem::Requirement
|
41
|
+
none: false
|
42
|
+
requirements:
|
43
|
+
- - ! '>='
|
44
|
+
- !ruby/object:Gem::Version
|
45
|
+
version: 1.2.1
|
46
|
+
description: Adds a generator for creating a foreign key migration based on your current
|
47
|
+
model associations
|
53
48
|
email: jenseng@gmail.com
|
54
49
|
executables: []
|
55
|
-
|
56
50
|
extensions: []
|
57
|
-
|
58
|
-
extra_rdoc_files:
|
51
|
+
extra_rdoc_files:
|
59
52
|
- README.rdoc
|
60
|
-
files:
|
53
|
+
files:
|
61
54
|
- LICENSE.txt
|
62
55
|
- Rakefile
|
63
56
|
- README.rdoc
|
@@ -71,43 +64,29 @@ files:
|
|
71
64
|
- lib/immigrant.rb
|
72
65
|
- test/helper.rb
|
73
66
|
- test/immigrant_test.rb
|
74
|
-
has_rdoc: true
|
75
67
|
homepage: http://github.com/jenseng/immigrant
|
76
68
|
licenses: []
|
77
|
-
|
78
69
|
post_install_message:
|
79
70
|
rdoc_options: []
|
80
|
-
|
81
|
-
require_paths:
|
71
|
+
require_paths:
|
82
72
|
- lib
|
83
|
-
required_ruby_version: !ruby/object:Gem::Requirement
|
73
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
84
74
|
none: false
|
85
|
-
requirements:
|
86
|
-
- -
|
87
|
-
- !ruby/object:Gem::Version
|
88
|
-
hash: 57
|
89
|
-
segments:
|
90
|
-
- 1
|
91
|
-
- 8
|
92
|
-
- 7
|
75
|
+
requirements:
|
76
|
+
- - ! '>='
|
77
|
+
- !ruby/object:Gem::Version
|
93
78
|
version: 1.8.7
|
94
|
-
required_rubygems_version: !ruby/object:Gem::Requirement
|
79
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
95
80
|
none: false
|
96
|
-
requirements:
|
97
|
-
- -
|
98
|
-
- !ruby/object:Gem::Version
|
99
|
-
hash: 17
|
100
|
-
segments:
|
101
|
-
- 1
|
102
|
-
- 3
|
103
|
-
- 5
|
81
|
+
requirements:
|
82
|
+
- - ! '>='
|
83
|
+
- !ruby/object:Gem::Version
|
104
84
|
version: 1.3.5
|
105
85
|
requirements: []
|
106
|
-
|
107
86
|
rubyforge_project:
|
108
|
-
rubygems_version: 1.
|
87
|
+
rubygems_version: 1.8.25
|
109
88
|
signing_key:
|
110
89
|
specification_version: 3
|
111
90
|
summary: Migration generator for Foreigner
|
112
91
|
test_files: []
|
113
|
-
|
92
|
+
has_rdoc:
|