Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source
Show
Ignore:
Timestamp:
10/01/07 06:44:07 (1 year ago)
Author:
nzkoz
Message:

Change the resource seperator from ; to / change the generated routes to use the new-style named routes. e.g. new_group_user_path(@group) instead of group_new_user_path(@group). [pixeltrix] Closes #8558

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • branches/1-2-stable/actionpack/lib/action_controller/resources.rb

    r7600 r7713  
    33    class Resource #:nodoc: 
    44      attr_reader :collection_methods, :member_methods, :new_methods 
    5       attr_reader :path_prefix, :name_prefix 
     5      attr_reader :path_prefix, :new_name_prefix 
    66      attr_reader :plural, :singular 
    77      attr_reader :options 
     
    1010        @plural   = entities 
    1111        @singular = options[:singular] || plural.to_s.singularize 
    12          
     12 
    1313        @options = options 
    1414 
     
    1717        set_prefixes 
    1818      end 
    19        
     19 
    2020      def controller 
    2121        @controller ||= (options[:controller] || plural).to_s 
    2222      end 
    23        
     23 
    2424      def path 
    2525        @path ||= "#{path_prefix}/#{plural}" 
    2626      end 
    27        
     27 
    2828      def new_path 
    2929        @new_path ||= "#{path}/new" 
    3030      end 
    31        
     31 
    3232      def member_path 
    3333        @member_path ||= "#{path}/:id" 
    3434      end 
    35        
     35 
    3636      def nesting_path_prefix 
    3737        @nesting_path_prefix ||= "#{path}/:#{singular}_id" 
    3838      end 
    39        
     39 
     40      def deprecate_name_prefix? 
     41        @name_prefix.blank? && !@new_name_prefix.blank? 
     42      end 
     43 
     44      def name_prefix 
     45        deprecate_name_prefix? ? @new_name_prefix : @name_prefix 
     46      end 
     47 
     48      def old_name_prefix 
     49        @name_prefix 
     50      end 
     51 
     52      def nesting_name_prefix 
     53        "#{new_name_prefix}#{singular}_" 
     54      end 
     55 
     56      def action_separator 
     57        @action_separator ||= Base.resource_action_separator 
     58      end 
     59 
    4060      protected 
    4161        def arrange_actions 
     
    4464          @new_methods        = arrange_actions_by_methods(options.delete(:new)) 
    4565        end 
    46          
     66 
    4767        def add_default_actions 
    4868          add_default_action(member_methods, :get, :edit) 
     
    5373          @path_prefix = options.delete(:path_prefix) 
    5474          @name_prefix = options.delete(:name_prefix) 
     75          @new_name_prefix = options.delete(:new_name_prefix) 
    5576        end 
    5677 
     
    6182          end 
    6283        end 
    63          
     84 
    6485        def add_default_action(collection, method, action) 
    6586          (collection[method] ||= []).unshift(action) 
     
    179200    #   The comment resources work the same, but must now include a value for :article_id. 
    180201    #    
    181     #     comments_url(@article) 
    182     #     comment_url(@article, @comment) 
    183     # 
    184     #     comments_url(:article_id => @article) 
    185     #     comment_url(:article_id => @article, :id => @comment) 
     202    #     article_comments_url(@article) 
     203    #     article_comment_url(@article, @comment) 
     204    # 
     205    #     article_comments_url(:article_id => @article) 
     206    #     article_comment_url(:article_id => @article, :id => @comment) 
    186207    # 
    187208    # * <tt>:name_prefix</tt> -- define a prefix for all generated routes, usually ending in an underscore. 
     
    193214    # * <tt>:collection</tt> -- add named routes for other actions that operate on the collection. 
    194215    #   Takes a hash of <tt>#{action} => #{method}</tt>, where method is <tt>:get</tt>/<tt>:post</tt>/<tt>:put</tt>/<tt>:delete</tt> 
    195     #   or <tt>:any</tt> if the method does not matter.  These routes map to a URL like /messages;rss, with a route of rss_messages_url. 
     216    #   or <tt>:any</tt> if the method does not matter.  These routes map to a URL like /messages/rss, with a route of rss_messages_url. 
    196217    # * <tt>:member</tt> -- same as :collection, but for actions that operate on a specific member. 
    197218    # * <tt>:new</tt> -- same as :collection, but for actions that operate on the new resource action. 
     
    205226    #   
    206227    #   map.resources :messages, :collection => { :rss => :get } 
    207     #   # --> GET /messages;rss (maps to the #rss action) 
     228    #   # --> GET /messages/rss (maps to the #rss action) 
    208229    #   #     also adds a named route called "rss_messages" 
    209230    #  
    210231    #   map.resources :messages, :member => { :mark => :post } 
    211     #   # --> POST /messages/1;mark (maps to the #mark action) 
     232    #   # --> POST /messages/1/mark (maps to the #mark action) 
    212233    #   #     also adds a named route called "mark_message" 
    213234    #  
    214235    #   map.resources :messages, :new => { :preview => :post } 
    215     #   # --> POST /messages/new;preview (maps to the #preview action) 
     236    #   # --> POST /messages/new/preview (maps to the #preview action) 
    216237    #   #     also adds a named route called "preview_new_message" 
    217238    #  
    218239    #   map.resources :messages, :new => { :new => :any, :preview => :post } 
    219     #   # --> POST /messages/new;preview (maps to the #preview action) 
     240    #   # --> POST /messages/new/preview (maps to the #preview action) 
    220241    #   #     also adds a named route called "preview_new_message" 
    221242    #   # --> /messages/new can be invoked via any request method 
     
    236257    #  
    237258    # See map.resources for general conventions.  These are the main differences: 
    238     #   - a singular name is given to map.resource.  The default controller name is taken from the singular name. 
    239     #   - To specify a custom plural name, use the :plural option.  There is no :singular option 
    240     #   - No default index, new, or create routes are created for the singleton resource controller. 
     259    #   - A singular name is given to map.resource.  The default controller name is taken from the singular name. 
     260    #   - There is no <tt>:collection</tt> option as there is only the singleton resource. 
     261    #   - There is no <tt>:singular</tt> option as the singular name is passed to map.resource. 
     262    #   - No default index route is created for the singleton resource controller. 
    241263    #   - When nesting singleton resources, only the singular name is used as the path prefix (example: 'account/messages/1') 
    242264    # 
     
    301323 
    302324          if block_given? 
    303             with_options(:path_prefix => resource.nesting_path_prefix, &block) 
     325            with_options(:path_prefix => resource.nesting_path_prefix, :new_name_prefix => resource.nesting_name_prefix, &block) 
    304326          end 
    305327        end 
     
    316338 
    317339          if block_given? 
    318             with_options(:path_prefix => resource.nesting_path_prefix, &block) 
     340            with_options(:path_prefix => resource.nesting_path_prefix, :new_name_prefix => resource.nesting_name_prefix, &block) 
    319341          end 
    320342        end 
     
    325347          actions.each do |action| 
    326348            action_options = action_options_for(action, resource, method) 
    327             map.named_route("#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path};#{action}", action_options) 
    328             map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.plural}", "#{resource.path}.:format;#{action}", action_options) 
     349 
     350            unless resource.old_name_prefix.blank? 
     351              map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.old_name_prefix}#{action}_#{resource.plural}") 
     352              map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "formatted_#{resource.old_name_prefix}#{action}_#{resource.plural}") 
     353            end 
     354 
     355            if resource.deprecate_name_prefix? 
     356              map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{action}_#{resource.plural}") 
     357              map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "formatted_#{action}_#{resource.plural}") 
     358            end 
     359 
     360            map.named_route("#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}#{resource.action_separator}#{action}", action_options) 
     361            map.connect("#{resource.path};#{action}", action_options) 
     362            map.connect("#{resource.path}.:format;#{action}", action_options) 
     363            map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.plural}", "#{resource.path}#{resource.action_separator}#{action}.:format", action_options) 
    329364          end 
    330365        end 
     
    335370        map.named_route("#{resource.name_prefix}#{resource.plural}", resource.path, index_action_options) 
    336371        map.named_route("formatted_#{resource.name_prefix}#{resource.plural}", "#{resource.path}.:format", index_action_options) 
     372 
     373        if resource.deprecate_name_prefix? 
     374          map.deprecated_named_route("#{resource.name_prefix}#{resource.plural}", "#{resource.plural}") 
     375          map.deprecated_named_route("formatted_#{resource.name_prefix}#{resource.plural}", "formatted_#{resource.plural}") 
     376        end 
    337377 
    338378        create_action_options = action_options_for("create", resource) 
     
    352392            action_options = action_options_for(action, resource, method) 
    353393            if action == :new 
    354               map.named_route("#{resource.name_prefix}new_#{resource.singular}", resource.new_path, action_options) 
    355               map.named_route("formatted_#{resource.name_prefix}new_#{resource.singular}", "#{resource.new_path}.:format", action_options) 
     394 
     395              unless resource.old_name_prefix.blank? 
     396                map.deprecated_named_route("new_#{resource.name_prefix}#{resource.singular}", "#{resource.old_name_prefix}new_#{resource.singular}") 
     397                map.deprecated_named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.old_name_prefix}new_#{resource.singular}") 
     398              end 
     399 
     400              if resource.deprecate_name_prefix? 
     401                map.deprecated_named_route("new_#{resource.name_prefix}#{resource.singular}", "new_#{resource.singular}") 
     402                map.deprecated_named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "formatted_new_#{resource.singular}") 
     403              end 
     404 
     405              map.named_route("new_#{resource.name_prefix}#{resource.singular}", resource.new_path, action_options) 
     406              map.named_route("formatted_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}.:format", action_options) 
     407 
    356408            else 
    357               map.named_route("#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path};#{action}", action_options) 
    358               map.named_route("formatted_#{resource.name_prefix}#{action}_new_#{resource.singular}", "#{resource.new_path}.:format;#{action}", action_options) 
     409 
     410              unless resource.old_name_prefix.blank? 
     411                map.deprecated_named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.old_name_prefix}#{action}_new_#{resource.singular}") 
     412                map.deprecated_named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.old_name_prefix}#{action}_new_#{resource.singular}") 
     413              end 
     414 
     415              if resource.deprecate_name_prefix? 
     416                map.deprecated_named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{action}_new_#{resource.singular}") 
     417                map.deprecated_named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "formatted_#{action}_new_#{resource.singular}") 
     418              end 
     419 
     420              map.named_route("#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}#{resource.action_separator}#{action}", action_options) 
     421              map.connect("#{resource.new_path};#{action}", action_options) 
     422              map.connect("#{resource.new_path}.:format;#{action}", action_options) 
     423              map.named_route("formatted_#{action}_new_#{resource.name_prefix}#{resource.singular}", "#{resource.new_path}#{resource.action_separator}#{action}.:format", action_options) 
     424 
    359425            end 
    360426          end 
     
    366432          actions.each do |action| 
    367433            action_options = action_options_for(action, resource, method) 
    368             map.named_route("#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path};#{action}", action_options) 
    369             map.named_route("formatted_#{resource.name_prefix}#{action}_#{resource.singular}", "#{resource.member_path}.:format;#{action}",action_options) 
     434 
     435            unless resource.old_name_prefix.blank? 
     436              map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.old_name_prefix}#{action}_#{resource.singular}") 
     437              map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.old_name_prefix}#{action}_#{resource.singular}") 
     438            end 
     439 
     440            if resource.deprecate_name_prefix? 
     441              map.deprecated_named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{action}_#{resource.singular}") 
     442              map.deprecated_named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "formatted_#{action}_#{resource.singular}") 
     443            end 
     444 
     445            map.named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}", action_options) 
     446            map.connect("#{resource.member_path};#{action}", action_options) 
     447            map.connect("#{resource.member_path}.:format;#{action}", action_options) 
     448            map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}.:format", action_options) 
     449 
    370450          end 
    371451        end 
     
    374454        map.named_route("#{resource.name_prefix}#{resource.singular}", resource.member_path, show_action_options) 
    375455        map.named_route("formatted_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}.:format", show_action_options) 
     456 
     457        if resource.deprecate_name_prefix? 
     458          map.deprecated_named_route("#{resource.name_prefix}#{resource.singular}", "#{resource.singular}") 
     459          map.deprecated_named_route("formatted_#{resource.name_prefix}#{resource.singular}", "formatted_#{resource.singular}") 
     460        end 
    376461 
    377462        update_action_options = action_options_for("update", resource)