Skip to content

Commit

Permalink
The route matcher accepts a format as a symbol
Browse files Browse the repository at this point in the history
  • Loading branch information
maurogeorge authored and mcmire committed Apr 2, 2015
1 parent a5bc580 commit 15359eb
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 6 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,9 @@
* Fix `validate_uniqueness_of` + `scoped_to` so that it does not raise an error
if a record exists where the scoped attribute is nil. ([#677])

* Fix `route` matcher so if your route includes a default `format`, you can
specify this as a symbol or string. ([#693])

### Features

* Add `on` qualifier to `permit`. This allows you to make an assertion that
Expand All @@ -78,6 +81,7 @@
[#675]: https://github.com/thoughtbot/shoulda-matchers/pull/675
[#677]: https://github.com/thoughtbot/shoulda-matchers/pull/677
[#620]: https://github.com/thoughtbot/shoulda-matchers/pull/620
[#693]: https://github.com/thoughtbot/shoulda-matchers/pull/693

# 2.8.0

Expand Down
4 changes: 4 additions & 0 deletions lib/shoulda/matchers/action_controller/route_matcher.rb
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,10 @@ module ActionController
#
# route(:get, '/posts/1').to('posts#show', id: 1)
#
# You may also specify special parameters such as `:format`:
#
# route(:get, '/posts').to('posts#index', format: :json)
#
# @return [RouteMatcher]
#
def route(method, path)
Expand Down
21 changes: 15 additions & 6 deletions lib/shoulda/matchers/action_controller/route_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ module Matchers
module ActionController
# @private
class RouteParams
PARAMS_TO_SYMBOLIZE = %i{ format }

def initialize(args)
@args = args
end
Expand All @@ -26,17 +28,24 @@ def controller_and_action_given_as_string?
def extract_params_from_string
controller, action = args[0].split('#')
params = (args[1] || {}).merge(controller: controller, action: action)
stringify_values(params)
normalize_values(params)
end

def stringify_params
stringify_values(args[0])
normalize_values(args[0])
end

def stringify_values(hash)
hash.inject({}) do |hash_copy, (key, value)|
hash_copy[key] = stringify(value)
hash_copy
def normalize_values(hash)
hash.each_with_object({}) do |(key, value), hash_copy|
hash_copy[key] = symbolize_or_stringify(key, value)
end
end

def symbolize_or_stringify(key, value)
if key.in?(PARAMS_TO_SYMBOLIZE)
value.to_sym
else
stringify(value)
end
end

Expand Down
37 changes: 37 additions & 0 deletions spec/unit/shoulda/matchers/action_controller/route_matcher_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,26 @@
to(action: 'show', some: 'other', params: 'here')
end
end

context 'when route has a default format' do
it 'accepts' do
expect(controller_with_defined_routes).
to route(:post, "/#{controller_path}").
to(action: 'create', format: 'json')
end

it 'accepts when format is specified as a symbol' do
expect(controller_with_defined_routes).
to route(:post, "/#{controller_path}").
to(action: 'create', format: :json)
end

it 'rejects when format is unspecified' do
expect(controller_with_defined_routes).
not_to route(:post, "/#{controller_path}").
to(action: 'create')
end
end
end

context 'when controller and action are specified as a joined string' do
Expand All @@ -64,6 +84,20 @@
to("#{controller_path}#show", id: 1)
end
end

context 'when route has the format' do
it 'accepts' do
expect(controller_with_defined_routes).
to route(:post, "/#{controller_path}").
to("#{controller_path}#create", format: 'json')
end

it 'rejects when format is unspecified' do
expect(controller_with_defined_routes).
not_to route(:post, "/#{controller_path}").
to(action: 'create')
end
end
end

def controller_with_defined_routes
Expand All @@ -76,6 +110,9 @@ def controller_with_defined_routes
define_routes do
get "/#{_controller_path}", to: "#{_controller_path}#index"
get "/#{_controller_path}/:id", to: "#{_controller_path}#show"
post "/#{_controller_path}",
to: "#{_controller_path}#create",
defaults: { format: :json }
end

controller
Expand Down

0 comments on commit 15359eb

Please sign in to comment.