Skip to content

Commit

Permalink
Fix GH rails#4344. A defined callback in extended module is called too.
Browse files Browse the repository at this point in the history
  • Loading branch information
kennyj committed Jan 24, 2012
1 parent dea2523 commit 5e59d75
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/callbacks.rb
Expand Up @@ -359,7 +359,7 @@ module ClassMethods
def __run_callbacks(key, kind, object, &blk) #:nodoc: def __run_callbacks(key, kind, object, &blk) #:nodoc:
name = __callback_runner_name(kind) name = __callback_runner_name(kind)
unless object.respond_to?(name) unless object.respond_to?(name)
str = send("_#{kind}_callbacks").compile(key, object) str = object.send("_#{kind}_callbacks").compile(key, object)
class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1 class_eval <<-RUBY_EVAL, __FILE__, __LINE__ + 1
def #{name}() #{str} end def #{name}() #{str} end
protected :#{name} protected :#{name}
Expand Down
56 changes: 56 additions & 0 deletions activesupport/test/callbacks_test.rb
Expand Up @@ -344,6 +344,54 @@ def save
end end
end end


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

class AroundCallbacksTest < ActiveSupport::TestCase class AroundCallbacksTest < ActiveSupport::TestCase
def test_save_around def test_save_around
around = AroundPerson.new around = AroundPerson.new
Expand Down Expand Up @@ -645,4 +693,12 @@ def test_skip_writer
end end
end end


class ExtendCallbacksTest < ActiveSupport::TestCase
def test_save
model = ExtendCallbacks.new.extend ExtendModule
model.save
assert_equal [1, 2, 3], model.recorder
end
end

end end

0 comments on commit 5e59d75

Please sign in to comment.