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

Prepend class methods of prepended concerns #38462

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
5 changes: 3 additions & 2 deletions activesupport/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@
prepend Imposter
end

Concerning is also updated: `concerning :Imposter, prepend: true do`
Class methods are prepended to the base class, concerning is also
updated: `concerning :Imposter, prepend: true do`.

*Jason Karns*
*Jason Karns, Elia Schito*

* Deprecate using `Range#include?` method to check the inclusion of a value
in a date time range. It is recommended to use `Range#cover?` method
Expand Down
4 changes: 2 additions & 2 deletions activesupport/lib/active_support/concern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ module ActiveSupport
#
# Just like `include`, concerns also support `prepend` with a corresponding
# `prepended do` callback. `module ClassMethods` or `class_methods do` are
# still extended.
# prepended as well.
#
# `prepend` is also used for any dependencies.
module Concern
Expand Down Expand Up @@ -145,7 +145,7 @@ def prepend_features(base) #:nodoc:
return false if base < self
@_dependencies.each { |dep| base.prepend(dep) }
super
base.extend const_get(:ClassMethods) if const_defined?(:ClassMethods)
base.singleton_class.prepend const_get(:ClassMethods) if const_defined?(:ClassMethods)
base.class_eval(&@_prepended_block) if instance_variable_defined?(:@_prepended_block)
end
end
Expand Down
30 changes: 30 additions & 0 deletions activesupport/test/concern_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -194,4 +194,34 @@ def test_no_raise_on_same_included_or_prepended_call
end
end
end

def test_prepended_and_included_methods
included = Module.new.extend(ActiveSupport::Concern)
prepended = Module.new.extend(ActiveSupport::Concern)

@klass.class_eval { def initialize; @foo = []; end }
included.module_eval { def foo; @foo << :included; end }
@klass.class_eval { def foo; super; @foo << :class; end }
prepended.module_eval { def foo; super; @foo << :prepended; end }

@klass.include included
@klass.prepend prepended

assert_equal @klass.new.foo, [:included, :class, :prepended]
end

def test_prepended_and_included_class_methods
included = Module.new.extend(ActiveSupport::Concern)
prepended = Module.new.extend(ActiveSupport::Concern)

@klass.class_eval { @foo = [] }
included.class_methods { def foo; @foo << :included; end }
@klass.class_eval { def self.foo; super; @foo << :class; end }
prepended.class_methods { def foo; super; @foo << :prepended; end }

@klass.include included
@klass.prepend prepended

assert_equal @klass.foo, [:included, :class, :prepended]
end
end