FilterParamMatcher supports Regexps

This commit is contained in:
Tamir Duberstein 2014-08-07 15:06:29 -07:00 committed by Elliot Winkler
parent 86fb6b9c1e
commit 1cd4a4cd12
3 changed files with 19 additions and 3 deletions

View File

@ -12,6 +12,9 @@
session variable in question was set to nil (previously this actually did
nothing).
* Fix `filter_param` so that it works when `config.filter_parameters` contains
regexes.
### Improvements
* `have_and_belongs_to_many` now checks to make sure that the join table

View File

@ -28,7 +28,7 @@ module Shoulda
# @private
class FilterParamMatcher
def initialize(key)
@key = key.to_s
@key = key
end
def matches?(controller)
@ -52,11 +52,18 @@ module Shoulda
private
def filters_key?
filtered_keys.include?(@key)
filtered_keys.any? do |filter|
case filter
when Regexp
filter =~ @key
else
filter == @key
end
end
end
def filtered_keys
Rails.application.config.filter_parameters.map(&:to_s)
Rails.application.config.filter_parameters
end
end
end

View File

@ -7,6 +7,12 @@ describe Shoulda::Matchers::ActionController::FilterParamMatcher do
expect(nil).to filter_param(:secret)
end
it 'accepts filtering a parameter matching a filtered regex' do
filter(/(?!tip)pin(?!g)/)
expect(nil).to filter_param(:pin)
end
it 'rejects filtering an unfiltered parameter' do
filter(:secret)
matcher = filter_param(:other)