Permalink
Browse files

Fixes to should_rest_route for singular routes

(1) Controller name remains plural even though route segment is singular (2) Should not test index action by default on singular routes.
  • Loading branch information...
1 parent 4a1b9cf commit e7bed413f43c6834094afb50037572978257fb40 @ffmike committed Oct 10, 2009
Showing with 13 additions and 8 deletions.
  1. +13 −8 shoulda_macros/functionals.rb
View
21 shoulda_macros/functionals.rb
@@ -15,35 +15,40 @@ def should_rest_route *actions
controller = self.name.gsub(/ControllerTest$/, '').underscore
options = actions.extract_options!
plural = options[:singular] ? nil : true
- actions = actions.empty? ? [:index, :show, :new, :create, :edit, :update, :destroy] : actions
+ controller_segment = plural ? controller : controller.singularize
+ if plural
+ actions = actions.empty? ? [:index, :show, :new, :create, :edit, :update, :destroy] : actions
+ else
+ actions = actions.empty? ? [:show, :new, :create, :edit, :update, :destroy] : actions
+ end
actions.each do |action|
case action
when :index
method = :get
- url = "/#{controller}"
+ url = "/#{controller_segment}"
when :show
id = plural && 1
method = :get
- url = "/#{controller}#{plural && '/1'}"
+ url = "/#{controller_segment}#{plural && '/1'}"
when :new
method = :get
- url = "/#{controller}/new"
+ url = "/#{controller_segment}/new"
when :create
method = :post
- url = "/#{controller}"
+ url = "/#{controller_segment}"
when :edit
id = plural && 1
method = :get
- url = "/#{controller}#{plural && '/1'}/edit"
+ url = "/#{controller_segment}#{plural && '/1'}/edit"
when :update
id = plural && 1
method = :put
- url = "/#{controller}#{plural && '/1'}"
+ url = "/#{controller_segment}#{plural && '/1'}"
when :destroy
id = plural && 1
method = :delete
- url = "/#{controller}#{plural && '/1'}"
+ url = "/#{controller_segment}#{plural && '/1'}"
end
id ||= nil

0 comments on commit e7bed41

Please sign in to comment.