lib/wcc/contentful/store/query.rb in wcc-contentful-1.3.2 vs lib/wcc/contentful/store/query.rb in wcc-contentful-1.4.0.rc1
- old
+ new
@@ -1,9 +1,10 @@
# frozen_string_literal: true
require_relative '../../contentful'
require_relative './query/interface'
+require_relative './query/condition'
module WCC::Contentful::Store
# The default query object returned by Stores that extend WCC::Contentful::Store::Base.
# It exposes several chainable query methods to apply query filters.
# Enumerating the query executes it, caching the result.
@@ -24,15 +25,16 @@
result_set.lazy.map { |row| resolve_includes(row, @options[:include]) }
end
attr_reader :store, :content_type, :conditions
- def initialize(store, content_type:, conditions: nil, options: nil, **extra)
+ def initialize(store, content_type:, conditions: nil, options: nil, configuration: nil, **extra) # rubocop:disable Metrics/ParameterLists
@store = store
@content_type = content_type
@conditions = conditions || []
@options = options || {}
+ @configuration = configuration || WCC::Contentful.configuration
@extra = extra
end
FALSE_VALUES = [
false, 0,
@@ -58,11 +60,11 @@
# @operator one of WCC::Contentful::Store::Query::Interface::OPERATORS
# @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)
+ 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
@@ -73,14 +75,14 @@
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)
+ path = self.class.normalize_condition_path(path, @options)
_append_condition(
- Condition.new(path, operator, expected)
+ Condition.new(path, operator, expected, @configuration&.locale_fallbacks || {})
)
end
WCC::Contentful::Store::Query::Interface::OPERATORS.each do |op|
# @see #apply_operator
@@ -175,19 +177,19 @@
end
end
end
def known_locales
- @known_locales = WCC::Contentful.locales.keys
+ @known_locales ||= WCC::Contentful.locales&.keys || ['en-US']
end
RESERVED_NAMES = %w[fields sys].freeze
# Takes a path array in non-normal form and inserts 'sys', 'fields',
# and the current locale as appropriate to normalize it.
# rubocop:disable Metrics/BlockNesting
- def normalize_condition_path(path, context = nil)
- context_locale = context[:locale] if context.present?
+ def normalize_condition_path(path, options = nil)
+ context_locale = options[:locale]&.to_s if options.present?
context_locale ||= 'en-US'
rev_path = path.reverse
new_path = []
@@ -232,35 +234,7 @@
new_path.flat_map { |x| x }.reverse.freeze
end
# rubocop:enable Metrics/BlockNesting
end
-
- Condition =
- Struct.new(:path, :op, :expected) do
- LINK_KEYS = %w[id type linkType].freeze # rubocop:disable Lint/ConstantDefinitionInBlock
-
- def path_tuples
- @path_tuples ||=
- [].tap do |arr|
- remaining = path.dup
- until remaining.empty?
- locale = nil
- link_sys = nil
- link_field = nil
-
- sys_or_fields = remaining.shift
- field = remaining.shift
- locale = remaining.shift if sys_or_fields == 'fields'
-
- if remaining[0] == 'sys' && LINK_KEYS.include?(remaining[1])
- link_sys = remaining.shift
- link_field = remaining.shift
- end
-
- arr << [sys_or_fields, field, locale, link_sys, link_field].compact
- end
- end
- end
- end
end
end