Makes new callbacks support keys with special characters
This commit is contained in:
parent
3be3470fab
commit
34509777fd
|
@ -356,6 +356,7 @@ module ActiveSupport
|
||||||
str = <<-RUBY_EVAL
|
str = <<-RUBY_EVAL
|
||||||
def _run_#{symbol}_callbacks(key = nil)
|
def _run_#{symbol}_callbacks(key = nil)
|
||||||
if key
|
if key
|
||||||
|
key = key.hash.to_s.gsub(/-/, '_')
|
||||||
name = "_run__\#{self.class.name.split("::").last}__#{symbol}__\#{key}__callbacks"
|
name = "_run__\#{self.class.name.split("::").last}__#{symbol}__\#{key}__callbacks"
|
||||||
|
|
||||||
if respond_to?(name)
|
if respond_to?(name)
|
||||||
|
|
|
@ -255,6 +255,26 @@ module NewCallbacksTest
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class HyphenatedCallbacks
|
||||||
|
include ActiveSupport::NewCallbacks
|
||||||
|
define_callbacks :save
|
||||||
|
attr_reader :stuff
|
||||||
|
|
||||||
|
save_callback :before, :omg, :per_key => {:if => :yes}
|
||||||
|
|
||||||
|
def yes() true end
|
||||||
|
|
||||||
|
def omg
|
||||||
|
@stuff = "OMG"
|
||||||
|
end
|
||||||
|
|
||||||
|
def save
|
||||||
|
_run_save_callbacks("hyphen-ated") do
|
||||||
|
@stuff
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
class AroundCallbacksTest < Test::Unit::TestCase
|
class AroundCallbacksTest < Test::Unit::TestCase
|
||||||
def test_save_around
|
def test_save_around
|
||||||
around = AroundPerson.new
|
around = AroundPerson.new
|
||||||
|
@ -381,4 +401,12 @@ module NewCallbacksTest
|
||||||
assert_equal ["first", "second", "third", "second", "first"], terminator.history
|
assert_equal ["first", "second", "third", "second", "first"], terminator.history
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
class HyphenatedKeyTest < Test::Unit::TestCase
|
||||||
|
def test_save
|
||||||
|
obj = HyphenatedCallbacks.new
|
||||||
|
obj.save
|
||||||
|
assert_equal obj.stuff, "OMG"
|
||||||
|
end
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
Loading…
Reference in New Issue