diff options
Diffstat (limited to 'actionpack')
-rw-r--r-- | actionpack/CHANGELOG | 2 | ||||
-rw-r--r-- | actionpack/lib/action_controller/resources.rb | 55 | ||||
-rw-r--r-- | actionpack/test/controller/resources_test.rb | 5 |
3 files changed, 38 insertions, 24 deletions
diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index 199999bfe5..3069dff849 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -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] diff --git a/actionpack/lib/action_controller/resources.rb b/actionpack/lib/action_controller/resources.rb index a6ab9dcdba..94f944f842 100644 --- a/actionpack/lib/action_controller/resources.rb +++ b/actionpack/lib/action_controller/resources.rb @@ -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) diff --git a/actionpack/test/controller/resources_test.rb b/actionpack/test/controller/resources_test.rb index ea3f4288f2..be0d6d1814 100644 --- a/actionpack/test/controller/resources_test.rb +++ b/actionpack/test/controller/resources_test.rb @@ -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) |