diff options
author | Ernie Miller <ernie@erniemiller.org> | 2012-08-23 09:02:37 -0400 |
---|---|---|
committer | Ernie Miller <ernie@erniemiller.org> | 2012-09-03 13:13:16 -0400 |
commit | eb43d3d1d94c67b3bf9c0cf576cdae8380f27260 (patch) | |
tree | 7e275b962d01cda13ce7c34c91efe9a7143173b7 | |
parent | 4037e31d8874250e485ca6a27bd792a3beb13f76 (diff) | |
download | rails-eb43d3d1d94c67b3bf9c0cf576cdae8380f27260.tar.gz rails-eb43d3d1d94c67b3bf9c0cf576cdae8380f27260.tar.bz2 rails-eb43d3d1d94c67b3bf9c0cf576cdae8380f27260.zip |
Fix concerns not executing block in mapper
Also, add documentation for alternate usage.
-rw-r--r-- | actionpack/lib/action_dispatch/routing/mapper.rb | 21 | ||||
-rw-r--r-- | actionpack/test/dispatch/routing/concerns_test.rb | 10 |
2 files changed, 29 insertions, 2 deletions
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index b1abbbe505..8573f4d80b 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -1606,15 +1606,32 @@ module ActionDispatch # 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 diff --git a/actionpack/test/dispatch/routing/concerns_test.rb b/actionpack/test/dispatch/routing/concerns_test.rb index 8600da32ad..0289f38ba7 100644 --- a/actionpack/test/dispatch/routing/concerns_test.rb +++ b/actionpack/test/dispatch/routing/concerns_test.rb @@ -103,4 +103,14 @@ class RoutingConcernsTest < ActionDispatch::IntegrationTest 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 |