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
This commit is contained in:
Rick Olson
2007-07-25 02:47:21 +00:00
parent 94e5b10aff
commit bdcbfa9944
3 changed files with 25 additions and 10 deletions

View File

@@ -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]

View File

@@ -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

View File

@@ -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'}