Skip to content
Browse files

Fixed named prefix scope in resource member and collection actions

  • Loading branch information...
1 parent 0c34e3f commit 1fc58a889d72e9a36167b41fc3cd055c1f58774e @josh josh committed Dec 7, 2009
Showing with 40 additions and 42 deletions.
  1. +34 −26 actionpack/lib/action_dispatch/routing/mapper.rb
  2. +6 −16 actionpack/test/dispatch/routing_test.rb
View
60 actionpack/lib/action_dispatch/routing/mapper.rb
@@ -41,15 +41,6 @@ def root(options = {})
def match(*args)
options = args.extract_options!
- if args.length > 1
- args.each { |path| match(path, options.reverse_merge(:as => path.to_sym)) }
- return self
- end
-
- if args.first.is_a?(Symbol)
- return match(args.first.to_s, options.merge(:to => args.first.to_sym))
- end
-
path = args.first
conditions, defaults = {}, {}
@@ -185,7 +176,7 @@ def scope(*args)
if name_prefix = options.delete(:name_prefix)
name_prefix_set = true
- name_prefix, @scope[:name_prefix] = @scope[:name_prefix], (@scope[:name_prefix] ? "#{@scope[:name_prefix]}#{name_prefix}" : name_prefix)
+ name_prefix, @scope[:name_prefix] = @scope[:name_prefix], (@scope[:name_prefix] ? "#{@scope[:name_prefix]}_#{name_prefix}" : name_prefix)
else
name_prefix_set = false
end
@@ -235,8 +226,10 @@ def match(*args)
options = (@scope[:options] || {}).merge(options)
- if @scope[:name_prefix] && options[:as]
- options[:as] = "#{@scope[:name_prefix]}#{options[:as]}"
+ if @scope[:name_prefix] && !options[:as].blank?
+ options[:as] = "#{@scope[:name_prefix]}_#{options[:as]}"
+ elsif @scope[:name_prefix] && options[:as].blank?
+ options[:as] = @scope[:name_prefix].to_s
end
args.push(options)
@@ -274,10 +267,6 @@ def collection_name
def id_segment
":#{singular}_id"
end
-
- def member_name_prefix
- "#{member_name}_"
- end
end
class SingletonResource < Resource #:nodoc:
@@ -303,7 +292,7 @@ def resource(*resources, &block)
if @scope[:scope_level] == :resources
with_scope_level(:member) do
- scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name_prefix) do
+ scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name) do
resource(resource.name, options, &block)
end
end
@@ -341,7 +330,7 @@ def resources(*resources, &block)
if @scope[:scope_level] == :resources
with_scope_level(:member) do
- scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name_prefix) do
+ scope(parent_resource.id_segment, :name_prefix => parent_resource.member_name) do
resources(resource.name, options, &block)
end
end
@@ -353,17 +342,19 @@ def resources(*resources, &block)
with_scope_level(:resources, resource) do
yield if block_given?
- collection do
+ with_scope_level(:collection) do
get "", :to => :index, :as => resource.collection_name
post "", :to => :create
get "new", :to => :new, :as => "new_#{resource.singular}"
end
- member do
- get "", :to => :show, :as => resource.member_name
- put "", :to => :update
- delete "", :to => :destroy
- get "edit", :to => :edit, :as => "edit_#{resource.singular}"
+ with_scope_level(:member) do
+ scope(":id") do
+ get "", :to => :show, :as => resource.member_name
+ put "", :to => :update
+ delete "", :to => :destroy
+ get "edit", :to => :edit, :as => "edit_#{resource.singular}"
+ end
end
end
end
@@ -378,7 +369,9 @@ def collection
end
with_scope_level(:collection) do
- yield
+ scope(:name_prefix => parent_resource.member_name, :as => "") do
+ yield
+ end
end
end
@@ -388,14 +381,29 @@ def member
end
with_scope_level(:member) do
- scope(":id") do
+ scope(":id", :name_prefix => parent_resource.member_name, :as => "") do
yield
end
end
end
def match(*args)
options = args.extract_options!
+
+ if args.length > 1
+ args.each { |path| match(path, options) }
+ return self
+ end
+
+ if args.first.is_a?(Symbol)
+ begin
+ old_name_prefix, @scope[:name_prefix] = @scope[:name_prefix], "#{args.first}_#{@scope[:name_prefix]}"
+ return match(args.first.to_s, options.merge(:to => args.first.to_sym))
+ ensure
+ @scope[:name_prefix] = old_name_prefix
+ end
+ end
+
args.push(options)
case options.delete(:on)
View
22 actionpack/test/dispatch/routing_test.rb
@@ -93,7 +93,7 @@ def self.matches?(request)
end
controller :articles do
- scope 'articles', :name_prefix => 'article_' do
+ scope 'articles', :name_prefix => 'article' do
scope :path => ':title', :title => /[a-z]+/, :as => :with_title do
match ':id', :to => :with_id
end
@@ -267,9 +267,7 @@ def test_project_images
post '/projects/1/images/1/revise'
assert_equal 'images#revise', @response.body
- pending do
- assert_equal '/projects/1/images/1/revise', revise_project_image_path(:project_id => '1', :id => '1')
- end
+ assert_equal '/projects/1/images/1/revise', revise_project_image_path(:project_id => '1', :id => '1')
end
end
@@ -291,21 +289,15 @@ def test_projects_people
put '/projects/1/people/1/accessible_projects'
assert_equal 'people#accessible_projects', @response.body
- pending do
- assert_equal '/projects/1/people/1/accessible_projects', accessible_projects_project_person_path(:project_id => '1', :id => '1')
- end
+ assert_equal '/projects/1/people/1/accessible_projects', accessible_projects_project_person_path(:project_id => '1', :id => '1')
post '/projects/1/people/1/resend'
assert_equal 'people#resend', @response.body
- pending do
- assert_equal '/projects/1/people/1/resend', resend_project_person_path(:project_id => '1', :id => '1')
- end
+ assert_equal '/projects/1/people/1/resend', resend_project_person_path(:project_id => '1', :id => '1')
post '/projects/1/people/1/generate_new_password'
assert_equal 'people#generate_new_password', @response.body
- pending do
- assert_equal '/projects/1/people/1/generate_new_password', generate_new_password_project_person_path(:project_id => '1', :id => '1')
- end
+ assert_equal '/projects/1/people/1/generate_new_password', generate_new_password_project_person_path(:project_id => '1', :id => '1')
end
end
@@ -329,9 +321,7 @@ def test_projects_posts
post '/projects/1/posts/1/preview'
assert_equal 'posts#preview', @response.body
- pending do
- assert_equal '/projects/1/posts/1/preview', preview_project_post_path(:project_id => '1', :id => '1')
- end
+ assert_equal '/projects/1/posts/1/preview', preview_project_post_path(:project_id => '1', :id => '1')
get '/projects/1/posts/1/subscription'
assert_equal 'subscriptions#show', @response.body

0 comments on commit 1fc58a8

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