Skip to content

Commit

Permalink
bring dependent: nullify along to add_foreign_key, fixes #24
Browse files Browse the repository at this point in the history
  • Loading branch information
jenseng committed Nov 21, 2015
1 parent adf6aaa commit 0cd3cc6
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 3 deletions.
13 changes: 10 additions & 3 deletions lib/immigrant.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ def model_keys(classes)
# Foo.has_many :bars
# Foo.has_many :bazzes, :class_name => Bar
# we need to make sure everything is legit and see if any of them
# specify :dependent => :delete
# specify :dependent => :delete|:delete_all|:nullify
if current_key = foreign_keys[foreign_key.hash_key]
if current_key.to_table != foreign_key.to_table || current_key.options[:primary_key] != foreign_key.options[:primary_key]
warnings[foreign_key.hash_key] ||= "Skipping #{foreign_key.from_table}.#{foreign_key.options[:column]}: it has multiple associations referencing different keys/tables."
Expand Down Expand Up @@ -149,15 +149,22 @@ def infer_belongs_to_keys(klass, reflection)
]
end

DEPENDENT_MAP = {
:delete => :cascade,
:delete_all => :cascade,
:nullify => :nullify
}

def infer_has_n_keys(klass, reflection)
from_table = reflection.klass.table_name
to_table = klass.table_name
column = reflection.send(FOREIGN_KEY).to_s
primary_key = (reflection.options[:primary_key] || klass.primary_key).to_s

actions = {}
if [:delete, :delete_all].include?(reflection.options[:dependent]) && !qualified_reflection?(reflection, klass)
actions = {:on_delete => :cascade, :on_update => :cascade}
dependent_action = DEPENDENT_MAP[reflection.options[:dependent]]
if dependent_action && !qualified_reflection?(reflection, klass)
actions = {:on_delete => dependent_action, :on_update => dependent_action}
end

[
Expand Down
34 changes: 34 additions & 0 deletions test/immigrant_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,23 @@ class Book < ActiveRecord::Base; end
)
end

test 'has_one :dependent => :nullify should generate a foreign key with :on_delete => :nullify' do
given <<-CODE
class Author < ActiveRecord::Base
has_one :book, :order => "id DESC", :dependent => :nullify
end
class Book < ActiveRecord::Base; end
CODE

assert_equal(
[foreign_key_definition(
'books', 'authors',
:column => 'author_id', :primary_key => 'id', :on_delete => :nullify, :on_update => :nullify
)],
infer_keys
)
end

test 'has_many should generate a foreign key' do
given <<-CODE
class Author < ActiveRecord::Base
Expand Down Expand Up @@ -177,6 +194,23 @@ class Book < ActiveRecord::Base; end
)
end

test 'has_many :dependent => :nullify should generate a foreign key with :on_delete => :nullify' do
given <<-CODE
class Author < ActiveRecord::Base
has_many :books, :dependent => :nullify
end
class Book < ActiveRecord::Base; end
CODE

assert_equal(
[foreign_key_definition(
'books', 'authors',
:column => 'author_id', :primary_key => 'id', :on_delete => :nullify, :on_update => :nullify
)],
infer_keys
)
end

test 'has_and_belongs_to_many should generate two foreign keys' do
given <<-CODE
class Author < ActiveRecord::Base
Expand Down

0 comments on commit 0cd3cc6

Please sign in to comment.