From 63aea3ffa9897e748ff03dd119605453d518ac3f Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Thu, 26 Apr 2007 23:11:31 +0000 Subject: Added :has_many and :has_one for declaring plural and singular resources 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 --- actionpack/lib/action_controller/resources.rb | 24 +++++++++++++++++++----- 1 file changed, 19 insertions(+), 5 deletions(-) (limited to 'actionpack/lib') diff --git a/actionpack/lib/action_controller/resources.rb b/actionpack/lib/action_controller/resources.rb index 506440c5c4..498ab44746 100644 --- a/actionpack/lib/action_controller/resources.rb +++ b/actionpack/lib/action_controller/resources.rb @@ -234,7 +234,7 @@ module ActionController # # 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 @@ module ActionController # 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 @@ module ActionController 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 @@ module ActionController 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| -- cgit v1.2.3