aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--actionpack/lib/action_controller/resources.rb104
-rw-r--r--actionpack/test/controller/resources_test.rb38
2 files changed, 71 insertions, 71 deletions
diff --git a/actionpack/lib/action_controller/resources.rb b/actionpack/lib/action_controller/resources.rb
index 767d4c400a..84094dd09c 100644
--- a/actionpack/lib/action_controller/resources.rb
+++ b/actionpack/lib/action_controller/resources.rb
@@ -1,15 +1,15 @@
module ActionController
# == Overview
#
- # ActionController::Resources are a way of defining RESTful resources. A RESTful resource, in basic terms,
+ # ActionController::Resources are a way of defining RESTful resources. A RESTful resource, in basic terms,
# is something that can be pointed at and it will respond with a representation of the data requested.
- # In real terms this could mean a user with a browser requests an HTML page, or that a desktop application
- # requests XML data.
+ # In real terms this could mean a user with a browser requests an HTML page, or that a desktop application
+ # requests XML data.
#
- # RESTful design is based on the assumption that there are four generic verbs that a user of an
+ # RESTful design is based on the assumption that there are four generic verbs that a user of an
# application can request from a resource (the noun).
#
- # Resources can be requested using four basic HTTP verbs (GET, POST, PUT, DELETE), the method used
+ # Resources can be requested using four basic HTTP verbs (GET, POST, PUT, DELETE), the method used
# denotes the type of action that should take place.
#
# === The Different Methods and their Usage
@@ -32,8 +32,8 @@ module ActionController
# # A DELETE request on a single Post resource is asking for it to be deleted
# DELETE /posts # with => { :id => 1 }
#
- # By using the REST convention, users of our application can assume certain things about how the data
- # is requested and how it is returned. Rails simplifies the routing part of RESTful design by
+ # By using the REST convention, users of our application can assume certain things about how the data
+ # is requested and how it is returned. Rails simplifies the routing part of RESTful design by
# supplying you with methods to create them in your routes.rb file.
#
# Read more about REST at http://en.wikipedia.org/wiki/Representational_State_Transfer
@@ -47,41 +47,41 @@ module ActionController
def initialize(entities, options)
@plural = entities
@singular = options[:singular] || plural.to_s.singularize
-
+
@options = options
-
+
arrange_actions
add_default_actions
set_prefixes
end
-
+
def controller
@controller ||= "#{options[:namespace]}#{(options[:controller] || plural).to_s}"
end
-
+
def requirements(with_id = false)
@requirements ||= @options[:requirements] || {}
@id_requirement ||= { :id => @requirements.delete(:id) || /[^#{Routing::SEPARATORS.join}]+/ }
-
+
with_id ? @requirements.merge(@id_requirement) : @requirements
end
-
+
def path
@path ||= "#{path_prefix}/#{plural}"
end
-
+
def new_path
@new_path ||= "#{path}/new"
end
-
+
def member_path
@member_path ||= "#{path}/:id"
end
-
+
def nesting_path_prefix
@nesting_path_prefix ||= "#{path}/:#{singular}_id"
end
-
+
def nesting_name_prefix
"#{name_prefix}#{singular}_"
end
@@ -93,7 +93,7 @@ module ActionController
@member_methods = arrange_actions_by_methods(options.delete(:member))
@new_methods = arrange_actions_by_methods(options.delete(:new))
end
-
+
def add_default_actions
add_default_action(member_methods, :get, :edit)
add_default_action(new_methods, :get, :new)
@@ -110,7 +110,7 @@ module ActionController
flipped_hash
end
end
-
+
def add_default_action(collection, method, action)
(collection[method] ||= []).unshift(action)
end
@@ -132,67 +132,67 @@ module ActionController
# Creates named routes for implementing verb-oriented controllers. This is
# useful for implementing REST API's, where a single resource has different
# behavior based on the HTTP verb (method) used to access it.
- #
+ #
# Example:
#
- # map.resources :messages
+ # map.resources :messages
#
# class MessagesController < ActionController::Base
# # GET messages_url
# def index
# # return all messages
# end
- #
+ #
# # GET new_message_url
# def new
# # return an HTML form for describing a new message
# end
- #
+ #
# # POST messages_url
# def create
# # create a new message
# end
- #
+ #
# # GET message_url(:id => 1)
# def show
# # find and return a specific message
# end
- #
+ #
# # GET edit_message_url(:id => 1)
# def edit
# # return an HTML form for editing a specific message
# end
- #
+ #
# # PUT message_url(:id => 1)
# def update
# # find and update a specific message
# end
- #
+ #
# # DELETE message_url(:id => 1)
# def destroy
# # delete a specific message
# end
# end
- #
+ #
# The #resources method sets HTTP method restrictions on the routes it generates. For example, making an
- # HTTP POST on <tt>new_message_url</tt> will raise a RoutingError exception. The default route in
+ # HTTP POST on <tt>new_message_url</tt> will raise a RoutingError exception. The default route in
# <tt>config/routes.rb</tt> overrides this and allows invalid HTTP methods for resource routes.
- #
+ #
# Along with the routes themselves, #resources generates named routes for use in
# controllers and views. <tt>map.resources :messages</tt> produces the following named routes and helpers:
- #
+ #
# Named Route Helpers
- # messages messages_url, hash_for_messages_url,
+ # messages messages_url, hash_for_messages_url,
# messages_path, hash_for_messages_path
- # message message_url(id), hash_for_message_url(id),
+ # message message_url(id), hash_for_message_url(id),
# message_path(id), hash_for_message_path(id)
- # new_message new_message_url, hash_for_new_message_url,
+ # new_message new_message_url, hash_for_new_message_url,
# new_message_path, hash_for_new_message_path
# edit_message edit_message_url(id), hash_for_edit_message_url(id),
# edit_message_path(id), hash_for_edit_message_path(id)
#
# You can use these helpers instead of #url_for or methods that take #url_for parameters:
- #
+ #
# redirect_to :controller => 'messages', :action => 'index'
# # becomes
# redirect_to messages_url
@@ -205,11 +205,11 @@ module ActionController
# form tags. The form helpers make this a little easier. For an update form with a <tt>@message</tt> object:
#
# <%= form_tag message_path(@message), :method => :put %>
- #
- # or
- #
+ #
+ # or
+ #
# <% form_for :message, @message, :url => message_path(@message), :html => {:method => :put} do |f| %>
- #
+ #
# The #resources method accepts various options, too, to customize the resulting
# routes:
# * <tt>:controller</tt> -- specify the controller name for the routes.
@@ -227,7 +227,7 @@ module ActionController
# end
#
# The comment resources work the same, but must now include a value for :article_id.
- #
+ #
# comments_url(@article)
# comment_url(@article, @comment)
#
@@ -249,27 +249,27 @@ module ActionController
# If <tt>map.resources</tt> is called with multiple resources, they all get the same options applied.
#
# Examples:
- #
+ #
# map.resources :messages, :path_prefix => "/thread/:thread_id"
# # --> GET /thread/7/messages/1
- #
+ #
# map.resources :messages, :collection => { :rss => :get }
# # --> GET /messages/rss (maps to the #rss action)
# # also adds a named route called "rss_messages"
- #
+ #
# map.resources :messages, :member => { :mark => :post }
# # --> POST /messages/1/mark (maps to the #mark action)
# # also adds a named route called "mark_message"
- #
+ #
# map.resources :messages, :new => { :preview => :post }
# # --> POST /messages/new/preview (maps to the #preview action)
# # also adds a named route called "preview_new_message"
- #
+ #
# map.resources :messages, :new => { :new => :any, :preview => :post }
# # --> POST /messages/new/preview (maps to the #preview action)
# # also adds a named route called "preview_new_message"
# # --> /messages/new can be invoked via any request method
- #
+ #
# map.resources :messages, :controller => "categories",
# :path_prefix => "/category/:category_id",
# :name_prefix => "category_"
@@ -280,10 +280,10 @@ module ActionController
entities.each { |entity| map_resource(entity, options.dup, &block) }
end
- # Creates named routes for implementing verb-oriented controllers for a singleton resource.
+ # Creates named routes for implementing verb-oriented controllers for a singleton resource.
# A singleton resource is global to the current user visiting the application, such as a user's
# /account profile.
- #
+ #
# See map.resources for general conventions. These are the main differences:
# - a singular name is given to map.resource. The default controller name is taken from the singular name.
# - To specify a custom plural name, use the :plural option. There is no :singular option
@@ -292,7 +292,7 @@ module ActionController
#
# Example:
#
- # map.resource :account
+ # map.resource :account
#
# class AccountController < ActionController::Base
# # POST account_url
@@ -328,9 +328,9 @@ module ActionController
#
# Along with the routes themselves, #resource generates named routes for use in
# controllers and views. <tt>map.resource :account</tt> produces the following named routes and helpers:
- #
+ #
# Named Route Helpers
- # account account_url, hash_for_account_url,
+ # account account_url, hash_for_account_url,
# account_path, hash_for_account_path
# edit_account edit_account_url, hash_for_edit_account_url,
# edit_account_path, hash_for_edit_account_path
@@ -353,7 +353,7 @@ module ActionController
if block_given?
with_options(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.nesting_name_prefix, &block)
- end
+ end
end
end
diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb
index 01836f6b94..60d94120af 100644
--- a/actionpack/test/controller/resources_test.rb
+++ b/actionpack/test/controller/resources_test.rb
@@ -71,22 +71,22 @@ class ResourcesTest < Test::Unit::TestCase
def test_irregular_id_with_no_requirements_should_raise_error
expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
-
+
with_restful_routing :messages do
assert_raises(ActionController::RoutingError) do
assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
end
end
end
-
+
def test_irregular_id_with_requirements_should_pass
expected_options = {:controller => 'messages', :action => 'show', :id => '1.1.1'}
-
+
with_restful_routing(:messages, :requirements => {:id => /[0-9]\.[0-9]\.[0-9]/}) do
assert_recognizes(expected_options, :path => 'messages/1.1.1', :method => :get)
end
end
-
+
def test_with_path_prefix_requirements
expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'}
with_restful_routing :messages, :path_prefix => '/thread/:thread_id', :requirements => {:thread_id => /[0-9]\.[0-9]\.[0-9]/} do
@@ -106,7 +106,7 @@ class ResourcesTest < Test::Unit::TestCase
assert_simply_restful_for :comments, :path_prefix => 'thread/5/', :options => { :thread_id => '5' }
end
end
-
+
def test_with_name_prefix
with_restful_routing :messages, :name_prefix => 'post_' do
assert_simply_restful_for :messages, :name_prefix => 'post_'
@@ -276,12 +276,12 @@ class ResourcesTest < Test::Unit::TestCase
admin.resource :account
end
end
-
+
assert_singleton_restful_for :admin
assert_singleton_restful_for :account, :name_prefix => "admin_", :path_prefix => 'admin/'
end
end
-
+
def test_resource_has_many_should_become_nested_resources
with_routing do |set|
set.draw do |map|
@@ -346,7 +346,7 @@ class ResourcesTest < Test::Unit::TestCase
account.resources :messages
end
end
-
+
assert_singleton_restful_for :account
assert_simply_restful_for :messages, :name_prefix => "account_", :path_prefix => 'account/'
end
@@ -364,7 +364,7 @@ class ResourcesTest < Test::Unit::TestCase
assert_simply_restful_for :messages, :name_prefix => "account_", :path_prefix => '7/account/', :options => { :site_id => '7' }
end
end
-
+
def test_should_nest_singleton_resource_in_resources
with_routing do |set|
set.draw do |map|
@@ -372,7 +372,7 @@ class ResourcesTest < Test::Unit::TestCase
thread.resource :admin
end
end
-
+
assert_simply_restful_for :threads
assert_singleton_restful_for :admin, :name_prefix => 'thread_', :path_prefix => 'threads/5/', :options => { :thread_id => '5' }
end
@@ -401,7 +401,7 @@ class ResourcesTest < Test::Unit::TestCase
backoffice.resources :products
end
end
-
+
assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
end
end
@@ -413,7 +413,7 @@ class ResourcesTest < Test::Unit::TestCase
backoffice.resources :products, :has_many => :tags
end
end
-
+
assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
assert_simply_restful_for :tags, :controller => "backoffice/tags", :name_prefix => "backoffice_product_", :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
end
@@ -426,12 +426,12 @@ class ResourcesTest < Test::Unit::TestCase
backoffice.resources :products, :has_one => :manufacturer
end
end
-
+
assert_simply_restful_for :products, :controller => "backoffice/products", :name_prefix => 'backoffice_', :path_prefix => 'backoffice/'
assert_singleton_restful_for :manufacturer, :controller => "backoffice/manufacturer", :name_prefix => 'backoffice_product_', :path_prefix => 'backoffice/products/1/', :options => { :product_id => '1' }
end
end
-
+
def test_resources_in_nested_namespace
with_routing do |set|
set.draw do |map|
@@ -441,21 +441,21 @@ class ResourcesTest < Test::Unit::TestCase
end
end
end
-
+
assert_simply_restful_for :products, :controller => "backoffice/admin/products", :name_prefix => 'backoffice_admin_', :path_prefix => 'backoffice/admin/'
end
end
-
+
def test_resources_using_namespace
with_routing do |set|
set.draw do |map|
map.resources :products, :namespace => "backoffice/"
end
-
+
assert_simply_restful_for :products, :controller => "backoffice/products"
end
end
-
+
protected
def with_restful_routing(*args)
with_routing do |set|
@@ -463,7 +463,7 @@ class ResourcesTest < Test::Unit::TestCase
yield
end
end
-
+
def with_singleton_resources(*args)
with_routing do |set|
set.draw { |map| map.resource(*args) }