Skip to content
This repository
Browse code

Merge pull request #5581 from jamie/custom_param_field

Allow a defining custom member field on resources
  • Loading branch information...
commit adb802e629f8d72afbb917575c9ed511a48a3d15 2 parents 8954ee6 + 3e67e45
José Valim josevalim authored
9 actionpack/lib/action_dispatch/routing/mapper.rb
@@ -880,17 +880,18 @@ module Resources
880 880 # CANONICAL_ACTIONS holds all actions that does not need a prefix or
881 881 # a path appended since they fit properly in their scope level.
882 882 VALID_ON_OPTIONS = [:new, :collection, :member]
883   - RESOURCE_OPTIONS = [:as, :controller, :path, :only, :except]
  883 + RESOURCE_OPTIONS = [:as, :controller, :path, :only, :except, :param]
884 884 CANONICAL_ACTIONS = %w(index create new show update destroy)
885 885
886 886 class Resource #:nodoc:
887   - attr_reader :controller, :path, :options
  887 + attr_reader :controller, :path, :options, :param
888 888
889 889 def initialize(entities, options = {})
890 890 @name = entities.to_s
891 891 @path = (options[:path] || @name).to_s
892 892 @controller = (options[:controller] || @name).to_s
893 893 @as = options[:as]
  894 + @param = options[:param] || :id
894 895 @options = options
895 896 end
896 897
@@ -935,7 +936,7 @@ def resource_scope
935 936 alias :collection_scope :path
936 937
937 938 def member_scope
938   - "#{path}/:id"
  939 + "#{path}/:#{param}"
939 940 end
940 941
941 942 def new_scope(new_path)
@@ -943,7 +944,7 @@ def new_scope(new_path)
943 944 end
944 945
945 946 def nested_scope
946   - "#{path}/:#{singular}_id"
  947 + "#{path}/:#{singular}_#{param}"
947 948 end
948 949
949 950 end
18 actionpack/test/dispatch/routing_test.rb
@@ -475,6 +475,11 @@ def self.call(params, request)
475 475 get :preview, :on => :member
476 476 end
477 477
  478 + resources :profiles, :param => :username do
  479 + get :details, :on => :member
  480 + resources :messages
  481 + end
  482 +
478 483 scope :as => "routes" do
479 484 get "/c/:id", :as => :collision, :to => "collision#show"
480 485 get "/collision", :to => "collision#show"
@@ -2183,6 +2188,19 @@ def test_root_in_deeply_nested_scope
2183 2188 assert_equal "/posts/1/admin", post_admin_root_path(:post_id => '1')
2184 2189 end
2185 2190
  2191 + def test_custom_param
  2192 + get '/profiles/bob'
  2193 + assert_equal 'profiles#show', @response.body
  2194 + assert_equal 'bob', @request.params[:username]
  2195 +
  2196 + get '/profiles/bob/details'
  2197 + assert_equal 'bob', @request.params[:username]
  2198 +
  2199 + get '/profiles/bob/messages/34'
  2200 + assert_equal 'bob', @request.params[:profile_username]
  2201 + assert_equal '34', @request.params[:id]
  2202 + end
  2203 +
2186 2204 private
2187 2205 def with_https
2188 2206 old_https = https?

0 comments on commit adb802e

Please sign in to comment.
Something went wrong with that request. Please try again.