2011-06-06 14:17:44 -04:00
|
|
|
require "cases/helper"
|
2014-11-14 03:55:32 -05:00
|
|
|
require 'models/developer'
|
|
|
|
require 'models/computer'
|
2005-01-01 11:14:15 -05:00
|
|
|
|
|
|
|
class CallbackDeveloper < ActiveRecord::Base
|
2011-11-29 07:28:04 -05:00
|
|
|
self.table_name = 'developers'
|
2005-01-01 11:14:15 -05:00
|
|
|
|
2005-06-13 06:52:53 -04:00
|
|
|
class << self
|
2005-01-01 11:14:15 -05:00
|
|
|
def callback_string(callback_method)
|
|
|
|
"history << [#{callback_method.to_sym.inspect}, :string]"
|
|
|
|
end
|
|
|
|
|
|
|
|
def callback_proc(callback_method)
|
|
|
|
Proc.new { |model| model.history << [callback_method, :proc] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def define_callback_method(callback_method)
|
2009-09-08 11:10:14 -04:00
|
|
|
define_method(callback_method) do
|
|
|
|
self.history << [callback_method, :method]
|
2005-01-01 11:14:15 -05:00
|
|
|
end
|
2010-09-05 20:20:54 -04:00
|
|
|
send(callback_method, :"#{callback_method}")
|
2005-01-01 11:14:15 -05:00
|
|
|
end
|
|
|
|
|
2009-09-08 11:22:45 -04:00
|
|
|
def callback_object(callback_method)
|
2005-01-01 11:14:15 -05:00
|
|
|
klass = Class.new
|
|
|
|
klass.send(:define_method, callback_method) do |model|
|
2009-09-08 11:22:45 -04:00
|
|
|
model.history << [callback_method, :object]
|
2005-01-01 11:14:15 -05:00
|
|
|
end
|
|
|
|
klass.new
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-09-05 20:20:54 -04:00
|
|
|
ActiveRecord::Callbacks::CALLBACKS.each do |callback_method|
|
|
|
|
next if callback_method.to_s =~ /^around_/
|
|
|
|
define_callback_method(callback_method)
|
|
|
|
send(callback_method, callback_string(callback_method))
|
|
|
|
send(callback_method, callback_proc(callback_method))
|
|
|
|
send(callback_method, callback_object(callback_method))
|
|
|
|
send(callback_method) { |model| model.history << [callback_method, :block] }
|
2005-01-01 11:14:15 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def history
|
|
|
|
@history ||= []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-03-16 16:51:26 -04:00
|
|
|
class CallbackDeveloperWithFalseValidation < CallbackDeveloper
|
2013-05-09 14:58:55 -04:00
|
|
|
before_validation proc { |model| model.history << [:before_validation, :returning_false]; false }
|
2010-03-16 16:51:26 -04:00
|
|
|
before_validation proc { |model| model.history << [:before_validation, :should_never_get_here] }
|
|
|
|
end
|
|
|
|
|
2014-12-15 01:10:15 -05:00
|
|
|
class CallbackDeveloperWithHaltedValidation < CallbackDeveloper
|
|
|
|
before_validation proc { |model| model.history << [:before_validation, :throwing_abort]; throw(:abort) }
|
|
|
|
before_validation proc { |model| model.history << [:before_validation, :should_never_get_here] }
|
|
|
|
end
|
|
|
|
|
2007-01-23 22:04:48 -05:00
|
|
|
class ParentDeveloper < ActiveRecord::Base
|
2011-11-29 07:28:04 -05:00
|
|
|
self.table_name = 'developers'
|
2007-01-23 22:04:48 -05:00
|
|
|
attr_accessor :after_save_called
|
|
|
|
before_validation {|record| record.after_save_called = true}
|
|
|
|
end
|
|
|
|
|
|
|
|
class ChildDeveloper < ParentDeveloper
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2007-01-23 22:04:48 -05:00
|
|
|
end
|
|
|
|
|
2005-09-27 20:09:50 -04:00
|
|
|
class ImmutableDeveloper < ActiveRecord::Base
|
2011-11-29 07:28:04 -05:00
|
|
|
self.table_name = 'developers'
|
2005-09-27 20:09:50 -04:00
|
|
|
|
2005-10-01 22:00:50 -04:00
|
|
|
validates_inclusion_of :salary, :in => 50000..200000
|
|
|
|
|
|
|
|
before_save :cancel
|
|
|
|
before_destroy :cancel
|
|
|
|
|
|
|
|
private
|
|
|
|
def cancel
|
|
|
|
false
|
|
|
|
end
|
2005-09-27 20:09:50 -04:00
|
|
|
end
|
|
|
|
|
2014-12-15 01:10:15 -05:00
|
|
|
class DeveloperWithCanceledCallbacks < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
|
|
|
|
2015-01-03 15:22:20 -05:00
|
|
|
validates_inclusion_of :salary, in: 50000..200000
|
2014-12-15 01:10:15 -05:00
|
|
|
|
|
|
|
before_save :cancel
|
|
|
|
before_destroy :cancel
|
|
|
|
|
|
|
|
private
|
|
|
|
def cancel
|
|
|
|
throw(:abort)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-01-06 16:16:49 -05:00
|
|
|
class OnCallbacksDeveloper < ActiveRecord::Base
|
2011-11-29 07:28:04 -05:00
|
|
|
self.table_name = 'developers'
|
2010-01-06 16:16:49 -05:00
|
|
|
|
|
|
|
before_validation { history << :before_validation }
|
|
|
|
before_validation(:on => :create){ history << :before_validation_on_create }
|
|
|
|
before_validation(:on => :update){ history << :before_validation_on_update }
|
|
|
|
|
|
|
|
validate do
|
|
|
|
history << :validate
|
|
|
|
end
|
|
|
|
|
|
|
|
after_validation { history << :after_validation }
|
|
|
|
after_validation(:on => :create){ history << :after_validation_on_create }
|
|
|
|
after_validation(:on => :update){ history << :after_validation_on_update }
|
|
|
|
|
|
|
|
def history
|
|
|
|
@history ||= []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-07-02 12:18:12 -04:00
|
|
|
class ContextualCallbacksDeveloper < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
|
|
|
|
|
|
|
before_validation { history << :before_validation }
|
|
|
|
before_validation :before_validation_on_create_and_update, :on => [ :create, :update ]
|
|
|
|
|
|
|
|
validate do
|
|
|
|
history << :validate
|
|
|
|
end
|
|
|
|
|
|
|
|
after_validation { history << :after_validation }
|
|
|
|
after_validation :after_validation_on_create_and_update, :on => [ :create, :update ]
|
|
|
|
|
|
|
|
def before_validation_on_create_and_update
|
|
|
|
history << "before_validation_on_#{self.validation_context}".to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def after_validation_on_create_and_update
|
|
|
|
history << "after_validation_on_#{self.validation_context}".to_sym
|
|
|
|
end
|
|
|
|
|
|
|
|
def history
|
|
|
|
@history ||= []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2006-10-04 11:38:59 -04:00
|
|
|
class CallbackCancellationDeveloper < ActiveRecord::Base
|
2011-11-29 07:28:04 -05:00
|
|
|
self.table_name = 'developers'
|
2009-01-09 18:09:50 -05:00
|
|
|
|
|
|
|
attr_reader :after_save_called, :after_create_called, :after_update_called, :after_destroy_called
|
|
|
|
attr_accessor :cancel_before_save, :cancel_before_create, :cancel_before_update, :cancel_before_destroy
|
|
|
|
|
2010-03-18 13:23:14 -04:00
|
|
|
before_save {defined?(@cancel_before_save) ? !@cancel_before_save : false}
|
2009-09-08 11:10:14 -04:00
|
|
|
before_create { !@cancel_before_create }
|
|
|
|
before_update { !@cancel_before_update }
|
|
|
|
before_destroy { !@cancel_before_destroy }
|
2009-01-09 18:09:50 -05:00
|
|
|
|
2009-09-08 11:10:14 -04:00
|
|
|
after_save { @after_save_called = true }
|
|
|
|
after_update { @after_update_called = true }
|
|
|
|
after_create { @after_create_called = true }
|
|
|
|
after_destroy { @after_destroy_called = true }
|
2006-10-04 11:38:59 -04:00
|
|
|
end
|
|
|
|
|
2014-12-15 01:10:15 -05:00
|
|
|
class CallbackHaltedDeveloper < ActiveRecord::Base
|
|
|
|
self.table_name = 'developers'
|
|
|
|
|
|
|
|
attr_reader :after_save_called, :after_create_called, :after_update_called, :after_destroy_called
|
|
|
|
attr_accessor :cancel_before_save, :cancel_before_create, :cancel_before_update, :cancel_before_destroy
|
|
|
|
|
|
|
|
before_save { throw(:abort) if defined?(@cancel_before_save) }
|
|
|
|
before_create { throw(:abort) if @cancel_before_create }
|
|
|
|
before_update { throw(:abort) if @cancel_before_update }
|
|
|
|
before_destroy { throw(:abort) if @cancel_before_destroy }
|
|
|
|
|
|
|
|
after_save { @after_save_called = true }
|
|
|
|
after_update { @after_update_called = true }
|
|
|
|
after_create { @after_create_called = true }
|
|
|
|
after_destroy { @after_destroy_called = true }
|
|
|
|
end
|
|
|
|
|
2008-01-21 12:20:51 -05:00
|
|
|
class CallbacksTest < ActiveRecord::TestCase
|
2005-06-10 10:58:02 -04:00
|
|
|
fixtures :developers
|
2005-01-01 11:14:15 -05:00
|
|
|
|
|
|
|
def test_initialize
|
|
|
|
david = CallbackDeveloper.new
|
|
|
|
assert_equal [
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_initialize, :method ],
|
2005-03-13 19:04:14 -05:00
|
|
|
[ :after_initialize, :string ],
|
|
|
|
[ :after_initialize, :proc ],
|
|
|
|
[ :after_initialize, :object ],
|
|
|
|
[ :after_initialize, :block ],
|
2005-01-01 11:14:15 -05:00
|
|
|
], david.history
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_find
|
|
|
|
david = CallbackDeveloper.find(1)
|
|
|
|
assert_equal [
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_find, :method ],
|
2005-03-13 19:04:14 -05:00
|
|
|
[ :after_find, :string ],
|
|
|
|
[ :after_find, :proc ],
|
|
|
|
[ :after_find, :object ],
|
|
|
|
[ :after_find, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_initialize, :method ],
|
2005-03-13 19:04:14 -05:00
|
|
|
[ :after_initialize, :string ],
|
|
|
|
[ :after_initialize, :proc ],
|
|
|
|
[ :after_initialize, :object ],
|
|
|
|
[ :after_initialize, :block ],
|
2005-01-01 11:14:15 -05:00
|
|
|
], david.history
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_new_valid?
|
|
|
|
david = CallbackDeveloper.new
|
|
|
|
david.valid?
|
|
|
|
assert_equal [
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_initialize, :method ],
|
2005-03-13 19:04:14 -05:00
|
|
|
[ :after_initialize, :string ],
|
|
|
|
[ :after_initialize, :proc ],
|
|
|
|
[ :after_initialize, :object ],
|
|
|
|
[ :after_initialize, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :before_validation, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :before_validation, :string ],
|
|
|
|
[ :before_validation, :proc ],
|
|
|
|
[ :before_validation, :object ],
|
|
|
|
[ :before_validation, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_validation, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :after_validation, :string ],
|
|
|
|
[ :after_validation, :proc ],
|
|
|
|
[ :after_validation, :object ],
|
|
|
|
[ :after_validation, :block ],
|
|
|
|
], david.history
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_existing_valid?
|
|
|
|
david = CallbackDeveloper.find(1)
|
|
|
|
david.valid?
|
|
|
|
assert_equal [
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_find, :method ],
|
2005-03-13 19:04:14 -05:00
|
|
|
[ :after_find, :string ],
|
|
|
|
[ :after_find, :proc ],
|
|
|
|
[ :after_find, :object ],
|
|
|
|
[ :after_find, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_initialize, :method ],
|
2005-03-13 19:04:14 -05:00
|
|
|
[ :after_initialize, :string ],
|
|
|
|
[ :after_initialize, :proc ],
|
|
|
|
[ :after_initialize, :object ],
|
|
|
|
[ :after_initialize, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :before_validation, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :before_validation, :string ],
|
|
|
|
[ :before_validation, :proc ],
|
|
|
|
[ :before_validation, :object ],
|
|
|
|
[ :before_validation, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_validation, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :after_validation, :string ],
|
|
|
|
[ :after_validation, :proc ],
|
|
|
|
[ :after_validation, :object ],
|
|
|
|
[ :after_validation, :block ],
|
|
|
|
], david.history
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_create
|
|
|
|
david = CallbackDeveloper.create('name' => 'David', 'salary' => 1000000)
|
|
|
|
assert_equal [
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_initialize, :method ],
|
2005-03-13 19:04:14 -05:00
|
|
|
[ :after_initialize, :string ],
|
|
|
|
[ :after_initialize, :proc ],
|
|
|
|
[ :after_initialize, :object ],
|
|
|
|
[ :after_initialize, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :before_validation, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :before_validation, :string ],
|
|
|
|
[ :before_validation, :proc ],
|
|
|
|
[ :before_validation, :object ],
|
|
|
|
[ :before_validation, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_validation, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :after_validation, :string ],
|
|
|
|
[ :after_validation, :proc ],
|
|
|
|
[ :after_validation, :object ],
|
|
|
|
[ :after_validation, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :before_save, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :before_save, :string ],
|
|
|
|
[ :before_save, :proc ],
|
|
|
|
[ :before_save, :object ],
|
|
|
|
[ :before_save, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :before_create, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :before_create, :string ],
|
|
|
|
[ :before_create, :proc ],
|
|
|
|
[ :before_create, :object ],
|
|
|
|
[ :before_create, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_create, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :after_create, :string ],
|
|
|
|
[ :after_create, :proc ],
|
|
|
|
[ :after_create, :object ],
|
|
|
|
[ :after_create, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_save, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :after_save, :string ],
|
|
|
|
[ :after_save, :proc ],
|
|
|
|
[ :after_save, :object ],
|
2015-01-11 18:38:22 -05:00
|
|
|
[ :after_save, :block ],
|
|
|
|
[ :after_commit, :block ],
|
|
|
|
[ :after_commit, :object ],
|
|
|
|
[ :after_commit, :proc ],
|
|
|
|
[ :after_commit, :string ],
|
|
|
|
[ :after_commit, :method ]
|
2005-01-01 11:14:15 -05:00
|
|
|
], david.history
|
|
|
|
end
|
|
|
|
|
2010-01-06 16:16:49 -05:00
|
|
|
def test_validate_on_create
|
|
|
|
david = OnCallbacksDeveloper.create('name' => 'David', 'salary' => 1000000)
|
|
|
|
assert_equal [
|
|
|
|
:before_validation,
|
|
|
|
:before_validation_on_create,
|
|
|
|
:validate,
|
|
|
|
:after_validation,
|
|
|
|
:after_validation_on_create
|
|
|
|
], david.history
|
|
|
|
end
|
|
|
|
|
2012-07-02 12:18:12 -04:00
|
|
|
def test_validate_on_contextual_create
|
|
|
|
david = ContextualCallbacksDeveloper.create('name' => 'David', 'salary' => 1000000)
|
|
|
|
assert_equal [
|
|
|
|
:before_validation,
|
|
|
|
:before_validation_on_create,
|
|
|
|
:validate,
|
|
|
|
:after_validation,
|
|
|
|
:after_validation_on_create
|
|
|
|
], david.history
|
|
|
|
end
|
|
|
|
|
2010-01-06 16:16:49 -05:00
|
|
|
def test_update
|
2005-01-01 11:14:15 -05:00
|
|
|
david = CallbackDeveloper.find(1)
|
|
|
|
david.save
|
|
|
|
assert_equal [
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_find, :method ],
|
2005-03-13 19:04:14 -05:00
|
|
|
[ :after_find, :string ],
|
|
|
|
[ :after_find, :proc ],
|
|
|
|
[ :after_find, :object ],
|
|
|
|
[ :after_find, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_initialize, :method ],
|
2005-03-13 19:04:14 -05:00
|
|
|
[ :after_initialize, :string ],
|
|
|
|
[ :after_initialize, :proc ],
|
|
|
|
[ :after_initialize, :object ],
|
|
|
|
[ :after_initialize, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :before_validation, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :before_validation, :string ],
|
|
|
|
[ :before_validation, :proc ],
|
|
|
|
[ :before_validation, :object ],
|
|
|
|
[ :before_validation, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_validation, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :after_validation, :string ],
|
|
|
|
[ :after_validation, :proc ],
|
|
|
|
[ :after_validation, :object ],
|
|
|
|
[ :after_validation, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :before_save, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :before_save, :string ],
|
|
|
|
[ :before_save, :proc ],
|
|
|
|
[ :before_save, :object ],
|
|
|
|
[ :before_save, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :before_update, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :before_update, :string ],
|
|
|
|
[ :before_update, :proc ],
|
|
|
|
[ :before_update, :object ],
|
|
|
|
[ :before_update, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_update, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :after_update, :string ],
|
|
|
|
[ :after_update, :proc ],
|
|
|
|
[ :after_update, :object ],
|
|
|
|
[ :after_update, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_save, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :after_save, :string ],
|
|
|
|
[ :after_save, :proc ],
|
|
|
|
[ :after_save, :object ],
|
2015-01-11 18:38:22 -05:00
|
|
|
[ :after_save, :block ],
|
|
|
|
[ :after_commit, :block ],
|
|
|
|
[ :after_commit, :object ],
|
|
|
|
[ :after_commit, :proc ],
|
|
|
|
[ :after_commit, :string ],
|
|
|
|
[ :after_commit, :method ]
|
2005-01-01 11:14:15 -05:00
|
|
|
], david.history
|
|
|
|
end
|
|
|
|
|
2010-01-06 16:16:49 -05:00
|
|
|
def test_validate_on_update
|
|
|
|
david = OnCallbacksDeveloper.find(1)
|
|
|
|
david.save
|
|
|
|
assert_equal [
|
|
|
|
:before_validation,
|
|
|
|
:before_validation_on_update,
|
|
|
|
:validate,
|
|
|
|
:after_validation,
|
|
|
|
:after_validation_on_update
|
|
|
|
], david.history
|
|
|
|
end
|
|
|
|
|
2012-07-02 12:18:12 -04:00
|
|
|
def test_validate_on_contextual_update
|
|
|
|
david = ContextualCallbacksDeveloper.find(1)
|
|
|
|
david.save
|
|
|
|
assert_equal [
|
|
|
|
:before_validation,
|
|
|
|
:before_validation_on_update,
|
|
|
|
:validate,
|
|
|
|
:after_validation,
|
|
|
|
:after_validation_on_update
|
|
|
|
], david.history
|
|
|
|
end
|
|
|
|
|
2005-01-01 11:14:15 -05:00
|
|
|
def test_destroy
|
|
|
|
david = CallbackDeveloper.find(1)
|
|
|
|
david.destroy
|
|
|
|
assert_equal [
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_find, :method ],
|
2005-03-13 19:04:14 -05:00
|
|
|
[ :after_find, :string ],
|
|
|
|
[ :after_find, :proc ],
|
|
|
|
[ :after_find, :object ],
|
|
|
|
[ :after_find, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_initialize, :method ],
|
2005-03-13 19:04:14 -05:00
|
|
|
[ :after_initialize, :string ],
|
|
|
|
[ :after_initialize, :proc ],
|
|
|
|
[ :after_initialize, :object ],
|
|
|
|
[ :after_initialize, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :before_destroy, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :before_destroy, :string ],
|
|
|
|
[ :before_destroy, :proc ],
|
|
|
|
[ :before_destroy, :object ],
|
|
|
|
[ :before_destroy, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_destroy, :method ],
|
2005-01-01 11:14:15 -05:00
|
|
|
[ :after_destroy, :string ],
|
|
|
|
[ :after_destroy, :proc ],
|
|
|
|
[ :after_destroy, :object ],
|
2015-01-11 18:38:22 -05:00
|
|
|
[ :after_destroy, :block ],
|
|
|
|
[ :after_commit, :block ],
|
|
|
|
[ :after_commit, :object ],
|
|
|
|
[ :after_commit, :proc ],
|
|
|
|
[ :after_commit, :string ],
|
|
|
|
[ :after_commit, :method ]
|
2005-01-01 11:14:15 -05:00
|
|
|
], david.history
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_delete
|
|
|
|
david = CallbackDeveloper.find(1)
|
|
|
|
CallbackDeveloper.delete(david.id)
|
|
|
|
assert_equal [
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_find, :method ],
|
2005-03-13 19:04:14 -05:00
|
|
|
[ :after_find, :string ],
|
|
|
|
[ :after_find, :proc ],
|
|
|
|
[ :after_find, :object ],
|
|
|
|
[ :after_find, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_initialize, :method ],
|
2005-03-13 19:04:14 -05:00
|
|
|
[ :after_initialize, :string ],
|
|
|
|
[ :after_initialize, :proc ],
|
|
|
|
[ :after_initialize, :object ],
|
|
|
|
[ :after_initialize, :block ],
|
2005-01-01 11:14:15 -05:00
|
|
|
], david.history
|
|
|
|
end
|
2005-10-01 22:00:50 -04:00
|
|
|
|
2014-12-15 01:10:15 -05:00
|
|
|
def test_deprecated_before_save_returning_false
|
2005-10-01 22:00:50 -04:00
|
|
|
david = ImmutableDeveloper.find(1)
|
2014-12-15 01:10:15 -05:00
|
|
|
assert_deprecated do
|
|
|
|
assert david.valid?
|
|
|
|
assert !david.save
|
|
|
|
exc = assert_raise(ActiveRecord::RecordNotSaved) { david.save! }
|
|
|
|
assert_equal exc.record, david
|
2015-01-06 07:31:08 -05:00
|
|
|
assert_equal "Failed to save the record", exc.message
|
2014-12-15 01:10:15 -05:00
|
|
|
end
|
2005-10-01 22:00:50 -04:00
|
|
|
|
2006-02-28 15:37:21 -05:00
|
|
|
david = ImmutableDeveloper.find(1)
|
2005-10-01 22:00:50 -04:00
|
|
|
david.salary = 10_000_000
|
|
|
|
assert !david.valid?
|
|
|
|
assert !david.save
|
2009-03-08 16:11:58 -04:00
|
|
|
assert_raise(ActiveRecord::RecordInvalid) { david.save! }
|
2009-01-09 18:09:50 -05:00
|
|
|
|
|
|
|
someone = CallbackCancellationDeveloper.find(1)
|
|
|
|
someone.cancel_before_save = true
|
2014-12-15 01:10:15 -05:00
|
|
|
assert_deprecated do
|
|
|
|
assert someone.valid?
|
|
|
|
assert !someone.save
|
|
|
|
end
|
2009-01-09 18:09:50 -05:00
|
|
|
assert_save_callbacks_not_called(someone)
|
2005-10-01 22:00:50 -04:00
|
|
|
end
|
|
|
|
|
2014-12-15 01:10:15 -05:00
|
|
|
def test_deprecated_before_create_returning_false
|
2006-10-04 11:38:59 -04:00
|
|
|
someone = CallbackCancellationDeveloper.new
|
2009-01-09 18:09:50 -05:00
|
|
|
someone.cancel_before_create = true
|
2014-12-15 01:10:15 -05:00
|
|
|
assert_deprecated do
|
|
|
|
assert someone.valid?
|
|
|
|
assert !someone.save
|
|
|
|
end
|
2009-01-09 18:09:50 -05:00
|
|
|
assert_save_callbacks_not_called(someone)
|
|
|
|
end
|
|
|
|
|
2014-12-15 01:10:15 -05:00
|
|
|
def test_deprecated_before_update_returning_false
|
2009-01-09 18:09:50 -05:00
|
|
|
someone = CallbackCancellationDeveloper.find(1)
|
|
|
|
someone.cancel_before_update = true
|
2014-12-15 01:10:15 -05:00
|
|
|
assert_deprecated do
|
|
|
|
assert someone.valid?
|
|
|
|
assert !someone.save
|
|
|
|
end
|
2009-01-09 18:09:50 -05:00
|
|
|
assert_save_callbacks_not_called(someone)
|
2006-10-04 11:38:59 -04:00
|
|
|
end
|
|
|
|
|
2014-12-15 01:10:15 -05:00
|
|
|
def test_deprecated_before_destroy_returning_false
|
2005-09-27 20:09:50 -04:00
|
|
|
david = ImmutableDeveloper.find(1)
|
2014-12-15 01:10:15 -05:00
|
|
|
assert_deprecated do
|
|
|
|
assert !david.destroy
|
|
|
|
exc = assert_raise(ActiveRecord::RecordNotDestroyed) { david.destroy! }
|
|
|
|
assert_equal exc.record, david
|
2015-01-06 07:31:08 -05:00
|
|
|
assert_equal "Failed to destroy the record", exc.message
|
2014-12-15 01:10:15 -05:00
|
|
|
end
|
2013-01-18 09:15:19 -05:00
|
|
|
assert_not_nil ImmutableDeveloper.find_by_id(1)
|
2009-01-09 18:09:50 -05:00
|
|
|
|
|
|
|
someone = CallbackCancellationDeveloper.find(1)
|
|
|
|
someone.cancel_before_destroy = true
|
2014-12-15 01:10:15 -05:00
|
|
|
assert_deprecated do
|
|
|
|
assert !someone.destroy
|
|
|
|
assert_raise(ActiveRecord::RecordNotDestroyed) { someone.destroy! }
|
|
|
|
end
|
2009-01-09 18:09:50 -05:00
|
|
|
assert !someone.after_destroy_called
|
|
|
|
end
|
|
|
|
|
|
|
|
def assert_save_callbacks_not_called(someone)
|
|
|
|
assert !someone.after_save_called
|
|
|
|
assert !someone.after_create_called
|
|
|
|
assert !someone.after_update_called
|
2008-01-18 02:30:42 -05:00
|
|
|
end
|
2009-01-09 18:09:50 -05:00
|
|
|
private :assert_save_callbacks_not_called
|
2005-01-15 12:45:16 -05:00
|
|
|
|
2014-12-15 01:10:15 -05:00
|
|
|
def test_before_create_throwing_abort
|
|
|
|
someone = CallbackHaltedDeveloper.new
|
|
|
|
someone.cancel_before_create = true
|
|
|
|
assert someone.valid?
|
|
|
|
assert !someone.save
|
|
|
|
assert_save_callbacks_not_called(someone)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_before_save_throwing_abort
|
|
|
|
david = DeveloperWithCanceledCallbacks.find(1)
|
|
|
|
assert david.valid?
|
|
|
|
assert !david.save
|
|
|
|
exc = assert_raise(ActiveRecord::RecordNotSaved) { david.save! }
|
|
|
|
assert_equal exc.record, david
|
|
|
|
|
|
|
|
david = DeveloperWithCanceledCallbacks.find(1)
|
|
|
|
david.salary = 10_000_000
|
|
|
|
assert !david.valid?
|
|
|
|
assert !david.save
|
|
|
|
assert_raise(ActiveRecord::RecordInvalid) { david.save! }
|
|
|
|
|
|
|
|
someone = CallbackHaltedDeveloper.find(1)
|
|
|
|
someone.cancel_before_save = true
|
|
|
|
assert someone.valid?
|
|
|
|
assert !someone.save
|
|
|
|
assert_save_callbacks_not_called(someone)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_before_update_throwing_abort
|
|
|
|
someone = CallbackHaltedDeveloper.find(1)
|
|
|
|
someone.cancel_before_update = true
|
|
|
|
assert someone.valid?
|
|
|
|
assert !someone.save
|
|
|
|
assert_save_callbacks_not_called(someone)
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_before_destroy_throwing_abort
|
|
|
|
david = DeveloperWithCanceledCallbacks.find(1)
|
|
|
|
assert !david.destroy
|
|
|
|
exc = assert_raise(ActiveRecord::RecordNotDestroyed) { david.destroy! }
|
|
|
|
assert_equal exc.record, david
|
|
|
|
assert_not_nil ImmutableDeveloper.find_by_id(1)
|
|
|
|
|
|
|
|
someone = CallbackHaltedDeveloper.find(1)
|
|
|
|
someone.cancel_before_destroy = true
|
|
|
|
assert !someone.destroy
|
|
|
|
assert_raise(ActiveRecord::RecordNotDestroyed) { someone.destroy! }
|
|
|
|
assert !someone.after_destroy_called
|
|
|
|
end
|
|
|
|
|
2010-03-16 16:51:26 -04:00
|
|
|
def test_callback_returning_false
|
|
|
|
david = CallbackDeveloperWithFalseValidation.find(1)
|
2014-12-15 01:10:15 -05:00
|
|
|
assert_deprecated { david.save }
|
2005-01-15 12:45:16 -05:00
|
|
|
assert_equal [
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_find, :method ],
|
2005-03-13 19:04:14 -05:00
|
|
|
[ :after_find, :string ],
|
|
|
|
[ :after_find, :proc ],
|
|
|
|
[ :after_find, :object ],
|
|
|
|
[ :after_find, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :after_initialize, :method ],
|
2005-03-13 19:04:14 -05:00
|
|
|
[ :after_initialize, :string ],
|
|
|
|
[ :after_initialize, :proc ],
|
|
|
|
[ :after_initialize, :object ],
|
|
|
|
[ :after_initialize, :block ],
|
2009-09-08 11:10:14 -04:00
|
|
|
[ :before_validation, :method ],
|
2005-01-15 12:45:16 -05:00
|
|
|
[ :before_validation, :string ],
|
|
|
|
[ :before_validation, :proc ],
|
|
|
|
[ :before_validation, :object ],
|
|
|
|
[ :before_validation, :block ],
|
2010-08-20 03:06:25 -04:00
|
|
|
[ :before_validation, :returning_false ],
|
|
|
|
[ :after_rollback, :block ],
|
|
|
|
[ :after_rollback, :object ],
|
|
|
|
[ :after_rollback, :proc ],
|
|
|
|
[ :after_rollback, :string ],
|
|
|
|
[ :after_rollback, :method ],
|
2005-01-15 12:45:16 -05:00
|
|
|
], david.history
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2014-12-15 01:10:15 -05:00
|
|
|
def test_callback_throwing_abort
|
|
|
|
david = CallbackDeveloperWithHaltedValidation.find(1)
|
|
|
|
david.save
|
|
|
|
assert_equal [
|
|
|
|
[ :after_find, :method ],
|
|
|
|
[ :after_find, :string ],
|
|
|
|
[ :after_find, :proc ],
|
|
|
|
[ :after_find, :object ],
|
|
|
|
[ :after_find, :block ],
|
|
|
|
[ :after_initialize, :method ],
|
|
|
|
[ :after_initialize, :string ],
|
|
|
|
[ :after_initialize, :proc ],
|
|
|
|
[ :after_initialize, :object ],
|
|
|
|
[ :after_initialize, :block ],
|
|
|
|
[ :before_validation, :method ],
|
|
|
|
[ :before_validation, :string ],
|
|
|
|
[ :before_validation, :proc ],
|
|
|
|
[ :before_validation, :object ],
|
|
|
|
[ :before_validation, :block ],
|
|
|
|
[ :before_validation, :throwing_abort ],
|
|
|
|
[ :after_rollback, :block ],
|
|
|
|
[ :after_rollback, :object ],
|
|
|
|
[ :after_rollback, :proc ],
|
|
|
|
[ :after_rollback, :string ],
|
|
|
|
[ :after_rollback, :method ],
|
|
|
|
], david.history
|
|
|
|
end
|
|
|
|
|
2013-03-20 01:47:01 -04:00
|
|
|
def test_inheritance_of_callbacks
|
2007-01-23 22:04:48 -05:00
|
|
|
parent = ParentDeveloper.new
|
|
|
|
assert !parent.after_save_called
|
|
|
|
parent.save
|
|
|
|
assert parent.after_save_called
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2007-01-23 22:04:48 -05:00
|
|
|
child = ChildDeveloper.new
|
|
|
|
assert !child.after_save_called
|
|
|
|
child.save
|
|
|
|
assert child.after_save_called
|
|
|
|
end
|
2008-01-18 02:30:42 -05:00
|
|
|
|
2005-01-01 11:14:15 -05:00
|
|
|
end
|