Skip to content

Commit b16c36e

Browse files
committed
Introduce Concern#class_methods and Kernel#concern
1 parent 96759cf commit b16c36e

7 files changed

Lines changed: 113 additions & 18 deletions

File tree

‎activesupport/CHANGELOG.md‎

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,31 @@
1+
* Introduce `Concern#class_methods` as a sleek alternative to clunky
2+
`module ClassMethods`. Add `Kernel#concern` to define at the toplevel
3+
without chunky `module Foo; extend ActiveSupport::Concern` boilerplate.
4+
5+
# app/models/concerns/authentication.rb
6+
concern :Authentication do
7+
included do
8+
after_create :generate_private_key
9+
end
10+
11+
class_methods do
12+
def authenticate(credentials)
13+
# ...
14+
end
15+
end
16+
17+
def generate_private_key
18+
# ...
19+
end
20+
end
21+
22+
# app/models/user.rb
23+
class User < ActiveRecord::Base
24+
include Authentication
25+
end
26+
27+
*Jeremy Kemper*
28+
129
* Added `Object#present_in` to simplify value whitelisting.
230

331
Before:

‎activesupport/lib/active_support/concern.rb‎

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ module ActiveSupport
2626
# scope :disabled, -> { where(disabled: true) }
2727
# end
2828
#
29-
# module ClassMethods
29+
# class_methods do
3030
# ...
3131
# end
3232
# end
@@ -130,5 +130,13 @@ def included(base = nil, &block)
130130
super
131131
end
132132
end
133+
134+
def class_methods(&class_methods_module_definition)
135+
mod = const_defined?(:ClassMethods) ?
136+
const_get(:ClassMethods) :
137+
const_set(:ClassMethods, Module.new)
138+
139+
mod.module_eval(&class_methods_module_definition)
140+
end
133141
end
134142
end
Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
require 'active_support/core_ext/kernel/reporting'
21
require 'active_support/core_ext/kernel/agnostics'
2+
require 'active_support/core_ext/kernel/concern'
33
require 'active_support/core_ext/kernel/debugger'
4+
require 'active_support/core_ext/kernel/reporting'
45
require 'active_support/core_ext/kernel/singleton_class'
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
require 'active_support/core_ext/module/concerning'
2+
3+
module Kernel
4+
# A shortcut to define a toplevel concern, not within a module.
5+
#
6+
# See ActiveSupport::CoreExt::Module::Concerning for more.
7+
def concern(topic, &module_definition)
8+
Object.concern topic, &module_definition
9+
end
10+
end

‎activesupport/test/concern_test.rb‎

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ class ConcernTest < ActiveSupport::TestCase
55
module Baz
66
extend ActiveSupport::Concern
77

8-
module ClassMethods
8+
class_methods do
99
def baz
1010
"baz"
1111
end
@@ -33,6 +33,12 @@ module Bar
3333

3434
include Baz
3535

36+
module ClassMethods
37+
def baz
38+
"bar's baz + " + super
39+
end
40+
end
41+
3642
def bar
3743
"bar"
3844
end
@@ -73,7 +79,7 @@ def test_modules_dependencies_are_met
7379
@klass.send(:include, Bar)
7480
assert_equal "bar", @klass.new.bar
7581
assert_equal "bar+baz", @klass.new.baz
76-
assert_equal "baz", @klass.baz
82+
assert_equal "bar's baz + baz", @klass.baz
7783
assert @klass.included_modules.include?(ConcernTest::Bar)
7884
end
7985

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
require 'abstract_unit'
2+
require 'active_support/core_ext/kernel/concern'
3+
4+
class KernelConcernTest < ActiveSupport::TestCase
5+
def test_may_be_defined_at_toplevel
6+
mod = ::TOPLEVEL_BINDING.eval 'concern(:ToplevelConcern) { }'
7+
assert_equal mod, ::ToplevelConcern
8+
assert_kind_of ActiveSupport::Concern, ::ToplevelConcern
9+
assert !Object.ancestors.include?(::ToplevelConcern), mod.ancestors.inspect
10+
Object.send :remove_const, :ToplevelConcern
11+
end
12+
end
Lines changed: 44 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,35 +1,65 @@
11
require 'abstract_unit'
22
require 'active_support/core_ext/module/concerning'
33

4-
class ConcerningTest < ActiveSupport::TestCase
5-
def test_concern_shortcut_creates_a_module_but_doesnt_include_it
6-
mod = Module.new { concern(:Foo) { } }
7-
assert_kind_of Module, mod::Foo
8-
assert mod::Foo.respond_to?(:included)
9-
assert !mod.ancestors.include?(mod::Foo), mod.ancestors.inspect
4+
class ModuleConcerningTest < ActiveSupport::TestCase
5+
def test_concerning_declares_a_concern_and_includes_it_immediately
6+
klass = Class.new { concerning(:Foo) { } }
7+
assert klass.ancestors.include?(klass::Foo), klass.ancestors.inspect
108
end
9+
end
1110

11+
class ModuleConcernTest < ActiveSupport::TestCase
1212
def test_concern_creates_a_module_extended_with_active_support_concern
1313
klass = Class.new do
14-
concern :Foo do
14+
concern :Baz do
1515
included { @foo = 1 }
1616
def should_be_public; end
1717
end
1818
end
1919

2020
# Declares a concern but doesn't include it
21-
assert_kind_of Module, klass::Foo
22-
assert !klass.ancestors.include?(klass::Foo), klass.ancestors.inspect
21+
assert klass.const_defined?(:Baz, false)
22+
assert !ModuleConcernTest.const_defined?(:Baz)
23+
assert_kind_of ActiveSupport::Concern, klass::Baz
24+
assert !klass.ancestors.include?(klass::Baz), klass.ancestors.inspect
2325

2426
# Public method visibility by default
25-
assert klass::Foo.public_instance_methods.map(&:to_s).include?('should_be_public')
27+
assert klass::Baz.public_instance_methods.map(&:to_s).include?('should_be_public')
2628

2729
# Calls included hook
28-
assert_equal 1, Class.new { include klass::Foo }.instance_variable_get('@foo')
30+
assert_equal 1, Class.new { include klass::Baz }.instance_variable_get('@foo')
2931
end
3032

31-
def test_concerning_declares_a_concern_and_includes_it_immediately
32-
klass = Class.new { concerning(:Foo) { } }
33-
assert klass.ancestors.include?(klass::Foo), klass.ancestors.inspect
33+
class Foo
34+
concerning :Bar do
35+
module ClassMethods
36+
def will_be_orphaned; end
37+
end
38+
39+
const_set :ClassMethods, Module.new {
40+
def hacked_on; end
41+
}
42+
43+
# Doesn't overwrite existing ClassMethods module.
44+
class_methods do
45+
def nicer_dsl; end
46+
end
47+
48+
# Doesn't overwrite previous class_methods definitions.
49+
class_methods do
50+
def doesnt_clobber; end
51+
end
52+
end
53+
end
54+
55+
def test_using_class_methods_blocks_instead_of_ClassMethods_module
56+
assert !Foo.respond_to?(:will_be_orphaned)
57+
assert Foo.respond_to?(:hacked_on)
58+
assert Foo.respond_to?(:nicer_dsl)
59+
assert Foo.respond_to?(:doesnt_clobber)
60+
61+
# Orphan in Foo::ClassMethods, not Bar::ClassMethods.
62+
assert Foo.const_defined?(:ClassMethods)
63+
assert Foo::ClassMethods.method_defined?(:will_be_orphaned)
3464
end
3565
end

0 commit comments

Comments
 (0)