1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

concerning learns how to prepend the concern

In rare instances, a module needs to be prepended into the ancestor
hierarchy, not simply included. In cases where this is necessary, and it
is desirable to use an inline concern, it would be beneficial to be able
to instruct `concerning` that the concern should be prepended.

This is now possible, by providing `prepend: true` kwarg to
`concerning`. (It is false by default.)
This commit is contained in:
Jason Karns 2019-09-11 21:42:59 -04:00 committed by Kasper Timm Hansen
parent 5bd29afeb4
commit 8543974afa
No known key found for this signature in database
GPG key ID: 191153215EDA53D8
2 changed files with 15 additions and 2 deletions

View file

@ -106,8 +106,9 @@ class Module
# * stop leaning on protected/private for crude "this is internal stuff" modularity.
module Concerning
# Define a new concern and mix it in.
def concerning(topic, &block)
include concern(topic, &block)
def concerning(topic, prepend: false, &block)
method = prepend ? :prepend : :include
__send__(method, concern(topic, &block))
end
# A low-cruft shortcut to define a concern.

View file

@ -8,6 +8,18 @@ class ModuleConcerningTest < ActiveSupport::TestCase
klass = Class.new { concerning(:Foo) { } }
assert_includes klass.ancestors, klass::Foo, klass.ancestors.inspect
end
def test_concerning_can_prepend_concern
klass = Class.new do
def hi; "self"; end
concerning(:Foo, prepend: true) do
def hi; "hello, #{super}"; end
end
end
assert_equal "hello, self", klass.new.hi
end
end
class ModuleConcernTest < ActiveSupport::TestCase