Skip to content
Browse files

Add support for nested hashes

  • Loading branch information...
1 parent 52a6442 commit f3dd234a2204f0c923c02f4218be17cd5e2dc99a @tfwright committed Feb 11, 2013
Showing with 15 additions and 5 deletions.
  1. +6 −5 lib/has_scope.rb
  2. +9 −0 test/has_scope_test.rb
View
11 lib/has_scope.rb
@@ -115,7 +115,7 @@ def apply_scopes(target, hash=params)
end
value = parse_value(options[:type], key, value)
- value = normalize_blanks(options[:type], value) if value
+ value = normalize_blanks(value)
if call_scope && (value.present? || options[:allow_blank])
current_scopes[key] = value
@@ -136,11 +136,12 @@ def parse_value(type, key, value) #:nodoc:
end
# Screens pseudo-blank params.
- def normalize_blanks(type, value) #:nodoc:
- if type == :array
+ def normalize_blanks(value) #:nodoc:
+ return value if value.nil?
+ if value.is_a?(Array)
value.select { |v| v.present? }
- elsif type == :hash
- value.select { |k, v| v.present? }
+ elsif [Hash, HashWithIndifferentAccess].include?(value.class)
+ value.select { |k, v| normalize_blanks(v).present? }
else
value
end
View
9 test/has_scope_test.rb
@@ -170,6 +170,15 @@ def test_hash_with_blank_values_is_ignored
assert_equal({ }, current_scopes)
end
+ def test_nested_hash_with_blank_values_is_ignored
+ hash = { "parent" => {"children" => ""} }
+ Tree.expects(:paginate).never
+ Tree.expects(:all).returns([mock_tree])
+ get :index, :paginate => hash
+ assert_equal([mock_tree], assigns(:trees))
+ assert_equal({ }, current_scopes)
+ end
+
def test_scope_of_type_array
array = %w(book kitchen sport)
Tree.expects(:categories).with(array).returns(Tree)

0 comments on commit f3dd234

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