Skip to content

Commit

Permalink
Fix route matcher to fully work with non-string params
Browse files Browse the repository at this point in the history
When using a string to specify controller and action, other parameters
needed to be specified as strings. This commit fixes `route` so that
this does not need to be the case.
  • Loading branch information
mcmire committed Nov 19, 2014
1 parent a1dbca3 commit abb087e
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 4 deletions.
5 changes: 5 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@
* Fix `validate_inclusion_of` used with `with_message` so that it fails if given
a message that does not match the message on the validation. ([#598])

* Fix `route` matcher so that when controller and action are specified in hash
notation (e.g. `posts#show`), route parameters such as `id` do not need to be
specified as a string but may be specified as a number as well ([#602]).

### Features

* Add ability to test `:primary_key` option on associations. ([#597])
Expand All @@ -51,6 +55,7 @@
[#597]: https://github.com/thoughtbot/shoulda-matchers/pull/597
[#537]: https://github.com/thoughtbot/shoulda-matchers/pull/537
[#598]: https://github.com/thoughtbot/shoulda-matchers/pull/598
[#602]: https://github.com/thoughtbot/shoulda-matchers/pull/602

# 2.7.0

Expand Down
13 changes: 9 additions & 4 deletions lib/shoulda/matchers/action_controller/route_params.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,14 +24,19 @@ def controller_and_action_given_as_string?
end

def extract_params_from_string
params = args[1] || {}
controller, action = args[0].split('#')
params.merge!(controller: controller, action: action)
params = (args[1] || {}).merge(controller: controller, action: action)
stringify_values(params)
end

def stringify_params
args[0].each do |key, value|
args[0][key] = stringify(value)
stringify_values(args[0])
end

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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@
to route(:get, "/#{controller_name}").
to("#{controller_name}#index")
end

context 'when route has parameters' do
it 'accepts a non-string parameter' do
expect(controller_with_defined_routes).
to route(:get, "/#{controller_name}/1").
to("#{controller_name}#show", id: 1)
end
end
end

def controller_with_defined_routes
Expand Down

0 comments on commit abb087e

Please sign in to comment.