immigrant 0.3.4 → 0.3.5
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/immigrant.rb +10 -3
- data/test/immigrant_test.rb +46 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 178034936e28a389c8a909243a9576c6212d8fb0
|
4
|
+
data.tar.gz: 881d1940852983b4a262e1904b7ccc9471233d25
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e59b3cd9d311554a884b871421e719082944ed0c6e891a8b28ddc8f20c8d09ac803a5afa708d7336fda28e141856276eb40034f21d43d0e0b00baa9df56c4f4e
|
7
|
+
data.tar.gz: 44a246c9e00744f3003a4c2d4481d3654129659a1f1dbd6c208b1d5d0d646d8ac100c3368da087d062e0703f4e6a3bf4a5048a6527ed36e2dad178c580619f14
|
data/lib/immigrant.rb
CHANGED
@@ -75,7 +75,7 @@ module Immigrant
|
|
75
75
|
# Foo.has_many :bars
|
76
76
|
# Foo.has_many :bazzes, :class_name => Bar
|
77
77
|
# we need to make sure everything is legit and see if any of them
|
78
|
-
# specify :dependent => :delete
|
78
|
+
# specify :dependent => :delete|:delete_all|:nullify
|
79
79
|
if current_key = foreign_keys[foreign_key.hash_key]
|
80
80
|
if current_key.to_table != foreign_key.to_table || current_key.options[:primary_key] != foreign_key.options[:primary_key]
|
81
81
|
warnings[foreign_key.hash_key] ||= "Skipping #{foreign_key.from_table}.#{foreign_key.options[:column]}: it has multiple associations referencing different keys/tables."
|
@@ -149,6 +149,12 @@ module Immigrant
|
|
149
149
|
]
|
150
150
|
end
|
151
151
|
|
152
|
+
DEPENDENT_MAP = {
|
153
|
+
:delete => :cascade,
|
154
|
+
:delete_all => :cascade,
|
155
|
+
:nullify => :nullify
|
156
|
+
}
|
157
|
+
|
152
158
|
def infer_has_n_keys(klass, reflection)
|
153
159
|
from_table = reflection.klass.table_name
|
154
160
|
to_table = klass.table_name
|
@@ -156,8 +162,9 @@ module Immigrant
|
|
156
162
|
primary_key = (reflection.options[:primary_key] || klass.primary_key).to_s
|
157
163
|
|
158
164
|
actions = {}
|
159
|
-
|
160
|
-
|
165
|
+
dependent_action = DEPENDENT_MAP[reflection.options[:dependent]]
|
166
|
+
if dependent_action && !qualified_reflection?(reflection, klass)
|
167
|
+
actions = {:on_delete => dependent_action, :on_update => dependent_action}
|
161
168
|
end
|
162
169
|
|
163
170
|
[
|
data/test/immigrant_test.rb
CHANGED
@@ -28,6 +28,18 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
28
28
|
end
|
29
29
|
})
|
30
30
|
end
|
31
|
+
if ActiveRecord::VERSION::STRING < '4.1'
|
32
|
+
# make sure habtms create a model class so that MockConnection.tables works
|
33
|
+
extend(Module.new{
|
34
|
+
define_method :has_and_belongs_to_many do |assoc, options = {}|
|
35
|
+
join_table = options[:join_table] || [table_name, assoc.to_s].sort.join("_")
|
36
|
+
Class.new(ActiveRecord::Base) do
|
37
|
+
self.table_name = join_table
|
38
|
+
end
|
39
|
+
super assoc, options
|
40
|
+
end
|
41
|
+
})
|
42
|
+
end
|
31
43
|
end
|
32
44
|
|
33
45
|
class MockConnection
|
@@ -131,6 +143,23 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
131
143
|
)
|
132
144
|
end
|
133
145
|
|
146
|
+
test 'has_one :dependent => :nullify should generate a foreign key with :on_delete => :nullify' do
|
147
|
+
given <<-CODE
|
148
|
+
class Author < ActiveRecord::Base
|
149
|
+
has_one :book, :order => "id DESC", :dependent => :nullify
|
150
|
+
end
|
151
|
+
class Book < ActiveRecord::Base; end
|
152
|
+
CODE
|
153
|
+
|
154
|
+
assert_equal(
|
155
|
+
[foreign_key_definition(
|
156
|
+
'books', 'authors',
|
157
|
+
:column => 'author_id', :primary_key => 'id', :on_delete => :nullify, :on_update => :nullify
|
158
|
+
)],
|
159
|
+
infer_keys
|
160
|
+
)
|
161
|
+
end
|
162
|
+
|
134
163
|
test 'has_many should generate a foreign key' do
|
135
164
|
given <<-CODE
|
136
165
|
class Author < ActiveRecord::Base
|
@@ -165,6 +194,23 @@ class ImmigrantTest < ActiveSupport::TestCase
|
|
165
194
|
)
|
166
195
|
end
|
167
196
|
|
197
|
+
test 'has_many :dependent => :nullify should generate a foreign key with :on_delete => :nullify' do
|
198
|
+
given <<-CODE
|
199
|
+
class Author < ActiveRecord::Base
|
200
|
+
has_many :books, :dependent => :nullify
|
201
|
+
end
|
202
|
+
class Book < ActiveRecord::Base; end
|
203
|
+
CODE
|
204
|
+
|
205
|
+
assert_equal(
|
206
|
+
[foreign_key_definition(
|
207
|
+
'books', 'authors',
|
208
|
+
:column => 'author_id', :primary_key => 'id', :on_delete => :nullify, :on_update => :nullify
|
209
|
+
)],
|
210
|
+
infer_keys
|
211
|
+
)
|
212
|
+
end
|
213
|
+
|
168
214
|
test 'has_and_belongs_to_many should generate two foreign keys' do
|
169
215
|
given <<-CODE
|
170
216
|
class Author < ActiveRecord::Base
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: immigrant
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.5
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Jon Jensen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2015-
|
11
|
+
date: 2015-11-21 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|