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

Changeset 4638

Show
Ignore:
Timestamp:
07/31/06 20:00:18 (2 years ago)
Author:
rick
Message:

Fixed the new_#{resource}_url route and added named route tests for Simply Restful. [Rick Olson]

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r4637 r4638  
    11*SVN* 
     2 
     3* Fixed the new_#{resource}_url route and added named route tests for Simply Restful.  [Rick Olson] 
    24 
    35* Added map.resources from the Simply Restful plugin [DHH]. Examples (the API has changed to use plurals!): 
  • trunk/actionpack/lib/action_controller/resources.rb

    r4637 r4638  
    137137          actions.each do |action| 
    138138            path = action == :new ? resource.new_path : "#{resource.new_path};#{action}" 
    139             name = "new_#{resource.plural}" 
     139            name = "new_#{resource.singular}" 
    140140            name = "#{action}_#{name}" unless action == :new 
    141141 
  • trunk/actionpack/test/controller/resources_test.rb

    r4637 r4638  
    22 
    33class MessagesController < ActionController::Base 
     4  def index() render :nothing => true end 
    45  def rescue_action(e) raise e end 
    56end 
     
    1213  def test_default_restful_routes 
    1314    with_restful_routing :messages do 
    14       assert_restful_routes_for :messages do 
    15         routing_options = {:controller => '/messages'} 
    16       end 
     15      assert_simply_restful_for :messages 
    1716    end 
    1817  end 
     
    2019  def test_with_path_prefix 
    2120    with_restful_routing :messages, :path_prefix => '/thread/:thread_id' do 
    22       assert_restful_routes_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' } 
     21      assert_simply_restful_for :messages, :path_prefix => 'thread/5/', :options => { :thread_id => '5' } 
    2322    end 
    2423  end 
     
    2625  def test_with_collection_action 
    2726    with_restful_routing :messages, :collection => { :rss => :get } do 
     27      rss_options = {:action => 'rss'} 
     28      rss_path    = "/messages;rss" 
    2829      assert_restful_routes_for :messages do |options| 
    29         assert_routing "/messages;rss", options.merge(:action => 'rss') 
     30        assert_routing rss_path, options.merge(rss_options) 
     31      end 
     32       
     33      assert_restful_named_routes_for :messages do |options| 
     34        assert_named_route rss_path, :rss_messages_path, rss_options 
    3035      end 
    3136    end 
     
    3540    [:put, :post].each do |method| 
    3641      with_restful_routing :messages, :member => { :mark => method } do 
     42        mark_options = {:action => 'mark', :id => '1'} 
     43        mark_path    = "/messages/1;mark" 
    3744        assert_restful_routes_for :messages do |options| 
    38           assert_recognizes( 
    39             options.merge(:action => 'mark', :id => '1'),  
    40             {:path => "/messages/1;mark", :method => method}) 
     45          assert_recognizes(options.merge(mark_options), :path => mark_path, :method => method) 
     46        end 
     47         
     48        assert_restful_named_routes_for :messages do |options| 
     49          assert_named_route mark_path, :mark_message_path, mark_options 
    4150        end 
    4251      end 
     
    4655  def test_with_new_action 
    4756    with_restful_routing :messages, :new => { :preview => :post } do 
     57      preview_options = {:action => 'preview'} 
     58      preview_path    = "/messages/new;preview" 
    4859      assert_restful_routes_for :messages do |options| 
    49         assert_recognizes( 
    50           options.merge(:action => 'preview'),  
    51           {:path => "/messages/new;preview", :method => :post}) 
     60        assert_recognizes(options.merge(preview_options), :path => preview_path, :method => :post) 
     61      end 
     62       
     63      assert_restful_named_routes_for :messages do |options| 
     64        assert_named_route preview_path, :preview_new_message_path, preview_options 
    5265      end 
    5366    end 
     
    8093      end 
    8194    end 
    82      
     95 
     96    # runs assert_restful_routes_for and assert_restful_named_routes for on the controller_name and options, without passing a block. 
     97    def assert_simply_restful_for(controller_name, options = {}) 
     98      assert_restful_routes_for       controller_name, options 
     99      assert_restful_named_routes_for controller_name, options 
     100    end 
     101 
    83102    def assert_restful_routes_for(controller_name, options = {}) 
    84103      (options[:options] ||= {})[:controller] = controller_name.to_s 
     
    104123        options[:options].merge(:action => 'destroy', :id => '1'), 
    105124        {:path => "/#{options[:path_prefix]}#{controller_name}/1", :method => :delete}) 
    106        
     125 
    107126      yield options[:options] if block_given? 
    108127    end 
     128     
     129    # test named routes like foo_path and foos_path map to the correct options. 
     130    def assert_restful_named_routes_for(controller_name, singular_name = nil, options = {}) 
     131      if singular_name.is_a?(Hash) 
     132        options       = singular_name 
     133        singular_name = nil 
     134      end 
     135      singular_name ||= controller_name.to_s.singularize 
     136      (options[:options] ||= {})[:controller] = controller_name.to_s 
     137      @controller = "#{controller_name.to_s.camelize}Controller".constantize.new 
     138      @request    = ActionController::TestRequest.new 
     139      @response   = ActionController::TestResponse.new 
     140      get :index, options[:options] 
     141      options[:options].delete :action 
     142 
     143      full_prefix = "/#{options[:path_prefix]}#{controller_name}" 
     144 
     145      assert_named_route "#{full_prefix}",        "#{controller_name}_path",           options[:options] 
     146      assert_named_route "#{full_prefix}.xml",    "formatted_#{controller_name}_path", options[:options].merge(:format => 'xml') 
     147      assert_named_route "#{full_prefix}/new",    "new_#{singular_name}_path",         options[:options] 
     148      assert_named_route "#{full_prefix}/1",      "#{singular_name}_path",             options[:options].merge(:id => '1') 
     149      assert_named_route "#{full_prefix}/1;edit", "edit_#{singular_name}_path",        options[:options].merge(:id => '1') 
     150      assert_named_route "#{full_prefix}/1.xml",  "formatted_#{singular_name}_path",   options[:options].merge(:format => 'xml', :id => '1') 
     151      yield options[:options] if block_given? 
     152    end 
     153 
     154    def assert_named_route(expected, route, options) 
     155      actual =  @controller.send(route, options) rescue $!.class.name 
     156      assert_equal expected, actual, "Error on route: #{route}(#{options.inspect})" 
     157    end 
    109158end