Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 6063

Show
Ignore:
Timestamp:
01/28/07 08:29:05 (2 years ago)
Author:
bitsweat
Message:

Merge [6062] from trunk. References #7229.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1-2-stable/actionpack/CHANGELOG

    r6045 r6063  
    11*SVN* 
     2 
     3* Resource member routes require :id, eliminating the ambiguous overlap with collection routes.  #7229 [dkubb] 
    24 
    35* Fixed NumberHelper#number_with_delimiter to use "." always for splitting the original number, not the delimiter parameter #7389 [ceefour] 
  • branches/1-2-stable/actionpack/lib/action_controller/resources.rb

    r5878 r6063  
    235235      def map_collection_actions(map, resource) 
    236236        resource.collection_methods.each do |method, actions| 
    237           route_options = requirements_for(method) 
    238  
    239237          actions.each do |action| 
    240             map.named_route( 
    241               "#{resource.name_prefix}#{action}_#{resource.plural}",  
    242               "#{resource.path};#{action}",  
    243               route_options.merge(:action => action.to_s) 
    244             ) 
    245  
    246             map.named_route( 
    247               "formatted_#{resource.name_prefix}#{action}_#{resource.plural}", 
    248               "#{resource.path}.:format;#{action}", 
    249               route_options.merge(:action => action.to_s) 
    250             ) 
    251           end 
    252         end 
    253  
    254         map.named_route("#{resource.name_prefix}#{resource.plural}", resource.path, :action => "index", :conditions => { :method => :get }) 
    255         map.named_route("formatted_#{resource.name_prefix}#{resource.plural}", "#{resource.path}.:format", :action => "index", :conditions => { :method => :get }) 
    256  
    257         map.connect(resource.path, :action => "create", :conditions => { :method => :post }) 
    258         map.connect("#{resource.path}.:format", :action => "create", :conditions => { :method => :post }) 
     238            action_options = action_options_for(action, resource, method) 
     239            map.named_route("#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path};#{action}", action_options) 
     240            map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path}.:format;#{action}", action_options) 
     241          end 
     242        end 
     243 
     244        index_action_options = action_options_for("index", resource) 
     245        map.named_route("#{resource.name_prefix}#{resource.plural}", resource.path, index_action_options) 
     246        map.named_route("formatted_#{resource.name_prefix}#{resource.plural}", "#{resource.path}.:format", index_action_options) 
     247 
     248        create_action_options = action_options_for("create", resource) 
     249        map.connect(resource.path, create_action_options) 
     250        map.connect("#{resource.path}.:format", create_action_options) 
    259251      end 
    260252 
    261253      def map_new_actions(map, resource) 
    262254        resource.new_methods.each do |method, actions| 
    263           route_options = requirements_for(method) 
    264255          actions.each do |action| 
     256            action_options = action_options_for(action, resource, method) 
    265257            if action == :new 
    266               map.named_route("#{resource.name_prefix}new_#{resource.singular}", resource.new_path, route_options.merge(:action => "new")
    267               map.named_route("formatted_#{resource.name_prefix}new_#{resource.singular}", "#{resource.new_path}.:format", route_options.merge(:action => "new")
     258              map.named_route("#{resource.name_prefix}new_#{resource.singular}", resource.new_path, action_options
     259              map.named_route("formatted_#{resource.name_prefix}new_#{resource.singular}", "#{resource.new_path}.:format", action_options
    268260            else 
    269               map.named_route("#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path};#{action}", route_options.merge(:action => action.to_s)
    270               map.named_route("formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}.:format;#{action}", route_options.merge(:action => action.to_s)
     261              map.named_route("#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path};#{action}", action_options
     262              map.named_route("formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}.:format;#{action}", action_options
    271263            end 
    272264          end 
    273265        end 
    274266      end 
    275        
     267 
    276268      def map_member_actions(map, resource) 
    277269        resource.member_methods.each do |method, actions| 
    278           route_options = requirements_for(method) 
    279  
    280270          actions.each do |action| 
    281             map.named_route("#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path};#{action}", route_options.merge(:action => action.to_s)) 
    282             map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}.:format;#{action}", route_options.merge(:action => action.to_s)) 
    283           end 
    284         end 
    285  
    286         map.named_route("#{resource.name_prefix}#{resource.singular}", resource.member_path, :action => "show", :conditions => { :method => :get }) 
    287         map.named_route("formatted_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}.:format", :action => "show", :conditions => { :method => :get }) 
    288  
    289         map.connect(resource.member_path, :action => "update", :conditions => { :method => :put }) 
    290         map.connect("#{resource.member_path}.:format", :action => "update", :conditions => { :method => :put }) 
    291  
    292         map.connect(resource.member_path, :action => "destroy", :conditions => { :method => :delete }) 
    293         map.connect("#{resource.member_path}.:format", :action => "destroy", :conditions => { :method => :delete }) 
     271            action_options = action_options_for(action, resource, method) 
     272            map.named_route("#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path};#{action}", action_options) 
     273            map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}.:format;#{action}",action_options) 
     274          end 
     275        end 
     276 
     277        show_action_options = action_options_for("show", resource) 
     278        map.named_route("#{resource.name_prefix}#{resource.singular}", resource.member_path, show_action_options) 
     279        map.named_route("formatted_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}.:format", show_action_options) 
     280 
     281        update_action_options = action_options_for("update", resource) 
     282        map.connect(resource.member_path, update_action_options) 
     283        map.connect("#{resource.member_path}.:format", update_action_options) 
     284 
     285        destroy_action_options = action_options_for("destroy", resource) 
     286        map.connect(resource.member_path, destroy_action_options) 
     287        map.connect("#{resource.member_path}.:format", destroy_action_options) 
    294288      end 
    295289     
    296       def requirements_for(method) 
    297         method == :any ? {} : { :conditions => { :method => method } } 
     290      def conditions_for(method) 
     291        { :conditions => method == :any ? {} : { :method => method } } 
     292      end 
     293 
     294      def action_options_for(action, resource, method = nil) 
     295        default_options = { :action => action.to_s } 
     296        require_id = { :requirements => { :id => Regexp.new("[^#{Routing::SEPARATORS.join}]+") } } 
     297        case default_options[:action] 
     298          when "index", "new" : default_options.merge(conditions_for(method || :get)) 
     299          when "create"       : default_options.merge(conditions_for(method || :post)) 
     300          when "show", "edit" : default_options.merge(conditions_for(method || :get)).merge(require_id) 
     301          when "update"       : default_options.merge(conditions_for(method || :put)).merge(require_id) 
     302          when "destroy"      : default_options.merge(conditions_for(method || :delete)).merge(require_id) 
     303          else                  default_options.merge(conditions_for(method)) 
     304        end 
    298305      end 
    299306  end 
  • branches/1-2-stable/actionpack/test/controller/resources_test.rb

    r5975 r6063  
    2323    assert_resource_methods [:upload, :fix],          resource, :member,     :post 
    2424    assert_resource_methods [:new, :preview, :draft], resource, :new,        :get 
     25  end 
     26 
     27  def test_should_resource_controller_name_equal_resource_name_by_default 
     28    resource = ActionController::Resources::Resource.new(:messages, {}) 
     29    assert_equal 'messages', resource.controller 
     30  end 
     31 
     32  def test_should_resource_controller_name_equal_controller_option 
     33    resource = ActionController::Resources::Resource.new(:messages, :controller => 'posts') 
     34    assert_equal 'posts', resource.controller 
    2535  end 
    2636 
     
    114124  end 
    115125 
    116  
    117126  def test_with_new_action 
    118127    with_restful_routing :messages, :new => { :preview => :post } do 
     
    179188  end 
    180189 
     190  def test_should_not_allow_delete_or_put_on_collection_path 
     191    controller_name = :messages 
     192    with_restful_routing controller_name do 
     193      options = { :controller => controller_name.to_s } 
     194      collection_path = "/#{controller_name}" 
     195 
     196      assert_raises(ActionController::RoutingError) do 
     197        assert_recognizes(options.merge(:action => 'update'), :path => collection_path, :method => :put) 
     198      end 
     199 
     200      assert_raises(ActionController::RoutingError) do 
     201        assert_recognizes(options.merge(:action => 'destroy'), :path => collection_path, :method => :delete) 
     202      end 
     203    end 
     204  end 
     205 
    181206  protected 
    182207    def with_restful_routing(*args) 
     
    196221      (options[:options] ||= {})[:controller] = controller_name.to_s 
    197222 
    198       collection_path = "/#{options[:path_prefix]}#{controller_name}" 
    199       member_path = "#{collection_path}/1" 
    200       new_path = "#{collection_path}/new" 
     223      collection_path            = "/#{options[:path_prefix]}#{controller_name}" 
     224      member_path                = "#{collection_path}/1" 
     225      new_path                   = "#{collection_path}/new" 
     226      edit_member_path           = "#{member_path};edit" 
     227      formatted_edit_member_path = "#{member_path}.xml;edit" 
    201228 
    202229      with_options(options[:options]) do |controller| 
    203230        controller.assert_routing collection_path,            :action => 'index' 
    204         controller.assert_routing "#{collection_path}.xml" ,  :action => 'index', :format => 'xml' 
    205231        controller.assert_routing new_path,                   :action => 'new' 
    206232        controller.assert_routing member_path,                :action => 'show', :id => '1' 
    207         controller.assert_routing "#{member_path};edit",      :action => 'edit', :id => '1' 
     233        controller.assert_routing edit_member_path,           :action => 'edit', :id => '1' 
     234        controller.assert_routing "#{collection_path}.xml",   :action => 'index',            :format => 'xml' 
     235        controller.assert_routing "#{new_path}.xml",          :action => 'new',              :format => 'xml' 
    208236        controller.assert_routing "#{member_path}.xml",       :action => 'show', :id => '1', :format => 'xml' 
    209       end 
    210  
    211       assert_recognizes( 
    212         options[:options].merge(:action => 'create'), 
    213         :path => collection_path, :method => :post) 
    214  
    215       assert_recognizes( 
    216         options[:options].merge(:action => 'update', :id => '1'), 
    217         :path => member_path, :method => :put) 
    218  
    219       assert_recognizes( 
    220         options[:options].merge(:action => 'destroy', :id => '1'), 
    221         :path => member_path, :method => :delete) 
     237        controller.assert_routing formatted_edit_member_path, :action => 'edit', :id => '1', :format => 'xml' 
     238      end 
     239 
     240      assert_recognizes(options[:options].merge(:action => 'index'),               :path => collection_path,  :method => :get) 
     241      assert_recognizes(options[:options].merge(:action => 'new'),                 :path => new_path,         :method => :get) 
     242      assert_recognizes(options[:options].merge(:action => 'create'),              :path => collection_path,  :method => :post) 
     243      assert_recognizes(options[:options].merge(:action => 'show',    :id => '1'), :path => member_path,      :method => :get) 
     244      assert_recognizes(options[:options].merge(:action => 'edit',    :id => '1'), :path => edit_member_path, :method => :get) 
     245      assert_recognizes(options[:options].merge(:action => 'update',  :id => '1'), :path => member_path,      :method => :put) 
     246      assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1'), :path => member_path,      :method => :delete) 
     247 
     248      assert_recognizes(options[:options].merge(:action => 'index',               :format => 'xml'), :path => "#{collection_path}.xml",   :method => :get) 
     249      assert_recognizes(options[:options].merge(:action => 'new',                 :format => 'xml'), :path => "#{new_path}.xml",          :method => :get) 
     250      assert_recognizes(options[:options].merge(:action => 'create',              :format => 'xml'), :path => "#{collection_path}.xml",   :method => :post) 
     251      assert_recognizes(options[:options].merge(:action => 'show',    :id => '1', :format => 'xml'), :path => "#{member_path}.xml",       :method => :get) 
     252      assert_recognizes(options[:options].merge(:action => 'edit',    :id => '1', :format => 'xml'), :path => formatted_edit_member_path, :method => :get) 
     253      assert_recognizes(options[:options].merge(:action => 'update',  :id => '1', :format => 'xml'), :path => "#{member_path}.xml",       :method => :put) 
     254      assert_recognizes(options[:options].merge(:action => 'destroy', :id => '1', :format => 'xml'), :path => "#{member_path}.xml",       :method => :delete) 
    222255 
    223256      yield options[:options] if block_given? 
     
    241274      name_prefix = options[:name_prefix] 
    242275 
    243       assert_named_route "#{full_prefix}",        "#{name_prefix}#{controller_name}_path",           options[:options] 
    244       assert_named_route "#{full_prefix}.xml",    "formatted_#{name_prefix}#{controller_name}_path", options[:options].merge(:format => 'xml') 
    245       assert_named_route "#{full_prefix}/new",    "#{name_prefix}new_#{singular_name}_path",         options[:options] 
    246       assert_named_route "#{full_prefix}/1",      "#{name_prefix}#{singular_name}_path",             options[:options].merge(:id => '1') 
    247       assert_named_route "#{full_prefix}/1;edit", "#{name_prefix}edit_#{singular_name}_path",        options[:options].merge(:id => '1') 
    248       assert_named_route "#{full_prefix}/1.xml",  "formatted_#{name_prefix}#{singular_name}_path",   options[:options].merge(:format => 'xml', :id => '1') 
     276      assert_named_route "#{full_prefix}",            "#{name_prefix}#{controller_name}_path",              options[:options] 
     277      assert_named_route "#{full_prefix}/new",        "#{name_prefix}new_#{singular_name}_path",            options[:options] 
     278      assert_named_route "#{full_prefix}/1",          "#{name_prefix}#{singular_name}_path",                options[:options].merge(:id => '1') 
     279      assert_named_route "#{full_prefix}/1;edit",     "#{name_prefix}edit_#{singular_name}_path",           options[:options].merge(:id => '1') 
     280      assert_named_route "#{full_prefix}.xml",        "formatted_#{name_prefix}#{controller_name}_path",    options[:options].merge(            :format => 'xml') 
     281      assert_named_route "#{full_prefix}/new.xml",    "formatted_#{name_prefix}new_#{singular_name}_path",  options[:options].merge(            :format => 'xml') 
     282      assert_named_route "#{full_prefix}/1.xml",      "formatted_#{name_prefix}#{singular_name}_path",      options[:options].merge(:id => '1', :format => 'xml') 
     283      assert_named_route "#{full_prefix}/1.xml;edit", "formatted_#{name_prefix}edit_#{singular_name}_path", options[:options].merge(:id => '1', :format => 'xml') 
    249284      yield options[:options] if block_given? 
    250285    end