Permalink
Browse files

Makes new callbacks support keys with special characters

  • Loading branch information...
1 parent 3be3470 commit 34509777fd375e5bc529f21ca66cf63263c2cf64 Yehuda Katz + Carl Lerche committed Apr 28, 2009
Showing with 29 additions and 0 deletions.
  1. +1 −0 activesupport/lib/active_support/new_callbacks.rb
  2. +28 −0 activesupport/test/new_callbacks_test.rb
@@ -356,6 +356,7 @@ def _define_runner(symbol, str, options)
str = <<-RUBY_EVAL
def _run_#{symbol}_callbacks(key = nil)
if key
+ key = key.hash.to_s.gsub(/-/, '_')
name = "_run__\#{self.class.name.split("::").last}__#{symbol}__\#{key}__callbacks"
if respond_to?(name)
@@ -255,6 +255,26 @@ def save
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
def test_save_around
around = AroundPerson.new
@@ -381,4 +401,12 @@ def test_termination
assert_equal ["first", "second", "third", "second", "first"], terminator.history
end
end
+
+ class HyphenatedKeyTest < Test::Unit::TestCase
+ def test_save
+ obj = HyphenatedCallbacks.new
+ obj.save
+ assert_equal obj.stuff, "OMG"
+ end
+ end
end

0 comments on commit 3450977

Please sign in to comment.