Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Screen blank array and hash params

  • Loading branch information...
commit 3fa4892c20926cc791e9a83b08ad74e6a4e66f9d 1 parent 15d0679
@tfwright tfwright authored
Showing with 48 additions and 0 deletions.
  1. +13 −0 lib/has_scope.rb
  2. +35 −0 test/has_scope_test.rb
View
13 lib/has_scope.rb
@@ -115,6 +115,7 @@ def apply_scopes(target, hash=params)
end
value = parse_value(options[:type], key, value)
+ value = normalize_blanks(value)
if call_scope && (value.present? || options[:allow_blank])
current_scopes[key] = value
@@ -134,6 +135,18 @@ def parse_value(type, key, value) #:nodoc:
end
end
+ # Screens pseudo-blank params.
+ def normalize_blanks(value) #:nodoc:
+ return value if value.nil?
+ if value.is_a?(Array)
+ value.select { |v| v.present? }
+ elsif value.is_a?(Hash)
+ value.select { |k, v| normalize_blanks(v).present? }.with_indifferent_access
+ else
+ value
+ end
+ end
+
# Call the scope taking into account its type.
def call_scope_by_type(type, scope, target, value, options) #:nodoc:
block = options[:block]
View
35 test/has_scope_test.rb
@@ -161,6 +161,33 @@ def test_scope_of_type_hash_with_using
assert_equal({ :args_paginate => hash }, current_scopes)
end
+ def test_hash_with_blank_values_is_ignored
+ hash = { "page" => "", "per_page" => "" }
+ 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_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_nested_blank_array_param_is_ignored
+ hash = { "parent" => [""] }
+ 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)
@@ -170,6 +197,14 @@ def test_scope_of_type_array
assert_equal({ :categories => array }, current_scopes)
end
+ def test_array_of_blank_values_is_ignored
+ Tree.expects(:categories).never
+ Tree.expects(:all).returns([mock_tree])
+ get :index, :categories => [""]
+ assert_equal([mock_tree], assigns(:trees))
+ assert_equal({ }, current_scopes)
+ end
+
def test_scope_of_invalid_type_silently_fails
Tree.expects(:all).returns([mock_tree])
get :index, :paginate => "1"
Please sign in to comment.
Something went wrong with that request. Please try again.