lib/wcc/contentful/store/query.rb in wcc-contentful-1.1.0 vs lib/wcc/contentful/store/query.rb in wcc-contentful-1.1.1
- old
+ new
@@ -32,10 +32,21 @@
@conditions = conditions || []
@options = options || {}
@extra = extra
end
+ FALSE_VALUES = [
+ false, 0,
+ '0', :"0",
+ 'f', :f,
+ 'F', :F,
+ 'false', :false, # rubocop:disable Lint/BooleanSymbol
+ 'FALSE', :FALSE,
+ 'off', :off,
+ 'OFF', :OFF
+ ].to_set.freeze
+
# Returns a new chained Query that has a new condition. The new condition
# represents the WHERE comparison being applied here. The underlying store
# implementation translates this condition statement into an appropriate
# query against the datastore.
#
@@ -48,12 +59,21 @@
# @field The path through the fields of the content type that we are querying against.
# Can be an array, symbol, or dotted-notation path specification.
# @expected The expected value to compare the field's value against.
# @context A context object optionally containing `context[:locale]`
def apply_operator(operator, field, expected, context = nil)
+ operator ||= expected.is_a?(Array) ? :in : :eq
raise ArgumentError, "Operator #{operator} not supported" unless respond_to?(operator)
+ raise ArgumentError, 'value cannot be nil (try using exists: false)' if expected.nil?
+ case operator
+ when :in, :nin, :all
+ expected = Array(expected)
+ when :exists
+ expected = !FALSE_VALUES.include?(expected)
+ end
+
field = field.to_s if field.is_a? Symbol
path = field.is_a?(Array) ? field : field.split('.')
path = self.class.normalize_condition_path(path, context)
@@ -149,10 +169,10 @@
if v.is_a? Hash
flatten_filter_hash(v, path + [k])
elsif op?(k)
{ path: path, op: k.to_sym, expected: v }
else
- { path: path + [k], op: :eq, expected: v }
+ { path: path + [k], op: nil, expected: v }
end
end
end
def known_locales