Permalink
Browse files

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
  • Loading branch information...
1 parent a1ca37e commit 68d2926ab09d49086eb4101c53d8ae604bd4b334 @jeremy jeremy committed Dec 22, 2006
View
@@ -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]
@@ -68,27 +68,13 @@ def add_default_action(collection, method, action)
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 @@ def resources(*entities, &block)
# 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 @@ def map_resource(entities, options = {}, &block)
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 @@ def map_singleton_resource(entities, options = {}, &block)
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,14 +330,21 @@ def map_collection_actions(map, resource)
)
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 })
map.connect(resource.path, :action => "create", :conditions => { :method => :post })
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)
@@ -350,8 +350,11 @@ def assert_singleton_routes_for(singleton_name, options = {})
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 @@ def assert_singleton_named_routes_for(singleton_name, options = {})
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)

0 comments on commit 68d2926

Please sign in to comment.