From bdcbfa9944abae475236c1559403c533b82392ff Mon Sep 17 00:00:00 2001 From: Rick Olson Date: Wed, 25 Jul 2007 02:47:21 +0000 Subject: Allow you to set custom :conditions on resource routes. [Rick] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@7234 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/lib/action_controller/resources.rb | 27 +++++++++++++++++---------- 1 file changed, 17 insertions(+), 10 deletions(-) (limited to 'actionpack/lib/action_controller') diff --git a/actionpack/lib/action_controller/resources.rb b/actionpack/lib/action_controller/resources.rb index 3cb9367fc2..8e7dedb86f 100644 --- a/actionpack/lib/action_controller/resources.rb +++ b/actionpack/lib/action_controller/resources.rb @@ -70,6 +70,10 @@ module ActionController with_id ? @requirements.merge(@id_requirement) : @requirements end + def conditions + @conditions = @options[:conditions] || {} + end + def path @path ||= "#{path_prefix}/#{plural}" end @@ -215,8 +219,7 @@ module ActionController # # <% form_for :message, @message, :url => message_path(@message), :html => {:method => :put} do |f| %> # - # The #resources method accepts the following options to customize the resulting - # routes: + # The #resources method accepts the following options to customize the resulting routes: # * :collection - add named routes for other actions that operate on the collection. # Takes a hash of #{action} => #{method}, where method is :get/:post/:put/:delete # or :any if the method does not matter. These routes map to a URL like /messages/rss, with a route of rss_messages_url. @@ -225,6 +228,8 @@ module ActionController # * :controller - specify the controller name for the routes. # * :singular - specify the singular name used in the member routes. # * :path_prefix - set a prefix to the routes with required route variables. + # * :requirements - set custom routing parameter requirements. + # * :conditions - specify custom routing recognition conditions. Resources sets the :method value for the method-specific routes. # Weblog comments usually belong to a post, so you might use resources like: # # map.resources :articles @@ -490,20 +495,22 @@ module ActionController map.connect("#{resource.member_path}.:format", destroy_action_options) end - def conditions_for(method) - { :conditions => method == :any ? {} : { :method => method } } + def add_conditions_for(conditions, method) + returning({:conditions => conditions.dup}) do |options| + options[:conditions][:method] = method unless method == :any + end end def action_options_for(action, resource, method = nil) default_options = { :action => action.to_s } require_id = !resource.kind_of?(SingletonResource) case default_options[:action] - when "index", "new" : default_options.merge(conditions_for(method || :get)).merge(resource.requirements) - when "create" : default_options.merge(conditions_for(method || :post)).merge(resource.requirements) - when "show", "edit" : default_options.merge(conditions_for(method || :get)).merge(resource.requirements(require_id)) - when "update" : default_options.merge(conditions_for(method || :put)).merge(resource.requirements(require_id)) - when "destroy" : default_options.merge(conditions_for(method || :delete)).merge(resource.requirements(require_id)) - else default_options.merge(conditions_for(method)).merge(resource.requirements) + when "index", "new" : default_options.merge(add_conditions_for(resource.conditions, method || :get)).merge(resource.requirements) + when "create" : default_options.merge(add_conditions_for(resource.conditions, method || :post)).merge(resource.requirements) + when "show", "edit" : default_options.merge(add_conditions_for(resource.conditions, method || :get)).merge(resource.requirements(require_id)) + when "update" : default_options.merge(add_conditions_for(resource.conditions, method || :put)).merge(resource.requirements(require_id)) + when "destroy" : default_options.merge(add_conditions_for(resource.conditions, method || :delete)).merge(resource.requirements(require_id)) + else default_options.merge(add_conditions_for(resource.conditions, method)).merge(resource.requirements) end end end -- cgit v1.2.3