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

Changeset 6588

Show
Ignore:
Timestamp:
04/26/07 23:11:31 (1 year ago)
Author:
david
Message:

Added :has_many and :has_one for declaring plural and singular resources beneath the current [DHH] Added :name_prefix as standard for nested resources [DHH]

Files:

Legend:

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

    r6574 r6588  
    11*SVN* 
     2 
     3* Added :name_prefix as standard for nested resources [DHH]. WARNING: May be backwards incompatible with your app 
     4 
     5    Before: 
     6 
     7      map.resources :emails do |emails| 
     8        emails.resources :comments,    :name_prefix => "email_" 
     9        emails.resources :attachments, :name_prefix => "email_" 
     10      end 
     11       
     12    After: 
     13 
     14      map.resources :emails do |emails| 
     15        emails.resources :comments 
     16        emails.resources :attachments 
     17      end 
     18     
     19    This does mean that if you intended to have comments_url go to /emails/5/comments, then you'll have to set :name_prefix to nil explicitly. 
     20 
     21* Added :has_many and :has_one for declaring plural and singular resources beneath the current [DHH] 
     22 
     23    Before: 
     24       
     25      map.resources :notes do |notes| 
     26        notes.resources :comments 
     27        notes.resources :attachments 
     28        notes.resource :author 
     29      end 
     30     
     31    After: 
     32     
     33      map.resources :notes, :has_many => [ :comments, :attachments ], :has_one => :author 
    234 
    335* Added that render :xml will try to call to_xml if it can [DHH]. Makes these work: 
  • trunk/actionpack/lib/action_controller/resources.rb

    r6485 r6588  
    235235    def resources(*entities, &block) 
    236236      options = entities.last.is_a?(Hash) ? entities.pop : { } 
    237       entities.each { |entity| map_resource entity, options.dup, &block
     237      entities.each { |entity| map_resource(entity, options.dup, &block)
    238238    end 
    239239 
     
    294294    def resource(*entities, &block) 
    295295      options = entities.last.is_a?(Hash) ? entities.pop : { } 
    296       entities.each { |entity| map_singleton_resource entity, options.dup, &block
     296      entities.each { |entity| map_singleton_resource(entity, options.dup, &block)
    297297    end 
    298298 
     
    307307          map_member_actions(map, resource) 
    308308 
     309          map_associations(resource, options) 
     310 
    309311          if block_given? 
    310             with_options(:path_prefix => resource.nesting_path_prefix, &block) 
    311           end 
     312            with_options(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.name_prefix, &block) 
     313          end           
    312314        end 
    313315      end 
     
    322324          map_member_actions(map, resource) 
    323325 
     326          map_associations(resource, options) 
     327 
    324328          if block_given? 
    325             with_options(:path_prefix => resource.nesting_path_prefix, &block) 
     329            with_options(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.name_prefix, &block) 
    326330          end 
     331        end 
     332      end 
     333 
     334      def map_associations(resource, options) 
     335        Array(options[:has_many]).each do |association| 
     336          resources(association, :path_prefix => resource.nesting_path_prefix, :name_prefix => resource.name_prefix) 
     337        end 
     338 
     339        Array(options[:has_one]).each do |association| 
     340          resource(association, :path_prefix => resource.nesting_path_prefix, :name_prefix => resource.name_prefix) 
    327341        end 
    328342      end 
  • trunk/actionpack/test/controller/resources_test.rb

    r6485 r6588  
    1010class MessagesController < ResourcesController; end 
    1111class CommentsController < ResourcesController; end 
     12class AuthorsController < ResourcesController; end 
     13class LogoController < ResourcesController; end 
    1214 
    1315class AccountController <  ResourcesController; end 
     
    248250    end 
    249251  end 
     252   
     253  def test_resource_has_many_should_become_nested_resources 
     254    with_routing do |set| 
     255      set.draw do |map| 
     256        map.resources :messages, :has_many => [ :comments, :authors ] 
     257      end 
     258 
     259      assert_simply_restful_for :messages 
     260      assert_simply_restful_for :comments, :path_prefix => 'messages/1/', :options => { :message_id => '1' } 
     261      assert_simply_restful_for :authors,  :path_prefix => 'messages/1/', :options => { :message_id => '1' } 
     262    end 
     263  end 
     264 
     265  def test_resource_has_one_should_become_nested_resources 
     266    with_routing do |set| 
     267      set.draw do |map| 
     268        map.resources :messages, :has_one => :logo 
     269      end 
     270 
     271      assert_simply_restful_for :messages 
     272      assert_singleton_restful_for :logo, :path_prefix => 'messages/1/', :options => { :message_id => '1' } 
     273    end 
     274  end 
    250275 
    251276  def test_singleton_resource_with_member_action