Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Fix concerns not executing block in mapper

Also, add documentation for alternate usage.
  • Loading branch information...
commit eb43d3d1d94c67b3bf9c0cf576cdae8380f27260 1 parent 4037e31
@ernie ernie authored
View
21 actionpack/lib/action_dispatch/routing/mapper.rb
@@ -1606,15 +1606,32 @@ def name_for_action(as, action) #:nodoc:
# concerns :commentable
# end
module Concerns
- # Define a routing concern using a name.
+ # Define a routing concern using a name. If a second parameter is
+ # supplied, it should respond to call, which will receive the mapper
+ # as a parameter, allowing for customized behavior based on the current
+ # scope.
#
# concern :commentable do
# resources :comments
# end
#
+ # # - or -
+ #
+ # class Commentable
+ # def self.call(mapper)
+ # if mapper.current_scope[:controller] == 'videos'
+ # mapper.resources :video_comments, as: :comments
+ # else
+ # mapper.resources :comments
+ # end
+ # end
+ # end
+ #
+ # concern :commentable, Commentable
+ #
# Any routing helpers can be used inside a concern.
def concern(name, callable = nil, &block)
- @concerns[name] = callable || block
+ @concerns[name] = callable || lambda { |m| m.instance_eval(&block) }
end
# Use the named concerns
View
10 actionpack/test/dispatch/routing/concerns_test.rb
@@ -103,4 +103,14 @@ def test_with_an_invalid_concern_name
assert_equal "No concern named foo was found!", e.message
end
+
+ def test_concerns_executes_block_in_context_of_current_mapper
+ mapper = ActionDispatch::Routing::Mapper.new(ActionDispatch::Routing::RouteSet.new)
+ mapper.concern :test_concern do
+ resources :things
+ return self
+ end
+
+ assert_equal mapper, mapper.concerns(:test_concern)
+ end
end
Please sign in to comment.
Something went wrong with that request. Please try again.