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

Ticket #11181: custom_paths_for_default_resource_actions.diff

File custom_paths_for_default_resource_actions.diff, 3.0 kB (added by ivanvr, 2 years ago)
  • test/controller/resources_test.rb

    old new  
    7676    end 
    7777  end 
    7878 
     79  def test_override_paths_for_default_restful_actions 
     80    resource = ActionController::Resources::Resource.new(:messages, 
     81      :path_names => {:new => 'nuevo', :edit => 'editar'}) 
     82    assert_equal resource.new_path, "#{resource.path}/nuevo" 
     83  end 
     84 
    7985  def test_multiple_default_restful_routes 
    8086    with_restful_routing :messages, :comments do 
    8187      assert_simply_restful_for :messages 
  • lib/action_controller/base.rb

    old new  
    340340    @@resource_action_separator = "/" 
    341341    cattr_accessor :resource_action_separator 
    342342     
     343    # Allow to override path names for default resources' actions 
     344    @@resources_path_names = { :new => 'new', :edit => 'edit' } 
     345    cattr_accessor :resources_path_names 
     346 
    343347    # Sets the token parameter name for RequestForgery.  Calling #protect_from_forgery sets it to :authenticity_token by default 
    344348    cattr_accessor :request_forgery_protection_token 
    345349 
  • lib/action_controller/resources.rb

    old new  
    8080      end 
    8181 
    8282      def new_path 
    83         @new_path ||= "#{path}/new" 
     83        new_action = self.options[:path_names][:new] if self.options[:path_names] 
     84        new_action ||= Base.resources_path_names[:new] 
     85        @new_path ||= "#{path}/#{new_action}" 
    8486      end 
    8587 
    8688      def member_path 
     
    493495        resource.member_methods.each do |method, actions| 
    494496          actions.each do |action| 
    495497            action_options = action_options_for(action, resource, method) 
    496             map.named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}", action_options) 
    497             map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action}.:format",action_options) 
     498            action_path = action 
     499            if resource.options[:path_names] 
     500              action_path = resource.options[:path_names][action] 
     501              action_path ||= Base.resources_path_names[action] 
     502            end 
     503             
     504            map.named_route("#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action_path}", action_options) 
     505            map.named_route("formatted_#{action}_#{resource.name_prefix}#{resource.singular}", "#{resource.member_path}#{resource.action_separator}#{action_path}.:format",action_options) 
    498506          end 
    499507        end 
    500508