Singleton resources: POST /singleton => create, GET /singleton/new => new

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5772 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
This commit is contained in:
Jeremy Kemper
2006-12-22 03:16:50 +00:00
parent a1ca37ec86
commit 68d2926ab0
3 changed files with 38 additions and 24 deletions

View File

@@ -1,5 +1,7 @@
*SVN*
* Singleton resources: POST /singleton => create, GET /singleton/new => new. [Jeremy Kemper]
* Use 400 Bad Request status for unrescued ActiveRecord::RecordInvalid exceptions. [Jeremy Kemper]
* Silence log_error deprecation warnings from inspecting deprecated instance variables. [Nate Wiger]

View File

@@ -68,27 +68,13 @@ module ActionController
class SingletonResource < Resource #:nodoc:
def initialize(entity, options)
@singular = entity
@plural = options[:plural] || singular.to_s.pluralize
@options = options
@plural = @singular = entity
@options = options
arrange_actions
add_default_actions
set_prefixes
end
def controller
@controller ||= (options[:controller] || singular).to_s
end
def path
@path ||= "#{path_prefix}/#{singular}"
end
def new_path
nil
end
alias_method :member_path, :path
alias_method :nesting_path_prefix, :path
end
@@ -251,27 +237,37 @@ module ActionController
# map.resource :account
#
# class AccountController < ActionController::Base
# # POST account_url
# def create
# # create an account
# end
#
# # GET new_account_url
# def new
# # return an HTML form for describing the new account
# end
#
# # GET account_url
# def show
# # find and return a specific message
# # find and return the account
# end
#
#
# # GET edit_account_url
# def edit
# # return an HTML form for editing a specific message
# # return an HTML form for editing the account
# end
#
#
# # PUT account_url
# def update
# # find and update a specific message
# # find and update the account
# end
#
#
# # DELETE account_url
# def destroy
# # delete a specific message
# # delete the account
# end
# end
#
#
# 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:
#
@@ -291,6 +287,7 @@ module ActionController
with_options :controller => resource.controller do |map|
map_collection_actions(map, resource)
map_default_collection_actions(map, resource)
map_new_actions(map, resource)
map_member_actions(map, resource)
@@ -304,6 +301,9 @@ module ActionController
resource = SingletonResource.new(entities, options)
with_options :controller => resource.controller do |map|
map_collection_actions(map, resource)
map_default_singleton_actions(map, resource)
map_new_actions(map, resource)
map_member_actions(map, resource)
if block_given?
@@ -330,7 +330,9 @@ module ActionController
)
end
end
end
def map_default_collection_actions(map, resource)
map.named_route("#{resource.name_prefix}#{resource.plural}", resource.path, :action => "index", :conditions => { :method => :get })
map.named_route("formatted_#{resource.name_prefix}#{resource.plural}", "#{resource.path}.:format", :action => "index", :conditions => { :method => :get })
@@ -338,6 +340,11 @@ module ActionController
map.connect("#{resource.path}.:format", :action => "create", :conditions => { :method => :post })
end
def map_default_singleton_actions(map, resource)
map.connect(resource.path, :action => "create", :conditions => { :method => :post })
map.connect("#{resource.path}.:format", :action => "create", :conditions => { :method => :post })
end
def map_new_actions(map, resource)
resource.new_methods.each do |method, actions|
route_options = requirements_for(method)

View File

@@ -350,8 +350,11 @@ class ResourcesTest < Test::Unit::TestCase
with_options options[:options] do |controller|
controller.assert_routing full_path, :action => 'show'
controller.assert_routing "#{full_path}.xml", :action => 'show', :format => 'xml'
controller.assert_routing "#{full_path}/new", :action => 'new'
controller.assert_routing "#{full_path};edit", :action => 'edit'
end
assert_recognizes(options[:options].merge(:action => 'create'), :path => full_path, :method => :post)
assert_recognizes(options[:options].merge(:action => 'update'), :path => full_path, :method => :put)
assert_recognizes(options[:options].merge(:action => 'destroy'), :path => full_path, :method => :delete)
@@ -370,6 +373,8 @@ class ResourcesTest < Test::Unit::TestCase
assert_named_route "#{full_path}", "#{singleton_name}_path", options[:options]
assert_named_route "#{full_path}.xml", "formatted_#{singleton_name}_path", options[:options].merge(:format => 'xml')
assert_named_route "#{full_path}/new", "new_#{singleton_name}_path", options[:options]
assert_named_route "#{full_path};edit", "edit_#{singleton_name}_path", options[:options]
end
def assert_named_route(expected, route, options)