Permalink
Browse files

Fix concerns not executing block in mapper

Also, add documentation for alternate usage.
  • Loading branch information...
ernie committed Aug 23, 2012
1 parent 4037e31 commit eb43d3d1d94c67b3bf9c0cf576cdae8380f27260
@@ -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
@@ -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

0 comments on commit eb43d3d

Please sign in to comment.