提交 3e67e45d 编写于 作者: J Jamie Macey

Allow a defining custom member field on resources

By default, resources routes are created with :resource/:id. A model
defining to_param can make prettier urls by using something more
readable than an integer ID, but since the route picks it up as :id you
wind up with awkward User.find_by_username(params[:id]) calls.

By overriding the key to be used in @request.params you can be more
obvious in your intent.
上级 8954ee65
...@@ -880,17 +880,18 @@ module Resources ...@@ -880,17 +880,18 @@ module Resources
# CANONICAL_ACTIONS holds all actions that does not need a prefix or # CANONICAL_ACTIONS holds all actions that does not need a prefix or
# a path appended since they fit properly in their scope level. # a path appended since they fit properly in their scope level.
VALID_ON_OPTIONS = [:new, :collection, :member] VALID_ON_OPTIONS = [:new, :collection, :member]
RESOURCE_OPTIONS = [:as, :controller, :path, :only, :except] RESOURCE_OPTIONS = [:as, :controller, :path, :only, :except, :param]
CANONICAL_ACTIONS = %w(index create new show update destroy) CANONICAL_ACTIONS = %w(index create new show update destroy)
class Resource #:nodoc: class Resource #:nodoc:
attr_reader :controller, :path, :options attr_reader :controller, :path, :options, :param
def initialize(entities, options = {}) def initialize(entities, options = {})
@name = entities.to_s @name = entities.to_s
@path = (options[:path] || @name).to_s @path = (options[:path] || @name).to_s
@controller = (options[:controller] || @name).to_s @controller = (options[:controller] || @name).to_s
@as = options[:as] @as = options[:as]
@param = options[:param] || :id
@options = options @options = options
end end
...@@ -935,7 +936,7 @@ def resource_scope ...@@ -935,7 +936,7 @@ def resource_scope
alias :collection_scope :path alias :collection_scope :path
def member_scope def member_scope
"#{path}/:id" "#{path}/:#{param}"
end end
def new_scope(new_path) def new_scope(new_path)
...@@ -943,7 +944,7 @@ def new_scope(new_path) ...@@ -943,7 +944,7 @@ def new_scope(new_path)
end end
def nested_scope def nested_scope
"#{path}/:#{singular}_id" "#{path}/:#{singular}_#{param}"
end end
end end
......
...@@ -475,6 +475,11 @@ def self.call(params, request) ...@@ -475,6 +475,11 @@ def self.call(params, request)
get :preview, :on => :member get :preview, :on => :member
end end
resources :profiles, :param => :username do
get :details, :on => :member
resources :messages
end
scope :as => "routes" do scope :as => "routes" do
get "/c/:id", :as => :collision, :to => "collision#show" get "/c/:id", :as => :collision, :to => "collision#show"
get "/collision", :to => "collision#show" get "/collision", :to => "collision#show"
...@@ -2183,6 +2188,19 @@ def test_root_in_deeply_nested_scope ...@@ -2183,6 +2188,19 @@ def test_root_in_deeply_nested_scope
assert_equal "/posts/1/admin", post_admin_root_path(:post_id => '1') assert_equal "/posts/1/admin", post_admin_root_path(:post_id => '1')
end end
def test_custom_param
get '/profiles/bob'
assert_equal 'profiles#show', @response.body
assert_equal 'bob', @request.params[:username]
get '/profiles/bob/details'
assert_equal 'bob', @request.params[:username]
get '/profiles/bob/messages/34'
assert_equal 'bob', @request.params[:profile_username]
assert_equal '34', @request.params[:id]
end
private private
def with_https def with_https
old_https = https? old_https = https?
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册