2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2016-08-06 12:26:20 -04:00
|
|
|
require "models/post"
|
|
|
|
require "models/author"
|
|
|
|
require "models/project"
|
|
|
|
require "models/developer"
|
|
|
|
require "models/computer"
|
|
|
|
require "models/company"
|
2005-07-04 04:54:27 -04:00
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class AssociationCallbacksTest < ActiveRecord::TestCase
|
2005-07-04 04:54:27 -04:00
|
|
|
fixtures :posts, :authors, :projects, :developers
|
|
|
|
|
|
|
|
def setup
|
2005-07-05 03:20:24 -04:00
|
|
|
@david = authors(:david)
|
|
|
|
@thinking = posts(:thinking)
|
|
|
|
@authorless = posts(:authorless)
|
|
|
|
assert @david.post_log.empty?
|
2005-07-04 04:54:27 -04:00
|
|
|
end
|
2007-06-11 03:15:59 -04:00
|
|
|
|
2005-07-04 04:54:27 -04:00
|
|
|
def test_adding_macro_callbacks
|
|
|
|
@david.posts_with_callbacks << @thinking
|
|
|
|
assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}"], @david.post_log
|
|
|
|
@david.posts_with_callbacks << @thinking
|
2007-06-11 03:15:59 -04:00
|
|
|
assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}", "before_adding#{@thinking.id}",
|
2005-07-04 04:54:27 -04:00
|
|
|
"after_adding#{@thinking.id}"], @david.post_log
|
|
|
|
end
|
2007-06-11 03:15:59 -04:00
|
|
|
|
2005-07-04 04:54:27 -04:00
|
|
|
def test_adding_with_proc_callbacks
|
|
|
|
@david.posts_with_proc_callbacks << @thinking
|
|
|
|
assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}"], @david.post_log
|
|
|
|
@david.posts_with_proc_callbacks << @thinking
|
2007-06-11 03:15:59 -04:00
|
|
|
assert_equal ["before_adding#{@thinking.id}", "after_adding#{@thinking.id}", "before_adding#{@thinking.id}",
|
2005-07-04 04:54:27 -04:00
|
|
|
"after_adding#{@thinking.id}"], @david.post_log
|
|
|
|
end
|
2007-06-11 03:15:59 -04:00
|
|
|
|
2005-07-04 04:54:27 -04:00
|
|
|
def test_removing_with_macro_callbacks
|
|
|
|
first_post, second_post = @david.posts_with_callbacks[0, 2]
|
|
|
|
@david.posts_with_callbacks.delete(first_post)
|
|
|
|
assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}"], @david.post_log
|
|
|
|
@david.posts_with_callbacks.delete(second_post)
|
2007-06-11 03:15:59 -04:00
|
|
|
assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}", "before_removing#{second_post.id}",
|
2005-07-04 04:54:27 -04:00
|
|
|
"after_removing#{second_post.id}"], @david.post_log
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_removing_with_proc_callbacks
|
|
|
|
first_post, second_post = @david.posts_with_callbacks[0, 2]
|
|
|
|
@david.posts_with_proc_callbacks.delete(first_post)
|
|
|
|
assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}"], @david.post_log
|
|
|
|
@david.posts_with_proc_callbacks.delete(second_post)
|
2007-06-11 03:15:59 -04:00
|
|
|
assert_equal ["before_removing#{first_post.id}", "after_removing#{first_post.id}", "before_removing#{second_post.id}",
|
2005-07-04 04:54:27 -04:00
|
|
|
"after_removing#{second_post.id}"], @david.post_log
|
|
|
|
end
|
2007-06-11 03:15:59 -04:00
|
|
|
|
2005-07-04 04:54:27 -04:00
|
|
|
def test_multiple_callbacks
|
|
|
|
@david.posts_with_multiple_callbacks << @thinking
|
2007-06-11 03:15:59 -04:00
|
|
|
assert_equal ["before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}", "after_adding#{@thinking.id}",
|
2005-07-04 04:54:27 -04:00
|
|
|
"after_adding_proc#{@thinking.id}"], @david.post_log
|
|
|
|
@david.posts_with_multiple_callbacks << @thinking
|
2007-06-11 03:15:59 -04:00
|
|
|
assert_equal ["before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}", "after_adding#{@thinking.id}",
|
|
|
|
"after_adding_proc#{@thinking.id}", "before_adding#{@thinking.id}", "before_adding_proc#{@thinking.id}",
|
2005-07-04 04:54:27 -04:00
|
|
|
"after_adding#{@thinking.id}", "after_adding_proc#{@thinking.id}"], @david.post_log
|
|
|
|
end
|
2007-06-11 03:15:59 -04:00
|
|
|
|
2007-10-16 01:07:58 -04:00
|
|
|
def test_has_many_callbacks_with_create
|
2016-08-06 13:37:57 -04:00
|
|
|
morten = Author.create name: "Morten"
|
|
|
|
post = morten.posts_with_proc_callbacks.create! title: "Hello", body: "How are you doing?"
|
2007-10-16 01:07:58 -04:00
|
|
|
assert_equal ["before_adding<new>", "after_adding#{post.id}"], morten.post_log
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_many_callbacks_with_create!
|
2016-08-06 13:37:57 -04:00
|
|
|
morten = Author.create! name: "Morten"
|
|
|
|
post = morten.posts_with_proc_callbacks.create title: "Hello", body: "How are you doing?"
|
2007-10-16 01:07:58 -04:00
|
|
|
assert_equal ["before_adding<new>", "after_adding#{post.id}"], morten.post_log
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_has_many_callbacks_for_save_on_parent
|
2016-08-06 13:37:57 -04:00
|
|
|
jack = Author.new name: "Jack"
|
|
|
|
jack.posts_with_callbacks.build title: "Call me back!", body: "Before you wake up and after you sleep"
|
2007-10-16 01:07:58 -04:00
|
|
|
|
|
|
|
callback_log = ["before_adding<new>", "after_adding#{jack.posts_with_callbacks.first.id}"]
|
|
|
|
assert_equal callback_log, jack.post_log
|
|
|
|
assert jack.save
|
|
|
|
assert_equal 1, jack.posts_with_callbacks.count
|
|
|
|
assert_equal callback_log, jack.post_log
|
|
|
|
end
|
|
|
|
|
2010-12-30 16:44:29 -05:00
|
|
|
def test_has_many_callbacks_for_destroy_on_parent
|
2016-08-06 13:37:57 -04:00
|
|
|
firm = Firm.create! name: "Firm"
|
|
|
|
client = firm.clients.create! name: "Client"
|
2010-12-30 16:44:29 -05:00
|
|
|
firm.destroy
|
|
|
|
|
|
|
|
assert_equal ["before_remove#{client.id}", "after_remove#{client.id}"], firm.log
|
|
|
|
end
|
|
|
|
|
2005-07-04 04:54:27 -04:00
|
|
|
def test_has_and_belongs_to_many_add_callback
|
|
|
|
david = developers(:david)
|
|
|
|
ar = projects(:active_record)
|
|
|
|
assert ar.developers_log.empty?
|
|
|
|
ar.developers_with_callbacks << david
|
|
|
|
assert_equal ["before_adding#{david.id}", "after_adding#{david.id}"], ar.developers_log
|
|
|
|
ar.developers_with_callbacks << david
|
2007-06-11 03:15:59 -04:00
|
|
|
assert_equal ["before_adding#{david.id}", "after_adding#{david.id}", "before_adding#{david.id}",
|
2005-07-04 04:54:27 -04:00
|
|
|
"after_adding#{david.id}"], ar.developers_log
|
|
|
|
end
|
2007-06-11 03:15:59 -04:00
|
|
|
|
2014-02-25 17:57:06 -05:00
|
|
|
def test_has_and_belongs_to_many_before_add_called_before_save
|
|
|
|
dev = nil
|
|
|
|
new_dev = nil
|
|
|
|
klass = Class.new(Project) do
|
|
|
|
def self.name; Project.name; end
|
|
|
|
has_and_belongs_to_many :developers_with_callbacks,
|
2016-08-06 13:37:57 -04:00
|
|
|
class_name: "Developer",
|
2016-10-28 23:05:58 -04:00
|
|
|
before_add: lambda { |o, r|
|
2014-02-25 17:57:06 -05:00
|
|
|
dev = r
|
|
|
|
new_dev = r.new_record?
|
|
|
|
}
|
|
|
|
end
|
|
|
|
rec = klass.create!
|
2016-08-06 13:37:57 -04:00
|
|
|
alice = Developer.new(name: "alice")
|
2014-02-25 17:57:06 -05:00
|
|
|
rec.developers_with_callbacks << alice
|
|
|
|
assert_equal alice, dev
|
|
|
|
assert_not_nil new_dev
|
|
|
|
assert new_dev, "record should not have been saved"
|
|
|
|
assert_not alice.new_record?
|
|
|
|
end
|
|
|
|
|
2008-04-04 10:37:22 -04:00
|
|
|
def test_has_and_belongs_to_many_after_add_called_after_save
|
|
|
|
ar = projects(:active_record)
|
|
|
|
assert ar.developers_log.empty?
|
2016-08-06 13:37:57 -04:00
|
|
|
alice = Developer.new(name: "alice")
|
2008-04-04 10:37:22 -04:00
|
|
|
ar.developers_with_callbacks << alice
|
|
|
|
assert_equal"after_adding#{alice.id}", ar.developers_log.last
|
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
bob = ar.developers_with_callbacks.create(name: "bob")
|
2008-04-04 10:37:22 -04:00
|
|
|
assert_equal "after_adding#{bob.id}", ar.developers_log.last
|
|
|
|
|
2016-08-06 13:37:57 -04:00
|
|
|
ar.developers_with_callbacks.build(name: "charlie")
|
2008-04-04 10:37:22 -04:00
|
|
|
assert_equal "after_adding<new>", ar.developers_log.last
|
|
|
|
end
|
|
|
|
|
2005-07-04 04:54:27 -04:00
|
|
|
def test_has_and_belongs_to_many_remove_callback
|
|
|
|
david = developers(:david)
|
|
|
|
jamis = developers(:jamis)
|
2005-07-05 03:20:24 -04:00
|
|
|
activerecord = projects(:active_record)
|
|
|
|
assert activerecord.developers_log.empty?
|
|
|
|
activerecord.developers_with_callbacks.delete(david)
|
|
|
|
assert_equal ["before_removing#{david.id}", "after_removing#{david.id}"], activerecord.developers_log
|
2007-06-11 03:15:59 -04:00
|
|
|
|
2005-07-05 03:20:24 -04:00
|
|
|
activerecord.developers_with_callbacks.delete(jamis)
|
2007-06-11 03:15:59 -04:00
|
|
|
assert_equal ["before_removing#{david.id}", "after_removing#{david.id}", "before_removing#{jamis.id}",
|
2005-07-05 03:20:24 -04:00
|
|
|
"after_removing#{jamis.id}"], activerecord.developers_log
|
2005-07-04 04:54:27 -04:00
|
|
|
end
|
2005-10-14 08:53:39 -04:00
|
|
|
|
2014-04-28 19:15:42 -04:00
|
|
|
def test_has_and_belongs_to_many_does_not_fire_callbacks_on_clear
|
2005-10-14 08:53:39 -04:00
|
|
|
activerecord = projects(:active_record)
|
|
|
|
assert activerecord.developers_log.empty?
|
|
|
|
if activerecord.developers_with_callbacks.size == 0
|
|
|
|
activerecord.developers << developers(:david)
|
|
|
|
activerecord.developers << developers(:jamis)
|
|
|
|
activerecord.reload
|
|
|
|
assert activerecord.developers_with_callbacks.size == 2
|
|
|
|
end
|
2016-10-28 23:05:58 -04:00
|
|
|
activerecord.developers_with_callbacks.flat_map { |d| ["before_removing#{d.id}", "after_removing#{d.id}"] }.sort
|
2005-10-14 08:53:39 -04:00
|
|
|
assert activerecord.developers_with_callbacks.clear
|
2014-04-28 19:15:42 -04:00
|
|
|
assert_predicate activerecord.developers_log, :empty?
|
2005-10-14 08:53:39 -04:00
|
|
|
end
|
2007-06-11 03:15:59 -04:00
|
|
|
|
2007-10-16 01:07:58 -04:00
|
|
|
def test_has_many_and_belongs_to_many_callbacks_for_save_on_parent
|
2016-08-06 13:37:57 -04:00
|
|
|
project = Project.new name: "Callbacks"
|
|
|
|
project.developers_with_callbacks.build name: "Jack", salary: 95000
|
2007-10-16 01:07:58 -04:00
|
|
|
|
|
|
|
callback_log = ["before_adding<new>", "after_adding<new>"]
|
|
|
|
assert_equal callback_log, project.developers_log
|
|
|
|
assert project.save
|
2007-12-22 06:26:03 -05:00
|
|
|
assert_equal 1, project.developers_with_callbacks.size
|
2007-10-16 01:07:58 -04:00
|
|
|
assert_equal callback_log, project.developers_log
|
|
|
|
end
|
|
|
|
|
2005-07-04 04:54:27 -04:00
|
|
|
def test_dont_add_if_before_callback_raises_exception
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes @david.unchangeable_posts, @authorless
|
2005-07-04 04:54:27 -04:00
|
|
|
begin
|
2016-02-03 08:06:00 -05:00
|
|
|
@david.unchangeable_posts << @authorless
|
2015-03-06 06:13:40 -05:00
|
|
|
rescue Exception
|
2005-07-04 04:54:27 -04:00
|
|
|
end
|
|
|
|
assert @david.post_log.empty?
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes @david.unchangeable_posts, @authorless
|
2005-07-04 04:54:27 -04:00
|
|
|
@david.reload
|
2016-09-16 12:44:05 -04:00
|
|
|
assert_not_includes @david.unchangeable_posts, @authorless
|
2005-07-04 04:54:27 -04:00
|
|
|
end
|
|
|
|
end
|