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

Changeset 8785

Show
Ignore:
Timestamp:
02/02/08 05:57:16 (2 years ago)
Author:
bitsweat
Message:

Introduce map.resources :cards, :as => 'tarjetas' to use a custom resource name in the URL: cards_path == '/tarjetas'. Closes #10578.

Files:

Legend:

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

    r8782 r8785  
    11*SVN* 
     2 
     3* Introduce map.resources :cards, :as => 'tarjetas' to use a custom resource name in the URL: cards_path == '/tarjetas'.  #10578 [blj] 
    24 
    35* TestSession supports indifferent access.  #7372 [tamc, Arsen7, mhackett, julik, jean.helou] 
  • trunk/actionpack/lib/action_controller/resources.rb

    r8028 r8785  
    4545    class Resource #:nodoc: 
    4646      attr_reader :collection_methods, :member_methods, :new_methods 
    47       attr_reader :path_prefix, :name_prefix 
     47      attr_reader :path_prefix, :name_prefix, :path_segment 
    4848      attr_reader :plural, :singular 
    4949      attr_reader :options 
     
    5252        @plural   ||= entities 
    5353        @singular ||= options[:singular] || plural.to_s.singularize 
     54        @path_segment = options.delete(:as) || @plural 
    5455 
    5556        @options = options 
     
    7677 
    7778      def path 
    78         @path ||= "#{path_prefix}/#{plural}" 
     79        @path ||= "#{path_prefix}/#{path_segment}" 
    7980      end 
    8081 
     
    237238    # * <tt>:requirements</tt> - set custom routing parameter requirements. 
    238239    # * <tt>:conditions</tt> - specify custom routing recognition conditions.  Resources sets the :method value for the method-specific routes. 
     240    # * <tt>:as</tt> - specify a different resource name to use in the URL path. For example: 
     241    #     # products_path == '/productos' 
     242    #     map.resources :products, :as => 'productos' do |product| 
     243    #       # product_reviews_path(product) == '/productos/1234/comentarios' 
     244    #       product.resources :product_reviews, :as => 'comentarios' 
     245    #     end 
     246    # 
    239247    # * <tt>:path_prefix</tt> - set a prefix to the routes with required route variables. 
    240248    # 
  • trunk/actionpack/test/controller/resources_test.rb

    r8564 r8785  
    610610  end 
    611611 
     612  def test_with_path_segment 
     613    with_restful_routing :messages, :as => 'reviews' do 
     614      assert_simply_restful_for :messages, :as => 'reviews' 
     615    end 
     616  end 
     617 
     618  def test_multiple_with_path_segment_and_controller 
     619    with_routing do |set| 
     620      set.draw do |map| 
     621        map.resources :products do |products| 
     622          products.resources :product_reviews, :as => 'reviews', :controller => 'messages' 
     623        end 
     624        map.resources :tutors do |tutors| 
     625          tutors.resources :tutor_reviews, :as => 'reviews', :controller => 'comments' 
     626        end 
     627      end 
     628 
     629      assert_simply_restful_for :product_reviews, :controller=>'messages', :as => 'reviews', :name_prefix => 'product_', :path_prefix => 'products/1/', :options => {:product_id => '1'} 
     630      assert_simply_restful_for :tutor_reviews,:controller=>'comments', :as => 'reviews', :name_prefix => 'tutor_', :path_prefix => 'tutors/1/', :options => {:tutor_id => '1'} 
     631    end 
     632  end 
     633 
     634  def test_with_path_segment_path_prefix_requirements 
     635    expected_options = {:controller => 'messages', :action => 'show', :thread_id => '1.1.1', :id => '1'} 
     636    with_restful_routing :messages, :as => 'comments',:path_prefix => '/thread/:thread_id', :requirements => { :thread_id => /[0-9]\.[0-9]\.[0-9]/ } do 
     637      assert_recognizes(expected_options, :path => 'thread/1.1.1/comments/1', :method => :get) 
     638    end 
     639  end 
     640 
    612641  protected 
    613642    def with_restful_routing(*args) 
     
    640669      options[:options][:controller] = options[:controller] || controller_name.to_s 
    641670 
    642       collection_path            = "/#{options[:path_prefix]}#{controller_name}" 
     671      collection_path            = "/#{options[:path_prefix]}#{options[:as] || controller_name}" 
    643672      member_path                = "#{collection_path}/1" 
    644673      new_path                   = "#{collection_path}/new" 
     
    693722      options[:options].delete :action 
    694723 
    695       full_prefix = "/#{options[:path_prefix]}#{controller_name}" 
     724      full_prefix = "/#{options[:path_prefix]}#{options[:as] || controller_name}" 
    696725      name_prefix = options[:name_prefix] 
    697        
     726 
    698727      assert_named_route "#{full_prefix}",            "#{name_prefix}#{controller_name}_path",              options[:options] 
    699728      assert_named_route "#{full_prefix}.xml",        "formatted_#{name_prefix}#{controller_name}_path",    options[:options].merge(            :format => 'xml') 
     
    713742      options[:options][:controller] = options[:controller] || singleton_name.to_s.pluralize 
    714743 
    715       full_path           = "/#{options[:path_prefix]}#{singleton_name}" 
     744      full_path           = "/#{options[:path_prefix]}#{options[:as] || singleton_name}" 
    716745      new_path            = "#{full_path}/new" 
    717746      edit_path           = "#{full_path}/edit" 
     
    752781      options[:options].delete :action 
    753782 
    754       full_path = "/#{options[:path_prefix]}#{singleton_name}" 
     783      full_path = "/#{options[:path_prefix]}#{options[:as] || singleton_name}" 
    755784      name_prefix = options[:name_prefix] 
    756785