diff options
author | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-08-09 19:12:43 -0300 |
---|---|---|
committer | Rafael Mendonça França <rafaelmfranca@gmail.com> | 2012-08-13 21:41:53 -0300 |
commit | 0dd24728a088fcb4ae616bb5d62734aca5276b1b (patch) | |
tree | 9937a33c73190d47191705a8d5a8978062d790ff /actionpack/lib/action_dispatch/routing | |
parent | fa736e69a197522ae3af3d3e6394cdc1eb1da228 (diff) | |
download | rails-0dd24728a088fcb4ae616bb5d62734aca5276b1b.tar.gz rails-0dd24728a088fcb4ae616bb5d62734aca5276b1b.tar.bz2 rails-0dd24728a088fcb4ae616bb5d62734aca5276b1b.zip |
Implementing Routing Concerns
This pattern was introduced as a plugin by @dhh.
The original implementation can be found in
https://github.com/rails/routing_concerns
Diffstat (limited to 'actionpack/lib/action_dispatch/routing')
-rw-r--r-- | actionpack/lib/action_dispatch/routing/mapper.rb | 24 |
1 files changed, 23 insertions, 1 deletions
diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index 5e2f1ff1e0..5ff2420921 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -909,7 +909,7 @@ module ActionDispatch # CANONICAL_ACTIONS holds all actions that does not need a prefix or # a path appended since they fit properly in their scope level. VALID_ON_OPTIONS = [:new, :collection, :member] - RESOURCE_OPTIONS = [:as, :controller, :path, :only, :except, :param] + RESOURCE_OPTIONS = [:as, :controller, :path, :only, :except, :param, :concerns] CANONICAL_ACTIONS = %w(index create new show update destroy) class Resource #:nodoc: @@ -1046,6 +1046,8 @@ module ActionDispatch resource_scope(:resource, SingletonResource.new(resources.pop, options)) do yield if block_given? + concerns(options[:concerns]) if options[:concerns] + collection do post :create end if parent_resource.actions.include?(:create) @@ -1210,6 +1212,8 @@ module ActionDispatch resource_scope(:resources, Resource.new(resources.pop, options)) do yield if block_given? + concerns(options[:concerns]) if options[:concerns] + collection do get :index if parent_resource.actions.include?(:index) post :create if parent_resource.actions.include?(:create) @@ -1580,15 +1584,33 @@ module ActionDispatch end end + module Concerns + def concern(name, &block) + @concerns[name] = block + end + + def concerns(*names) + names.flatten.each do |name| + if concern = @concerns[name] + instance_eval(&concern) + else + raise ArgumentError, "No concern named #{name} was found!" + end + end + end + end + def initialize(set) #:nodoc: @set = set @scope = { :path_names => @set.resources_path_names } + @concerns = {} end include Base include HttpHelpers include Redirection include Scoping + include Concerns include Resources end end |