diff options
author | Rick Olson <technoweenie@gmail.com> | 2007-07-25 02:47:21 +0000 |
---|---|---|
committer | Rick Olson <technoweenie@gmail.com> | 2007-07-25 02:47:21 +0000 |
commit | bdcbfa9944abae475236c1559403c533b82392ff (patch) | |
tree | 167791d184b0e14d850127d0a138bb5328b91da9 | |
parent | 94e5b10aff8adbda671d5b63a8ad46b1e1263b18 (diff) | |
download | rails-bdcbfa9944abae475236c1559403c533b82392ff.tar.gz rails-bdcbfa9944abae475236c1559403c533b82392ff.tar.bz2 rails-bdcbfa9944abae475236c1559403c533b82392ff.zip |
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
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/resources.rb | 27 | ||||
-rw-r--r-- | actionpack/test/controller/resources_test.rb | 6 |
3 files changed, 25 insertions, 10 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 8e18d8e4ed..9fbcc58ea7 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* Allow you to set custom :conditions on resource routes. [Rick] + * Fixed that file.content_type for uploaded files would include a trailing \r #9053 [bgreenlee] * url_for now accepts a series of symbols representing the namespace of the record [Josh Knowles] 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: # * <tt>:collection</tt> - add named routes for other actions that operate on the collection. # Takes a hash of <tt>#{action} => #{method}</tt>, where method is <tt>:get</tt>/<tt>:post</tt>/<tt>:put</tt>/<tt>:delete</tt> # or <tt>:any</tt> 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 # * <tt>:controller</tt> - specify the controller name for the routes. # * <tt>:singular</tt> - specify the singular name used in the member routes. # * <tt>:path_prefix</tt> - set a prefix to the routes with required route variables. + # * <tt>:requirements</tt> - set custom routing parameter requirements. + # * <tt>:conditions</tt> - 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 diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb index 732f3e21da..c3e0836205 100644 --- a/actionpack/test/controller/resources_test.rb +++ b/actionpack/test/controller/resources_test.rb @@ -69,6 +69,12 @@ class ResourcesTest < Test::Unit::TestCase end end + def test_with_custom_conditions + with_restful_routing :messages, :conditions => { :subdomain => 'app' } do + assert_equal 'app', ActionController::Routing::Routes.named_routes.routes[:messages].conditions[:subdomain] + end + end + def test_irregular_id_with_no_requirements_should_raise_error expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'} |