Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[ActiveSupport] Fix for #20489 - ActiveSupport::Concern#class_methods affects parent classes #20494

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion activesupport/lib/active_support/concern.rb
Expand Up @@ -132,7 +132,7 @@ def included(base = nil, &block)
end

def class_methods(&class_methods_module_definition)
mod = const_defined?(:ClassMethods) ?
mod = const_defined?(:ClassMethods, false) ?
const_get(:ClassMethods) :
const_set(:ClassMethods, Module.new)

Expand Down
25 changes: 25 additions & 0 deletions activesupport/test/concern_test.rb
Expand Up @@ -54,6 +54,11 @@ module Foo
include Bar, Baz
end

module Qux
module ClassMethods
end
end

def setup
@klass = Class.new
end
Expand All @@ -70,6 +75,26 @@ def test_class_methods_are_extended
assert_equal ConcernTest::Baz::ClassMethods, (class << @klass; self.included_modules; end)[0]
end

def test_class_methods_are_extended_only_on_expected_objects
::Object.__send__(:include, Qux)
Object.extend(Qux::ClassMethods)
# module needs to be created after Qux is included in Object or bug won't
# be triggered
test_module = Module.new do
extend ActiveSupport::Concern

class_methods do
def test
end
end
end
@klass.include test_module
assert_equal false, Object.respond_to?(:test)
Qux.class_eval do
remove_const :ClassMethods
end
end

def test_included_block_is_ran
@klass.include(Baz)
assert_equal true, @klass.included_ran
Expand Down