From f1368ae07590b12f6a220c84c193801e774daf34 Mon Sep 17 00:00:00 2001 From: Diego Carrion Date: Sun, 18 Apr 2010 20:25:37 -0300 Subject: [PATCH] shallow routes are back --- actionpack/lib/action_dispatch/routing/mapper.rb | 22 ++++++++++-- actionpack/test/dispatch/routing_test.rb | 37 ++++++++++++++++++++++ 2 files changed, 55 insertions(+), 4 deletions(-) diff --git a/actionpack/lib/action_dispatch/routing/mapper.rb b/actionpack/lib/action_dispatch/routing/mapper.rb index ef2826a..732afca 100644 --- a/actionpack/lib/action_dispatch/routing/mapper.rb +++ b/actionpack/lib/action_dispatch/routing/mapper.rb @@ -373,12 +373,21 @@ module ActionDispatch @scope_options ||= private_methods.grep(/^merge_(.+)_scope$/) { $1.to_sym } end + def merge_shallow_scope(parent, child) + parent or child + end + def merge_path_scope(parent, child) - Mapper.normalize_path("#{parent}/#{child}") + parent_path = (@scope[:shallow] and child.eql?(':id')) ? parent.split('/').last : parent + Mapper.normalize_path "#{parent_path}/#{child}" end def merge_name_prefix_scope(parent, child) - parent ? "#{parent}_#{child}" : child + if @scope[:shallow] + child + else + parent ? "#{parent}_#{child}" : child + end end def merge_module_scope(parent, child) @@ -513,6 +522,10 @@ module ActionDispatch options["#{singular}_id".to_sym] = id_constraint if id_constraint? options end + + def shallow? + options[:shallow] + end end class SingletonResource < Resource #:nodoc: @@ -580,10 +593,10 @@ module ActionDispatch resource = Resource.new(resources.pop, options) - scope(:path => resource.path, :controller => resource.controller) do + scope(:path => resource.path, :controller => resource.controller, :shallow => resource.shallow?) do with_scope_level(:resources, resource) do + @scope[:path] = "/#{@scope[:name_prefix].pluralize}/:#{@scope[:name_prefix]}_id/#{resource.path}" if @scope[:shallow] and @scope[:name_prefix] yield if block_given? - with_scope_level(:collection) do scope(resource.collection_options) do get :index if resource.actions.include?(:index) @@ -595,6 +608,7 @@ module ActionDispatch with_scope_level(:member) do scope(':id') do scope(resource.options) do + @scope[:name_prefix] = nil if @scope[:shallow] get :show if resource.actions.include?(:show) put :update if resource.actions.include?(:update) delete :destroy if resource.actions.include?(:destroy) diff --git a/actionpack/test/dispatch/routing_test.rb b/actionpack/test/dispatch/routing_test.rb index 5bca476..ddc370a 100644 --- a/actionpack/test/dispatch/routing_test.rb +++ b/actionpack/test/dispatch/routing_test.rb @@ -30,6 +30,31 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest resource :info end + resources :users, :shallow => true do + resources :photos do + resources :types do + member do + post :preview + end + collection do + delete :erase + end + end + end + end + + scope :shallow => true do + resources :teams do + resources :players + end + + resources :countries do + resources :cities do + resources :places + end + end + end + match 'account/logout' => redirect("/logout"), :as => :logout_redirect match 'account/login', :to => redirect("/login") @@ -708,6 +733,18 @@ class TestRoutingMapper < ActionDispatch::IntegrationTest end end + def test_shallow_routes + with_test_routes do + assert_equal '/photos/4', photo_path(4) + assert_equal '/types/10/edit', edit_type_path(10) + assert_equal '/types/5/preview', preview_type_path(5) + assert_equal '/photos/2/types', photo_types_path(2) + assert_equal '/cities/1/places', url_for(:controller => :places, :action => :index, :city_id => 1, :only_path => true) + assert_equal '/teams/new', url_for(:controller => :teams, :action => :new, :only_path => true) + assert_equal '/photos/11/types/erase', url_for(:controller => :types, :action => :erase, :photo_id => 11, :only_path => true) + end + end + def test_update_project_person with_test_routes do get '/projects/1/people/2/update' -- 1.6.5.2