2017-07-09 08:06:36 -04:00
|
|
|
# frozen_string_literal: true
|
2017-07-10 09:39:13 -04:00
|
|
|
|
2018-09-29 20:50:43 -04:00
|
|
|
require_relative "abstract_unit"
|
2020-05-09 15:25:26 -04:00
|
|
|
require "active_support/core_ext/kernel/singleton_class"
|
2008-01-18 21:44:45 -05:00
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
module CallbacksTest
|
|
|
|
class Record
|
|
|
|
include ActiveSupport::Callbacks
|
2008-01-18 21:44:45 -05:00
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
define_callbacks :save
|
2008-01-18 21:44:45 -05:00
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
def self.before_save(*filters, &blk)
|
|
|
|
set_callback(:save, :before, *filters, &blk)
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.after_save(*filters, &blk)
|
|
|
|
set_callback(:save, :after, *filters, &blk)
|
|
|
|
end
|
|
|
|
|
|
|
|
class << self
|
|
|
|
def callback_symbol(callback_method)
|
|
|
|
method_name = :"#{callback_method}_method"
|
|
|
|
define_method(method_name) do
|
|
|
|
history << [callback_method, :symbol]
|
|
|
|
end
|
|
|
|
method_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def callback_proc(callback_method)
|
|
|
|
Proc.new { |model| model.history << [callback_method, :proc] }
|
|
|
|
end
|
|
|
|
|
|
|
|
def callback_object(callback_method)
|
|
|
|
klass = Class.new
|
2018-12-20 11:39:18 -05:00
|
|
|
klass.define_method(callback_method) do |model|
|
2009-10-12 23:15:43 -04:00
|
|
|
model.history << [:"#{callback_method}_save", :object]
|
|
|
|
end
|
|
|
|
klass.new
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
def history
|
|
|
|
@history ||= []
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-01-02 06:53:37 -05:00
|
|
|
class CallbackClass
|
|
|
|
def self.before(model)
|
|
|
|
model.history << [:before_save, :class]
|
|
|
|
end
|
2014-12-14 20:55:30 -05:00
|
|
|
|
2013-01-02 06:53:37 -05:00
|
|
|
def self.after(model)
|
|
|
|
model.history << [:after_save, :class]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
class Person < Record
|
2016-08-13 04:43:53 -04:00
|
|
|
attr_accessor :save_fails
|
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
[:before_save, :after_save].each do |callback_method|
|
|
|
|
callback_method_sym = callback_method.to_sym
|
|
|
|
send(callback_method, callback_symbol(callback_method_sym))
|
|
|
|
send(callback_method, callback_proc(callback_method_sym))
|
2016-08-06 12:03:25 -04:00
|
|
|
send(callback_method, callback_object(callback_method_sym.to_s.gsub(/_save/, "")))
|
2013-01-02 06:53:37 -05:00
|
|
|
send(callback_method, CallbackClass)
|
2009-10-12 23:15:43 -04:00
|
|
|
send(callback_method) { |model| model.history << [callback_method_sym, :block] }
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
def save
|
2016-08-13 04:43:53 -04:00
|
|
|
run_callbacks :save do
|
|
|
|
raise "inside save" if save_fails
|
|
|
|
end
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
class PersonSkipper < Person
|
2016-08-06 13:38:33 -04:00
|
|
|
skip_callback :save, :before, :before_save_method, if: :yes
|
|
|
|
skip_callback :save, :after, :after_save_method, unless: :yes
|
|
|
|
skip_callback :save, :after, :after_save_method, if: :no
|
|
|
|
skip_callback :save, :before, :before_save_method, unless: :no
|
2017-12-12 06:00:50 -05:00
|
|
|
skip_callback :save, :before, CallbackClass, if: :yes
|
2009-10-12 23:15:43 -04:00
|
|
|
def yes; true; end
|
|
|
|
def no; false; end
|
|
|
|
end
|
|
|
|
|
2013-05-07 14:22:52 -04:00
|
|
|
class PersonForProgrammaticSkipping < Person
|
|
|
|
end
|
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
class ParentController
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
|
|
|
|
define_callbacks :dispatch
|
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
set_callback :dispatch, :before, :log, unless: proc { |c| c.action_name == :index || c.action_name == :show }
|
2009-10-12 23:15:43 -04:00
|
|
|
set_callback :dispatch, :after, :log2
|
2008-01-18 21:44:45 -05:00
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
attr_reader :action_name, :logger
|
|
|
|
def initialize(action_name)
|
|
|
|
@action_name, @logger = action_name, []
|
|
|
|
end
|
|
|
|
|
|
|
|
def log
|
|
|
|
@logger << action_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def log2
|
|
|
|
@logger << action_name
|
|
|
|
end
|
|
|
|
|
|
|
|
def dispatch
|
2012-05-10 03:21:03 -04:00
|
|
|
run_callbacks :dispatch do
|
2009-10-12 23:15:43 -04:00
|
|
|
@logger << "Done"
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
self
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
class Child < ParentController
|
2016-08-16 03:30:11 -04:00
|
|
|
skip_callback :dispatch, :before, :log, if: proc { |c| c.action_name == :update }
|
2009-10-12 23:15:43 -04:00
|
|
|
skip_callback :dispatch, :after, :log2
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
class OneTimeCompile < Record
|
|
|
|
@@starts_true, @@starts_false = true, false
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
super
|
|
|
|
end
|
|
|
|
|
2016-08-16 03:30:11 -04:00
|
|
|
before_save Proc.new { |r| r.history << [:before_save, :starts_true, :if] }, if: :starts_true
|
|
|
|
before_save Proc.new { |r| r.history << [:before_save, :starts_false, :if] }, if: :starts_false
|
|
|
|
before_save Proc.new { |r| r.history << [:before_save, :starts_true, :unless] }, unless: :starts_true
|
|
|
|
before_save Proc.new { |r| r.history << [:before_save, :starts_false, :unless] }, unless: :starts_false
|
2009-10-12 23:15:43 -04:00
|
|
|
|
|
|
|
def starts_true
|
|
|
|
if @@starts_true
|
|
|
|
@@starts_true = false
|
|
|
|
return true
|
|
|
|
end
|
|
|
|
@@starts_true
|
|
|
|
end
|
|
|
|
|
|
|
|
def starts_false
|
|
|
|
unless @@starts_false
|
|
|
|
@@starts_false = true
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
@@starts_false
|
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
2012-05-10 03:21:03 -04:00
|
|
|
run_callbacks :save
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class OneTimeCompileTest < ActiveSupport::TestCase
|
2009-10-12 23:15:43 -04:00
|
|
|
def test_optimized_first_compile
|
|
|
|
around = OneTimeCompile.new
|
|
|
|
around.save
|
|
|
|
assert_equal [
|
|
|
|
[:before_save, :starts_true, :if],
|
|
|
|
[:before_save, :starts_true, :unless]
|
|
|
|
], around.history
|
|
|
|
end
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
|
|
|
|
2010-09-25 18:31:43 -04:00
|
|
|
class AfterSaveConditionalPerson < Record
|
|
|
|
after_save Proc.new { |r| r.history << [:after_save, :string1] }
|
|
|
|
after_save Proc.new { |r| r.history << [:after_save, :string2] }
|
|
|
|
def save
|
|
|
|
run_callbacks :save
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class AfterSaveConditionalPersonCallbackTest < ActiveSupport::TestCase
|
2010-09-25 18:31:43 -04:00
|
|
|
def test_after_save_runs_in_the_reverse_order
|
|
|
|
person = AfterSaveConditionalPerson.new
|
|
|
|
person.save
|
|
|
|
assert_equal [
|
|
|
|
[:after_save, :string2],
|
|
|
|
[:after_save, :string1]
|
|
|
|
], person.history
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
class ConditionalPerson < Record
|
|
|
|
# proc
|
2016-08-06 13:38:33 -04:00
|
|
|
before_save Proc.new { |r| r.history << [:before_save, :proc] }, if: Proc.new { |r| true }
|
|
|
|
before_save Proc.new { |r| r.history << "b00m" }, if: Proc.new { |r| false }
|
|
|
|
before_save Proc.new { |r| r.history << [:before_save, :proc] }, unless: Proc.new { |r| false }
|
|
|
|
before_save Proc.new { |r| r.history << "b00m" }, unless: Proc.new { |r| true }
|
2009-10-12 23:15:43 -04:00
|
|
|
# symbol
|
2016-08-06 13:38:33 -04:00
|
|
|
before_save Proc.new { |r| r.history << [:before_save, :symbol] }, if: :yes
|
|
|
|
before_save Proc.new { |r| r.history << "b00m" }, if: :no
|
|
|
|
before_save Proc.new { |r| r.history << [:before_save, :symbol] }, unless: :no
|
|
|
|
before_save Proc.new { |r| r.history << "b00m" }, unless: :yes
|
2009-10-12 23:15:43 -04:00
|
|
|
# Combined if and unless
|
2016-08-06 13:38:33 -04:00
|
|
|
before_save Proc.new { |r| r.history << [:before_save, :combined_symbol] }, if: :yes, unless: :no
|
|
|
|
before_save Proc.new { |r| r.history << "b00m" }, if: :yes, unless: :yes
|
2009-10-12 23:15:43 -04:00
|
|
|
|
|
|
|
def yes; true; end
|
|
|
|
def other_yes; true; end
|
|
|
|
def no; false; end
|
|
|
|
def other_no; false; end
|
|
|
|
|
|
|
|
def save
|
|
|
|
run_callbacks :save
|
|
|
|
end
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
class CleanPerson < ConditionalPerson
|
|
|
|
reset_callbacks :save
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
class MySuper
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
define_callbacks :save
|
2008-01-18 21:44:45 -05:00
|
|
|
end
|
2008-03-18 13:56:05 -04:00
|
|
|
|
2016-12-15 03:53:10 -05:00
|
|
|
class MySlate < MySuper
|
2009-10-12 23:15:43 -04:00
|
|
|
attr_reader :history
|
2016-08-13 04:43:53 -04:00
|
|
|
attr_accessor :save_fails
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2016-12-15 03:53:10 -05:00
|
|
|
def initialize
|
|
|
|
@history = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
|
|
|
run_callbacks :save do
|
|
|
|
raise "inside save" if save_fails
|
|
|
|
@history << "running"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def no; false; end
|
|
|
|
def yes; true; end
|
|
|
|
|
|
|
|
def method_missing(sym, *)
|
|
|
|
case sym
|
|
|
|
when /^log_(.*)/
|
|
|
|
@history << $1
|
|
|
|
nil
|
|
|
|
when /^wrap_(.*)/
|
|
|
|
@history << "wrap_#$1"
|
|
|
|
yield
|
|
|
|
@history << "unwrap_#$1"
|
|
|
|
nil
|
|
|
|
when /^double_(.*)/
|
|
|
|
@history << "first_#$1"
|
|
|
|
yield
|
|
|
|
@history << "second_#$1"
|
|
|
|
yield
|
|
|
|
@history << "third_#$1"
|
|
|
|
else
|
|
|
|
super
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def respond_to_missing?(sym)
|
2019-07-29 01:23:10 -04:00
|
|
|
sym.match?(/^(log|wrap)_/) || super
|
2016-12-15 03:53:10 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class AroundPerson < MySlate
|
2016-08-06 13:38:33 -04:00
|
|
|
set_callback :save, :before, :nope, if: :no
|
|
|
|
set_callback :save, :before, :nope, unless: :yes
|
2009-10-12 23:15:43 -04:00
|
|
|
set_callback :save, :after, :tweedle
|
2016-08-16 03:30:11 -04:00
|
|
|
set_callback :save, :before, proc { |m| m.history << "yup" }
|
2016-08-06 13:38:33 -04:00
|
|
|
set_callback :save, :before, :nope, if: proc { false }
|
|
|
|
set_callback :save, :before, :nope, unless: proc { true }
|
|
|
|
set_callback :save, :before, :yup, if: proc { true }
|
|
|
|
set_callback :save, :before, :yup, unless: proc { false }
|
2009-10-12 23:15:43 -04:00
|
|
|
set_callback :save, :around, :tweedle_dum
|
2016-08-06 13:38:33 -04:00
|
|
|
set_callback :save, :around, :w0tyes, if: :yes
|
|
|
|
set_callback :save, :around, :w0tno, if: :no
|
2009-10-12 23:15:43 -04:00
|
|
|
set_callback :save, :around, :tweedle_deedle
|
|
|
|
|
|
|
|
def nope
|
|
|
|
@history << "boom"
|
|
|
|
end
|
|
|
|
|
|
|
|
def yup
|
|
|
|
@history << "yup"
|
|
|
|
end
|
|
|
|
|
|
|
|
def w0tyes
|
|
|
|
@history << "w0tyes before"
|
|
|
|
yield
|
|
|
|
@history << "w0tyes after"
|
|
|
|
end
|
|
|
|
|
|
|
|
def w0tno
|
|
|
|
@history << "boom"
|
|
|
|
yield
|
|
|
|
end
|
|
|
|
|
|
|
|
def tweedle_dum
|
|
|
|
@history << "tweedle dum pre"
|
|
|
|
yield
|
|
|
|
@history << "tweedle dum post"
|
|
|
|
end
|
|
|
|
|
|
|
|
def tweedle
|
|
|
|
@history << "tweedle"
|
|
|
|
end
|
|
|
|
|
|
|
|
def tweedle_deedle
|
|
|
|
@history << "tweedle deedle pre"
|
|
|
|
yield
|
|
|
|
@history << "tweedle deedle post"
|
|
|
|
end
|
2008-03-18 13:56:05 -04:00
|
|
|
end
|
2012-08-15 08:43:04 -04:00
|
|
|
|
2011-03-07 08:07:46 -05:00
|
|
|
class AroundPersonResult < MySuper
|
|
|
|
attr_reader :result
|
|
|
|
|
2011-03-07 08:29:38 -05:00
|
|
|
set_callback :save, :after, :tweedle_1
|
2011-03-07 08:07:46 -05:00
|
|
|
set_callback :save, :around, :tweedle_dum
|
2011-03-07 08:29:38 -05:00
|
|
|
set_callback :save, :after, :tweedle_2
|
2011-03-07 08:07:46 -05:00
|
|
|
|
|
|
|
def tweedle_dum
|
|
|
|
@result = yield
|
|
|
|
end
|
2012-08-15 08:43:04 -04:00
|
|
|
|
2011-03-07 08:29:38 -05:00
|
|
|
def tweedle_1
|
|
|
|
:tweedle_1
|
|
|
|
end
|
2011-03-07 08:07:46 -05:00
|
|
|
|
2011-03-07 08:29:38 -05:00
|
|
|
def tweedle_2
|
|
|
|
:tweedle_2
|
|
|
|
end
|
2012-08-15 08:43:04 -04:00
|
|
|
|
2011-03-07 08:07:46 -05:00
|
|
|
def save
|
|
|
|
run_callbacks :save do
|
|
|
|
:running
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-03-18 13:56:05 -04:00
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
class HyphenatedCallbacks
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
define_callbacks :save
|
|
|
|
attr_reader :stuff
|
|
|
|
|
2016-08-06 13:38:33 -04:00
|
|
|
set_callback :save, :before, :action, if: :yes
|
2009-10-12 23:15:43 -04:00
|
|
|
|
|
|
|
def yes() true end
|
|
|
|
|
2010-01-31 00:27:24 -05:00
|
|
|
def action
|
|
|
|
@stuff = "ACTION"
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
2012-05-10 03:21:03 -04:00
|
|
|
run_callbacks :save do
|
2009-10-12 23:15:43 -04:00
|
|
|
@stuff
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-21 13:23:40 -05:00
|
|
|
module ExtendModule
|
|
|
|
def self.extended(base)
|
|
|
|
base.class_eval do
|
|
|
|
set_callback :save, :before, :record3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def record3
|
|
|
|
@recorder << 3
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
module IncludeModule
|
|
|
|
def self.included(base)
|
|
|
|
base.class_eval do
|
|
|
|
set_callback :save, :before, :record2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
def record2
|
|
|
|
@recorder << 2
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class ExtendCallbacks
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
|
|
|
|
define_callbacks :save
|
|
|
|
set_callback :save, :before, :record1
|
|
|
|
|
|
|
|
include IncludeModule
|
|
|
|
|
|
|
|
def save
|
|
|
|
run_callbacks :save
|
|
|
|
end
|
|
|
|
|
|
|
|
attr_reader :recorder
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@recorder = []
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
def record1
|
|
|
|
@recorder << 1
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class AroundCallbacksTest < ActiveSupport::TestCase
|
2009-10-12 23:15:43 -04:00
|
|
|
def test_save_around
|
|
|
|
around = AroundPerson.new
|
|
|
|
around.save
|
|
|
|
assert_equal [
|
|
|
|
"yup", "yup",
|
|
|
|
"tweedle dum pre",
|
|
|
|
"w0tyes before",
|
|
|
|
"tweedle deedle pre",
|
|
|
|
"running",
|
|
|
|
"tweedle deedle post",
|
|
|
|
"w0tyes after",
|
|
|
|
"tweedle dum post",
|
|
|
|
"tweedle"
|
|
|
|
], around.history
|
|
|
|
end
|
|
|
|
end
|
2012-08-15 08:43:04 -04:00
|
|
|
|
2016-12-15 03:53:10 -05:00
|
|
|
class DoubleYieldTest < ActiveSupport::TestCase
|
|
|
|
class DoubleYieldModel < MySlate
|
|
|
|
set_callback :save, :around, :wrap_outer
|
|
|
|
set_callback :save, :around, :double_trouble
|
|
|
|
set_callback :save, :around, :wrap_inner
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_double_save
|
|
|
|
double = DoubleYieldModel.new
|
|
|
|
double.save
|
|
|
|
assert_equal [
|
|
|
|
"wrap_outer",
|
|
|
|
"first_trouble",
|
|
|
|
"wrap_inner",
|
|
|
|
"running",
|
|
|
|
"unwrap_inner",
|
|
|
|
"second_trouble",
|
|
|
|
"wrap_inner",
|
|
|
|
"running",
|
|
|
|
"unwrap_inner",
|
|
|
|
"third_trouble",
|
|
|
|
"unwrap_outer",
|
|
|
|
], double.history
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-08-13 04:43:53 -04:00
|
|
|
class CallStackTest < ActiveSupport::TestCase
|
|
|
|
def test_tidy_call_stack
|
|
|
|
around = AroundPerson.new
|
|
|
|
around.save_fails = true
|
|
|
|
|
|
|
|
exception = (around.save rescue $!)
|
|
|
|
|
|
|
|
# Make sure we have the exception we're expecting
|
|
|
|
assert_equal "inside save", exception.message
|
|
|
|
|
|
|
|
call_stack = exception.backtrace_locations
|
|
|
|
call_stack.pop caller_locations(0).size
|
|
|
|
|
|
|
|
# Yes, this looks like an implementation test, but it's the least
|
|
|
|
# obtuse way of asserting that there aren't a load of entries in
|
|
|
|
# the call stack for each callback.
|
|
|
|
#
|
|
|
|
# If you've renamed a method, or squeezed more lines out, go ahead
|
|
|
|
# and update this assertion. But if you're here because a
|
|
|
|
# refactoring added new lines, please reconsider.
|
|
|
|
|
|
|
|
# As shown here, our current budget is one line for run_callbacks
|
|
|
|
# itself, plus N+1 lines where N is the number of :around
|
|
|
|
# callbacks that have been invoked, if there are any (plus
|
|
|
|
# whatever the callbacks do themselves, of course).
|
|
|
|
|
|
|
|
assert_equal [
|
|
|
|
"block in save",
|
|
|
|
"block in run_callbacks",
|
|
|
|
"tweedle_deedle",
|
|
|
|
"block in run_callbacks",
|
|
|
|
"w0tyes",
|
|
|
|
"block in run_callbacks",
|
|
|
|
"tweedle_dum",
|
|
|
|
"block in run_callbacks",
|
|
|
|
"run_callbacks",
|
|
|
|
"save"
|
2018-02-16 20:14:27 -05:00
|
|
|
], call_stack.map(&:label)
|
2016-08-13 04:43:53 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def test_short_call_stack
|
|
|
|
person = Person.new
|
|
|
|
person.save_fails = true
|
|
|
|
|
|
|
|
exception = (person.save rescue $!)
|
|
|
|
|
|
|
|
# Make sure we have the exception we're expecting
|
|
|
|
assert_equal "inside save", exception.message
|
|
|
|
|
|
|
|
call_stack = exception.backtrace_locations
|
|
|
|
call_stack.pop caller_locations(0).size
|
|
|
|
|
|
|
|
# This budget much simpler: with no :around callbacks invoked,
|
|
|
|
# there should be just one line. run_callbacks yields directly
|
|
|
|
# back to its caller.
|
|
|
|
|
|
|
|
assert_equal [
|
|
|
|
"block in save",
|
|
|
|
"run_callbacks",
|
|
|
|
"save"
|
|
|
|
], call_stack.map(&:label)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class AroundCallbackResultTest < ActiveSupport::TestCase
|
2011-03-07 08:07:46 -05:00
|
|
|
def test_save_around
|
|
|
|
around = AroundPersonResult.new
|
|
|
|
around.save
|
|
|
|
assert_equal :running, around.result
|
|
|
|
end
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class SkipCallbacksTest < ActiveSupport::TestCase
|
2009-10-12 23:15:43 -04:00
|
|
|
def test_skip_person
|
|
|
|
person = PersonSkipper.new
|
|
|
|
assert_equal [], person.history
|
|
|
|
person.save
|
|
|
|
assert_equal [
|
|
|
|
[:before_save, :proc],
|
|
|
|
[:before_save, :object],
|
|
|
|
[:before_save, :block],
|
|
|
|
[:after_save, :block],
|
2013-01-02 06:53:37 -05:00
|
|
|
[:after_save, :class],
|
2009-10-12 23:15:43 -04:00
|
|
|
[:after_save, :object],
|
|
|
|
[:after_save, :proc],
|
|
|
|
[:after_save, :symbol]
|
|
|
|
], person.history
|
|
|
|
end
|
2013-05-07 14:22:52 -04:00
|
|
|
|
|
|
|
def test_skip_person_programmatically
|
|
|
|
PersonForProgrammaticSkipping._save_callbacks.each do |save_callback|
|
|
|
|
if "before" == save_callback.kind.to_s
|
|
|
|
PersonForProgrammaticSkipping.skip_callback("save", save_callback.kind, save_callback.filter)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
person = PersonForProgrammaticSkipping.new
|
|
|
|
assert_equal [], person.history
|
|
|
|
person.save
|
|
|
|
assert_equal [
|
|
|
|
[:after_save, :block],
|
|
|
|
[:after_save, :class],
|
|
|
|
[:after_save, :object],
|
|
|
|
[:after_save, :proc],
|
|
|
|
[:after_save, :symbol]
|
|
|
|
], person.history
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class CallbacksTest < ActiveSupport::TestCase
|
2009-10-12 23:15:43 -04:00
|
|
|
def test_save_person
|
|
|
|
person = Person.new
|
|
|
|
assert_equal [], person.history
|
|
|
|
person.save
|
|
|
|
assert_equal [
|
|
|
|
[:before_save, :symbol],
|
|
|
|
[:before_save, :proc],
|
|
|
|
[:before_save, :object],
|
2013-01-02 06:53:37 -05:00
|
|
|
[:before_save, :class],
|
2009-10-12 23:15:43 -04:00
|
|
|
[:before_save, :block],
|
|
|
|
[:after_save, :block],
|
2013-01-02 06:53:37 -05:00
|
|
|
[:after_save, :class],
|
2009-10-12 23:15:43 -04:00
|
|
|
[:after_save, :object],
|
|
|
|
[:after_save, :proc],
|
|
|
|
[:after_save, :symbol]
|
|
|
|
], person.history
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class ConditionalCallbackTest < ActiveSupport::TestCase
|
2009-10-12 23:15:43 -04:00
|
|
|
def test_save_conditional_person
|
|
|
|
person = ConditionalPerson.new
|
|
|
|
person.save
|
|
|
|
assert_equal [
|
|
|
|
[:before_save, :proc],
|
|
|
|
[:before_save, :proc],
|
|
|
|
[:before_save, :symbol],
|
|
|
|
[:before_save, :symbol],
|
|
|
|
[:before_save, :combined_symbol],
|
|
|
|
], person.history
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class ResetCallbackTest < ActiveSupport::TestCase
|
2009-10-12 23:15:43 -04:00
|
|
|
def test_save_conditional_person
|
|
|
|
person = CleanPerson.new
|
|
|
|
person.save
|
|
|
|
assert_equal [], person.history
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-12-14 20:55:30 -05:00
|
|
|
class AbstractCallbackTerminator
|
2009-10-12 23:15:43 -04:00
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
|
2014-12-14 20:55:30 -05:00
|
|
|
def self.set_save_callbacks
|
|
|
|
set_callback :save, :before, :first
|
|
|
|
set_callback :save, :before, :second
|
|
|
|
set_callback :save, :around, :around_it
|
|
|
|
set_callback :save, :before, :third
|
|
|
|
set_callback :save, :after, :first
|
|
|
|
set_callback :save, :around, :around_it
|
|
|
|
set_callback :save, :after, :third
|
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
2020-02-25 21:09:00 -05:00
|
|
|
attr_reader :history, :saved, :halted, :callback_name
|
2009-10-12 23:15:43 -04:00
|
|
|
def initialize
|
|
|
|
@history = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def around_it
|
|
|
|
@history << "around1"
|
|
|
|
yield
|
|
|
|
@history << "around2"
|
|
|
|
end
|
|
|
|
|
|
|
|
def first
|
|
|
|
@history << "first"
|
|
|
|
end
|
|
|
|
|
|
|
|
def second
|
|
|
|
@history << "second"
|
|
|
|
:halt
|
|
|
|
end
|
|
|
|
|
|
|
|
def third
|
|
|
|
@history << "third"
|
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
|
|
|
run_callbacks :save do
|
|
|
|
@saved = true
|
|
|
|
end
|
|
|
|
end
|
2011-11-30 03:52:52 -05:00
|
|
|
|
2020-02-25 21:09:00 -05:00
|
|
|
def halted_callback_hook(filter, name)
|
2011-11-30 03:52:52 -05:00
|
|
|
@halted = filter
|
2020-02-25 21:09:00 -05:00
|
|
|
@callback_name = name
|
2011-11-30 03:52:52 -05:00
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
|
|
|
|
2014-12-14 20:55:30 -05:00
|
|
|
class CallbackTerminator < AbstractCallbackTerminator
|
2014-10-16 19:21:24 -04:00
|
|
|
define_callbacks :save, terminator: ->(_, result_lambda) { result_lambda.call == :halt }
|
2014-12-14 20:55:30 -05:00
|
|
|
set_save_callbacks
|
|
|
|
end
|
|
|
|
|
|
|
|
class CallbackTerminatorSkippingAfterCallbacks < AbstractCallbackTerminator
|
2014-10-16 19:21:24 -04:00
|
|
|
define_callbacks :save, terminator: ->(_, result_lambda) { result_lambda.call == :halt },
|
2014-12-14 20:55:30 -05:00
|
|
|
skip_after_callbacks_if_terminated: true
|
|
|
|
set_save_callbacks
|
|
|
|
end
|
|
|
|
|
2014-10-16 19:21:24 -04:00
|
|
|
class CallbackDefaultTerminator < AbstractCallbackTerminator
|
|
|
|
define_callbacks :save
|
|
|
|
|
|
|
|
def second
|
|
|
|
@history << "second"
|
|
|
|
throw(:abort)
|
|
|
|
end
|
|
|
|
|
|
|
|
set_save_callbacks
|
|
|
|
end
|
|
|
|
|
2014-12-24 03:58:19 -05:00
|
|
|
class CallbackFalseTerminator < AbstractCallbackTerminator
|
|
|
|
define_callbacks :save
|
|
|
|
|
|
|
|
def second
|
|
|
|
@history << "second"
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
set_save_callbacks
|
|
|
|
end
|
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
class CallbackObject
|
|
|
|
def before(caller)
|
|
|
|
caller.record << "before"
|
|
|
|
end
|
|
|
|
|
|
|
|
def before_save(caller)
|
|
|
|
caller.record << "before save"
|
|
|
|
end
|
|
|
|
|
|
|
|
def around(caller)
|
|
|
|
caller.record << "around before"
|
|
|
|
yield
|
|
|
|
caller.record << "around after"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class UsingObjectBefore
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
|
|
|
|
define_callbacks :save
|
|
|
|
set_callback :save, :before, CallbackObject.new
|
|
|
|
|
|
|
|
attr_accessor :record
|
|
|
|
def initialize
|
|
|
|
@record = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
|
|
|
run_callbacks :save do
|
|
|
|
@record << "yielded"
|
|
|
|
end
|
|
|
|
end
|
2008-03-18 13:56:05 -04:00
|
|
|
end
|
2008-04-18 00:30:01 -04:00
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
class UsingObjectAround
|
|
|
|
include ActiveSupport::Callbacks
|
2008-04-18 00:30:01 -04:00
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
define_callbacks :save
|
|
|
|
set_callback :save, :around, CallbackObject.new
|
|
|
|
|
|
|
|
attr_accessor :record
|
|
|
|
def initialize
|
|
|
|
@record = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
|
|
|
run_callbacks :save do
|
|
|
|
@record << "yielded"
|
|
|
|
end
|
|
|
|
end
|
2008-04-18 00:30:01 -04:00
|
|
|
end
|
|
|
|
|
2009-10-12 23:15:43 -04:00
|
|
|
class CustomScopeObject
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
|
2016-08-06 13:38:33 -04:00
|
|
|
define_callbacks :save, scope: [:kind, :name]
|
2009-10-12 23:15:43 -04:00
|
|
|
set_callback :save, :before, CallbackObject.new
|
|
|
|
|
|
|
|
attr_accessor :record
|
|
|
|
def initialize
|
|
|
|
@record = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
|
|
|
run_callbacks :save do
|
|
|
|
@record << "yielded"
|
|
|
|
"CallbackResult"
|
|
|
|
end
|
|
|
|
end
|
2008-04-18 00:30:01 -04:00
|
|
|
end
|
|
|
|
|
2012-08-15 08:43:04 -04:00
|
|
|
class OneTwoThreeSave
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
|
|
|
|
define_callbacks :save
|
|
|
|
|
|
|
|
attr_accessor :record
|
|
|
|
|
|
|
|
def initialize
|
|
|
|
@record = []
|
|
|
|
end
|
|
|
|
|
|
|
|
def save
|
|
|
|
run_callbacks :save do
|
|
|
|
@record << "yielded"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def first
|
|
|
|
@record << "one"
|
|
|
|
end
|
|
|
|
|
|
|
|
def second
|
|
|
|
@record << "two"
|
|
|
|
end
|
|
|
|
|
|
|
|
def third
|
|
|
|
@record << "three"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
class DuplicatingCallbacks < OneTwoThreeSave
|
|
|
|
set_callback :save, :before, :first, :second
|
|
|
|
set_callback :save, :before, :first, :third
|
|
|
|
end
|
|
|
|
|
|
|
|
class DuplicatingCallbacksInSameCall < OneTwoThreeSave
|
|
|
|
set_callback :save, :before, :first, :second, :first, :third
|
|
|
|
end
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class UsingObjectTest < ActiveSupport::TestCase
|
2009-10-12 23:15:43 -04:00
|
|
|
def test_before_object
|
|
|
|
u = UsingObjectBefore.new
|
|
|
|
u.save
|
|
|
|
assert_equal ["before", "yielded"], u.record
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_around_object
|
|
|
|
u = UsingObjectAround.new
|
|
|
|
u.save
|
|
|
|
assert_equal ["around before", "yielded", "around after"], u.record
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_customized_object
|
|
|
|
u = CustomScopeObject.new
|
|
|
|
u.save
|
|
|
|
assert_equal ["before save", "yielded"], u.record
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_block_result_is_returned
|
|
|
|
u = CustomScopeObject.new
|
|
|
|
assert_equal "CallbackResult", u.save
|
|
|
|
end
|
2008-04-18 00:30:01 -04:00
|
|
|
end
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class CallbackTerminatorTest < ActiveSupport::TestCase
|
2014-12-14 20:55:30 -05:00
|
|
|
def test_termination_skips_following_before_and_around_callbacks
|
2009-10-12 23:15:43 -04:00
|
|
|
terminator = CallbackTerminator.new
|
|
|
|
terminator.save
|
2014-10-16 19:21:24 -04:00
|
|
|
assert_equal ["first", "second", "third", "first"], terminator.history
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
2011-11-30 03:52:52 -05:00
|
|
|
|
|
|
|
def test_termination_invokes_hook
|
|
|
|
terminator = CallbackTerminator.new
|
|
|
|
terminator.save
|
2013-05-13 15:05:22 -04:00
|
|
|
assert_equal :second, terminator.halted
|
2020-02-25 21:09:00 -05:00
|
|
|
assert_equal :save, terminator.callback_name
|
2011-11-30 03:52:52 -05:00
|
|
|
end
|
2009-10-12 23:15:43 -04:00
|
|
|
|
|
|
|
def test_block_never_called_if_terminated
|
|
|
|
obj = CallbackTerminator.new
|
|
|
|
obj.save
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not obj.saved
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
2008-04-18 00:30:01 -04:00
|
|
|
end
|
|
|
|
|
2014-12-14 20:55:30 -05:00
|
|
|
class CallbackTerminatorSkippingAfterCallbacksTest < ActiveSupport::TestCase
|
|
|
|
def test_termination_skips_after_callbacks
|
|
|
|
terminator = CallbackTerminatorSkippingAfterCallbacks.new
|
|
|
|
terminator.save
|
|
|
|
assert_equal ["first", "second"], terminator.history
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2014-10-16 19:21:24 -04:00
|
|
|
class CallbackDefaultTerminatorTest < ActiveSupport::TestCase
|
|
|
|
def test_default_termination
|
|
|
|
terminator = CallbackDefaultTerminator.new
|
|
|
|
terminator.save
|
|
|
|
assert_equal ["first", "second", "third", "first"], terminator.history
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_default_termination_invokes_hook
|
|
|
|
terminator = CallbackDefaultTerminator.new
|
|
|
|
terminator.save
|
|
|
|
assert_equal :second, terminator.halted
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_block_never_called_if_abort_is_thrown
|
|
|
|
obj = CallbackDefaultTerminator.new
|
|
|
|
obj.save
|
2018-04-17 18:21:34 -04:00
|
|
|
assert_not obj.saved
|
2014-10-16 19:21:24 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-02-07 10:13:15 -05:00
|
|
|
class CallbackFalseTerminatorTest < ActiveSupport::TestCase
|
|
|
|
def test_returning_false_does_not_halt_callback
|
2014-12-24 03:58:19 -05:00
|
|
|
obj = CallbackFalseTerminator.new
|
|
|
|
obj.save
|
2016-12-24 12:29:52 -05:00
|
|
|
assert_nil obj.halted
|
2014-12-24 03:58:19 -05:00
|
|
|
assert obj.saved
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class HyphenatedKeyTest < ActiveSupport::TestCase
|
2009-10-12 23:15:43 -04:00
|
|
|
def test_save
|
|
|
|
obj = HyphenatedCallbacks.new
|
|
|
|
obj.save
|
2010-05-19 15:14:51 -04:00
|
|
|
assert_equal "ACTION", obj.stuff
|
2009-10-12 23:15:43 -04:00
|
|
|
end
|
2008-04-18 00:30:01 -04:00
|
|
|
end
|
2010-08-05 18:09:23 -04:00
|
|
|
|
|
|
|
class WriterSkipper < Person
|
|
|
|
attr_accessor :age
|
2016-08-07 19:05:28 -04:00
|
|
|
skip_callback :save, :before, :before_save_method, if: -> { age > 21 }
|
2010-08-05 18:09:23 -04:00
|
|
|
end
|
|
|
|
|
2012-01-05 20:12:46 -05:00
|
|
|
class WriterCallbacksTest < ActiveSupport::TestCase
|
2010-08-05 18:09:23 -04:00
|
|
|
def test_skip_writer
|
|
|
|
writer = WriterSkipper.new
|
|
|
|
writer.age = 18
|
|
|
|
assert_equal [], writer.history
|
|
|
|
writer.save
|
|
|
|
assert_equal [
|
|
|
|
[:before_save, :symbol],
|
|
|
|
[:before_save, :proc],
|
|
|
|
[:before_save, :object],
|
2013-01-02 06:53:37 -05:00
|
|
|
[:before_save, :class],
|
2010-08-05 18:09:23 -04:00
|
|
|
[:before_save, :block],
|
|
|
|
[:after_save, :block],
|
2013-01-02 06:53:37 -05:00
|
|
|
[:after_save, :class],
|
2010-08-05 18:09:23 -04:00
|
|
|
[:after_save, :object],
|
|
|
|
[:after_save, :proc],
|
|
|
|
[:after_save, :symbol]
|
|
|
|
], writer.history
|
|
|
|
end
|
|
|
|
end
|
2010-12-16 22:04:20 -05:00
|
|
|
|
2012-01-21 13:23:40 -05:00
|
|
|
class ExtendCallbacksTest < ActiveSupport::TestCase
|
|
|
|
def test_save
|
|
|
|
model = ExtendCallbacks.new.extend ExtendModule
|
|
|
|
model.save
|
|
|
|
assert_equal [1, 2, 3], model.recorder
|
|
|
|
end
|
|
|
|
end
|
2012-02-04 06:31:00 -05:00
|
|
|
|
2012-08-15 08:43:04 -04:00
|
|
|
class ExcludingDuplicatesCallbackTest < ActiveSupport::TestCase
|
|
|
|
def test_excludes_duplicates_in_separate_calls
|
|
|
|
model = DuplicatingCallbacks.new
|
|
|
|
model.save
|
|
|
|
assert_equal ["two", "one", "three", "yielded"], model.record
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_excludes_duplicates_in_one_call
|
|
|
|
model = DuplicatingCallbacksInSameCall.new
|
|
|
|
model.save
|
|
|
|
assert_equal ["two", "one", "three", "yielded"], model.record
|
|
|
|
end
|
|
|
|
end
|
2013-05-08 13:07:19 -04:00
|
|
|
|
2013-05-13 18:40:39 -04:00
|
|
|
class CallbackProcTest < ActiveSupport::TestCase
|
|
|
|
def build_class(callback)
|
|
|
|
Class.new {
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
define_callbacks :foo
|
|
|
|
set_callback :foo, :before, callback
|
|
|
|
def run; run_callbacks :foo; end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proc_arity_0
|
|
|
|
calls = []
|
|
|
|
klass = build_class(->() { calls << :foo })
|
|
|
|
klass.new.run
|
|
|
|
assert_equal [:foo], calls
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proc_arity_1
|
|
|
|
calls = []
|
|
|
|
klass = build_class(->(o) { calls << o })
|
|
|
|
instance = klass.new
|
|
|
|
instance.run
|
|
|
|
assert_equal [instance], calls
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proc_arity_2
|
|
|
|
assert_raises(ArgumentError) do
|
2018-09-25 13:18:20 -04:00
|
|
|
klass = build_class(->(x, y) { })
|
2013-05-13 18:40:39 -04:00
|
|
|
klass.new.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proc_negative_called_with_empty_list
|
|
|
|
calls = []
|
|
|
|
klass = build_class(->(*args) { calls << args })
|
|
|
|
klass.new.run
|
|
|
|
assert_equal [[]], calls
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-08 17:35:43 -04:00
|
|
|
class ConditionalTests < ActiveSupport::TestCase
|
|
|
|
def build_class(callback)
|
|
|
|
Class.new {
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
define_callbacks :foo
|
2016-08-06 13:38:33 -04:00
|
|
|
set_callback :foo, :before, :foo, if: callback
|
2013-05-08 17:35:43 -04:00
|
|
|
def foo; end
|
|
|
|
def run; run_callbacks :foo; end
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-05-08 18:23:39 -04:00
|
|
|
# FIXME: do we really want to support classes as conditionals? There were
|
|
|
|
# no tests for it previous to this.
|
2013-05-08 18:51:45 -04:00
|
|
|
def test_class_conditional_with_scope
|
|
|
|
z = []
|
|
|
|
callback = Class.new {
|
|
|
|
define_singleton_method(:foo) { |o| z << o }
|
|
|
|
}
|
|
|
|
klass = Class.new {
|
|
|
|
include ActiveSupport::Callbacks
|
2016-08-06 13:38:33 -04:00
|
|
|
define_callbacks :foo, scope: [:name]
|
|
|
|
set_callback :foo, :before, :foo, if: callback
|
2013-05-08 18:51:45 -04:00
|
|
|
def run; run_callbacks :foo; end
|
2019-06-12 08:30:49 -04:00
|
|
|
|
2013-05-14 13:48:34 -04:00
|
|
|
private
|
2016-12-28 21:53:51 -05:00
|
|
|
def foo; end
|
2013-05-08 18:51:45 -04:00
|
|
|
}
|
|
|
|
object = klass.new
|
|
|
|
object.run
|
|
|
|
assert_equal [object], z
|
|
|
|
end
|
|
|
|
|
|
|
|
# FIXME: do we really want to support classes as conditionals? There were
|
|
|
|
# no tests for it previous to this.
|
2013-05-08 18:23:39 -04:00
|
|
|
def test_class
|
|
|
|
z = []
|
|
|
|
klass = build_class Class.new {
|
|
|
|
define_singleton_method(:before) { |o| z << o }
|
|
|
|
}
|
|
|
|
object = klass.new
|
|
|
|
object.run
|
|
|
|
assert_equal [object], z
|
|
|
|
end
|
|
|
|
|
2013-05-08 17:35:43 -04:00
|
|
|
def test_proc_negative_arity # passes an empty list if *args
|
|
|
|
z = []
|
|
|
|
object = build_class(->(*args) { z << args }).new
|
|
|
|
object.run
|
|
|
|
assert_equal [], z.flatten
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proc_arity0
|
|
|
|
z = []
|
|
|
|
object = build_class(->() { z << 0 }).new
|
|
|
|
object.run
|
|
|
|
assert_equal [0], z
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proc_arity1
|
|
|
|
z = []
|
|
|
|
object = build_class(->(x) { z << x }).new
|
|
|
|
object.run
|
|
|
|
assert_equal [object], z
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_proc_arity2
|
|
|
|
assert_raises(ArgumentError) do
|
2018-09-25 13:18:20 -04:00
|
|
|
object = build_class(->(a, b) { }).new
|
2013-05-08 17:35:43 -04:00
|
|
|
object.run
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-13 14:44:15 -04:00
|
|
|
class ResetCallbackTest < ActiveSupport::TestCase
|
|
|
|
def build_class(memo)
|
|
|
|
klass = Class.new {
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
define_callbacks :foo
|
|
|
|
set_callback :foo, :before, :hello
|
|
|
|
def run; run_callbacks :foo; end
|
|
|
|
}
|
|
|
|
klass.class_eval {
|
|
|
|
define_method(:hello) { memo << :hi }
|
|
|
|
}
|
|
|
|
klass
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_reset_callbacks
|
|
|
|
events = []
|
|
|
|
klass = build_class events
|
|
|
|
klass.new.run
|
|
|
|
assert_equal 1, events.length
|
|
|
|
|
|
|
|
klass.reset_callbacks :foo
|
|
|
|
klass.new.run
|
|
|
|
assert_equal 1, events.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_reset_impacts_subclasses
|
|
|
|
events = []
|
|
|
|
klass = build_class events
|
|
|
|
subclass = Class.new(klass) { set_callback :foo, :before, :world }
|
|
|
|
subclass.class_eval { define_method(:world) { events << :world } }
|
|
|
|
|
|
|
|
subclass.new.run
|
|
|
|
assert_equal 2, events.length
|
|
|
|
|
|
|
|
klass.reset_callbacks :foo
|
|
|
|
subclass.new.run
|
|
|
|
assert_equal 3, events.length
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-05-08 13:31:02 -04:00
|
|
|
class CallbackTypeTest < ActiveSupport::TestCase
|
2013-05-08 13:07:19 -04:00
|
|
|
def build_class(callback, n = 10)
|
|
|
|
Class.new {
|
|
|
|
include ActiveSupport::Callbacks
|
|
|
|
define_callbacks :foo
|
2013-05-08 14:31:25 -04:00
|
|
|
n.times { set_callback :foo, :before, callback }
|
2013-05-08 13:08:37 -04:00
|
|
|
def run; run_callbacks :foo; end
|
2015-02-21 09:37:07 -05:00
|
|
|
def self.skip(*things); skip_callback :foo, :before, *things; end
|
2013-05-08 13:07:19 -04:00
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2013-05-08 13:31:02 -04:00
|
|
|
def test_add_class
|
2013-05-08 13:07:19 -04:00
|
|
|
calls = []
|
|
|
|
callback = Class.new {
|
|
|
|
define_singleton_method(:before) { |o| calls << o }
|
|
|
|
}
|
2013-05-08 13:08:37 -04:00
|
|
|
build_class(callback).new.run
|
2013-05-08 13:07:19 -04:00
|
|
|
assert_equal 10, calls.length
|
|
|
|
end
|
|
|
|
|
2013-05-08 13:31:02 -04:00
|
|
|
def test_add_lambda
|
2013-05-08 13:07:19 -04:00
|
|
|
calls = []
|
2013-05-08 13:08:37 -04:00
|
|
|
build_class(->(o) { calls << o }).new.run
|
2013-05-08 13:07:19 -04:00
|
|
|
assert_equal 10, calls.length
|
|
|
|
end
|
|
|
|
|
2013-05-08 13:31:02 -04:00
|
|
|
def test_add_symbol
|
2013-05-08 13:07:19 -04:00
|
|
|
calls = []
|
|
|
|
klass = build_class(:bar)
|
|
|
|
klass.class_eval { define_method(:bar) { calls << klass } }
|
2013-05-08 13:08:37 -04:00
|
|
|
klass.new.run
|
2013-05-08 13:07:19 -04:00
|
|
|
assert_equal 1, calls.length
|
|
|
|
end
|
|
|
|
|
2013-05-08 13:31:02 -04:00
|
|
|
def test_skip_class # removes one at a time
|
|
|
|
calls = []
|
|
|
|
callback = Class.new {
|
|
|
|
define_singleton_method(:before) { |o| calls << o }
|
|
|
|
}
|
|
|
|
klass = build_class(callback)
|
|
|
|
9.downto(0) { |i|
|
|
|
|
klass.skip callback
|
|
|
|
klass.new.run
|
|
|
|
assert_equal i, calls.length
|
|
|
|
calls.clear
|
|
|
|
}
|
|
|
|
end
|
|
|
|
|
2015-02-21 09:37:07 -05:00
|
|
|
def test_skip_lambda # raises error
|
2013-05-08 13:31:02 -04:00
|
|
|
calls = []
|
|
|
|
callback = ->(o) { calls << o }
|
|
|
|
klass = build_class(callback)
|
2015-02-21 09:37:07 -05:00
|
|
|
assert_raises(ArgumentError) { klass.skip callback }
|
2013-05-08 13:31:02 -04:00
|
|
|
klass.new.run
|
|
|
|
assert_equal 10, calls.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_skip_symbol # removes all
|
|
|
|
calls = []
|
|
|
|
klass = build_class(:bar)
|
|
|
|
klass.class_eval { define_method(:bar) { calls << klass } }
|
|
|
|
klass.skip :bar
|
|
|
|
klass.new.run
|
|
|
|
assert_equal 0, calls.length
|
|
|
|
end
|
|
|
|
|
2015-02-21 09:37:07 -05:00
|
|
|
def test_skip_string # raises error
|
2013-05-08 13:31:02 -04:00
|
|
|
calls = []
|
2017-01-08 05:45:58 -05:00
|
|
|
klass = build_class(:bar)
|
2013-05-08 13:31:02 -04:00
|
|
|
klass.class_eval { define_method(:bar) { calls << klass } }
|
2015-02-21 09:37:07 -05:00
|
|
|
assert_raises(ArgumentError) { klass.skip "bar" }
|
|
|
|
klass.new.run
|
|
|
|
assert_equal 1, calls.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_skip_undefined_callback # raises error
|
|
|
|
calls = []
|
|
|
|
klass = build_class(:bar)
|
|
|
|
klass.class_eval { define_method(:bar) { calls << klass } }
|
|
|
|
assert_raises(ArgumentError) { klass.skip :qux }
|
|
|
|
klass.new.run
|
|
|
|
assert_equal 1, calls.length
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_skip_without_raise # removes nothing
|
|
|
|
calls = []
|
|
|
|
klass = build_class(:bar)
|
|
|
|
klass.class_eval { define_method(:bar) { calls << klass } }
|
|
|
|
klass.skip :qux, raise: false
|
2013-05-08 13:31:02 -04:00
|
|
|
klass.new.run
|
|
|
|
assert_equal 1, calls.length
|
|
|
|
end
|
2013-05-08 13:07:19 -04:00
|
|
|
end
|
2015-12-15 07:12:16 -05:00
|
|
|
|
2017-10-19 16:17:48 -04:00
|
|
|
class NotSupportedStringConditionalTest < ActiveSupport::TestCase
|
|
|
|
def test_string_conditional_options
|
2017-01-18 18:50:35 -05:00
|
|
|
klass = Class.new(Record)
|
|
|
|
|
2017-10-19 16:17:48 -04:00
|
|
|
assert_raises(ArgumentError) { klass.before_save :tweedle, if: ["true"] }
|
|
|
|
assert_raises(ArgumentError) { klass.before_save :tweedle, if: "true" }
|
|
|
|
assert_raises(ArgumentError) { klass.after_save :tweedle, unless: "false" }
|
|
|
|
assert_raises(ArgumentError) { klass.skip_callback :save, :before, :tweedle, if: "true" }
|
|
|
|
assert_raises(ArgumentError) { klass.skip_callback :save, :after, :tweedle, unless: "false" }
|
2017-01-18 18:50:35 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-01-08 05:45:58 -05:00
|
|
|
class NotPermittedStringCallbackTest < ActiveSupport::TestCase
|
|
|
|
def test_passing_string_callback_is_not_permitted
|
2015-12-15 07:12:16 -05:00
|
|
|
klass = Class.new(Record)
|
|
|
|
|
2017-01-08 05:45:58 -05:00
|
|
|
assert_raises(ArgumentError) do
|
|
|
|
klass.before_save "tweedle"
|
2015-12-15 07:12:16 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2008-04-18 00:30:01 -04:00
|
|
|
end
|