Permalink
Browse files

Added :has_many and :has_one for declaring plural and singular resour…

…ces beneath the current [DHH] Added :name_prefix as standard for nested resources [DHH]

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@6588 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent 234b0b7 commit 63aea3ffa9897e748ff03dd119605453d518ac3f @dhh dhh committed Apr 26, 2007
View
32 actionpack/CHANGELOG
@@ -1,5 +1,37 @@
*SVN*
+* Added :name_prefix as standard for nested resources [DHH]. WARNING: May be backwards incompatible with your app
+
+ Before:
+
+ map.resources :emails do |emails|
+ emails.resources :comments, :name_prefix => "email_"
+ emails.resources :attachments, :name_prefix => "email_"
+ end
+
+ After:
+
+ map.resources :emails do |emails|
+ emails.resources :comments
+ emails.resources :attachments
+ end
+
+ 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.
+
+* Added :has_many and :has_one for declaring plural and singular resources beneath the current [DHH]
+
+ Before:
+
+ map.resources :notes do |notes|
+ notes.resources :comments
+ notes.resources :attachments
+ notes.resource :author
+ end
+
+ After:
+
+ map.resources :notes, :has_many => [ :comments, :attachments ], :has_one => :author
+
* Added that render :xml will try to call to_xml if it can [DHH]. Makes these work:
render :xml => post
View
24 actionpack/lib/action_controller/resources.rb
@@ -234,7 +234,7 @@ def initialize(entity, options)
# # has named route "category_message"
def resources(*entities, &block)
options = entities.last.is_a?(Hash) ? entities.pop : { }
- entities.each { |entity| map_resource entity, options.dup, &block }
+ entities.each { |entity| map_resource(entity, options.dup, &block) }
end
# Creates named routes for implementing verb-oriented controllers for a singleton resource.
@@ -293,7 +293,7 @@ def resources(*entities, &block)
# edit_account_path, hash_for_edit_account_path
def resource(*entities, &block)
options = entities.last.is_a?(Hash) ? entities.pop : { }
- entities.each { |entity| map_singleton_resource entity, options.dup, &block }
+ entities.each { |entity| map_singleton_resource(entity, options.dup, &block) }
end
private
@@ -306,9 +306,11 @@ def map_resource(entities, options = {}, &block)
map_new_actions(map, resource)
map_member_actions(map, resource)
+ map_associations(resource, options)
+
if block_given?
- with_options(:path_prefix => resource.nesting_path_prefix, &block)
- end
+ with_options(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.name_prefix, &block)
+ end
end
end
@@ -321,12 +323,24 @@ def map_singleton_resource(entities, options = {}, &block)
map_new_actions(map, resource)
map_member_actions(map, resource)
+ map_associations(resource, options)
+
if block_given?
- with_options(:path_prefix => resource.nesting_path_prefix, &block)
+ with_options(:path_prefix => resource.nesting_path_prefix, :name_prefix => resource.name_prefix, &block)
end
end
end
+ def map_associations(resource, options)
+ Array(options[:has_many]).each do |association|
+ resources(association, :path_prefix => resource.nesting_path_prefix, :name_prefix => resource.name_prefix)
+ end
+
+ Array(options[:has_one]).each do |association|
+ resource(association, :path_prefix => resource.nesting_path_prefix, :name_prefix => resource.name_prefix)
+ end
+ end
+
def map_collection_actions(map, resource)
resource.collection_methods.each do |method, actions|
actions.each do |action|
View
25 actionpack/test/controller/resources_test.rb
@@ -9,6 +9,8 @@ def rescue_action(e) raise e end
class ThreadsController < ResourcesController; end
class MessagesController < ResourcesController; end
class CommentsController < ResourcesController; end
+class AuthorsController < ResourcesController; end
+class LogoController < ResourcesController; end
class AccountController < ResourcesController; end
class AdminController < ResourcesController; end
@@ -247,6 +249,29 @@ def test_should_create_nested_singleton_resource_routes
assert_singleton_restful_for :account, :path_prefix => 'admin/'
end
end
+
+ def test_resource_has_many_should_become_nested_resources
+ with_routing do |set|
+ set.draw do |map|
+ map.resources :messages, :has_many => [ :comments, :authors ]
+ end
+
+ assert_simply_restful_for :messages
+ assert_simply_restful_for :comments, :path_prefix => 'messages/1/', :options => { :message_id => '1' }
+ assert_simply_restful_for :authors, :path_prefix => 'messages/1/', :options => { :message_id => '1' }
+ end
+ end
+
+ def test_resource_has_one_should_become_nested_resources
+ with_routing do |set|
+ set.draw do |map|
+ map.resources :messages, :has_one => :logo
+ end
+
+ assert_simply_restful_for :messages
+ assert_singleton_restful_for :logo, :path_prefix => 'messages/1/', :options => { :message_id => '1' }
+ end
+ end
def test_singleton_resource_with_member_action
[:put, :post].each do |method|

0 comments on commit 63aea3f

Please sign in to comment.