Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

determine proper way to handle AND/OR filters

  • Loading branch information...
commit 42fc1fbfdfffd9171fde7ec5848b84326a5fdbd1 1 parent 48e84cb
@tpitale tpitale authored
View
10 lib/garb/filter_parameters.rb
@@ -4,7 +4,7 @@ def self.define_operators(*methods)
methods.each do |method|
class_eval <<-CODE
def #{method}(field, value)
- self.parameters << {SymbolOperator.new(field, :#{method}) => value}
+ @filter_hash.merge!({SymbolOperator.new(field, :#{method}) => value})
end
CODE
end
@@ -20,7 +20,11 @@ def initialize
end
def filters(&block)
+ @filter_hash = {}
+
instance_eval &block
+
+ self.parameters << @filter_hash
end
def to_params
@@ -28,8 +32,8 @@ def to_params
param.map do |k,v|
next unless k.is_a?(SymbolOperator)
"#{URI.encode(k.to_google_analytics, /[=<>]/)}#{CGI::escape(v.to_s)}"
- end.join(',') # Hash AND
- end.join(';') # Array OR
+ end.join(';') # Hash AND (no duplicate keys)
+ end.join(',') # Array OR
value.empty? ? {} : {'filters' => value}
end
View
12 test/unit/garb/filter_parameters_test.rb
@@ -9,7 +9,9 @@ def self.should_define_operators(*operators)
symbol = :foo
SymbolOperator.expects(:new).with(:bar, operator).returns(new_operator)
- @filter_parameters.send(operator.to_sym, :bar, 100)
+ @filter_parameters.filters do
+ send(operator.to_sym, :bar, 100)
+ end
parameter = {new_operator => 100}
assert_equal parameter, @filter_parameters.parameters.last
@@ -33,14 +35,14 @@ def self.should_define_operators(*operators)
end
context "when converting parameters hash into query string parameters" do
- should "parameterize hash operators and join elements" do
+ should "parameterize hash operators and join elements with AND" do
@filter_parameters.filters do
- eql(:city, 'New York')
+ eql(:city, 'New York City')
eql(:state, 'New York')
end
- params = {'filters' => 'ga:city%3D%3DNew+York;ga:state%3D%3DNew+York'}
- assert_equal params, @filter_parameters.to_params
+ params = ['ga:city%3D%3DNew+York+City', 'ga:state%3D%3DNew+York']
+ assert_equal params, @filter_parameters.to_params['filters'].split(';').sort
end
should "properly encode operators" do
Please sign in to comment.
Something went wrong with that request. Please try again.