class Locomotive::Steam::ContentEntryRepository::Conditions

Public Class Methods

new(conditions = {}, fields, target_repository) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 212
def initialize(conditions = {}, fields, target_repository)
  @conditions = conditions.try(:with_indifferent_access) || {}
  @fields, @operators = fields, {}
  @target_repository = target_repository
  @locale = target_repository.locale

  @conditions.each do |name, value|
    _name, operator = name.to_s.split('.')
    @operators[_name] = operator if operator
  end
end

Public Instance Methods

prepare() click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 224
def prepare
  # _id (primary key)
  _prepare([Locomotive::Steam::ContentTypeField.new(name: '_id')]) do |_, value|
    @target_repository.adapter.make_id(value)
  end

  # select
  _prepare(@fields.selects) do |field, value|
    # FIXME: [only in Wagon], if the user changes the locale, since all content is stored in memory,
    # we have to change the locale in the repository used to fetch the select options.
    field.select_options.locale = @locale

    field.select_options.by_name(value).try(:_id)
  end

  # date
  _prepare(@fields.dates_and_date_times) { |field, value| value_to_date(value, field.type) }

  # belongs_to
  _prepare(@fields.belongs_to) { |field, value| value_to_id(value, field.target_id) }

  # many_to_many
  _prepare(@fields.many_to_many) { |field, value| values_to_ids(value, field.target_id) }

  @conditions
end

Protected Instance Methods

_prepare(fields) { |field, value| ... } click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 253
def _prepare(fields, &block)
  fields.each do |field|
    name      = field.name.to_s
    operator  = @operators[name]
    _name     = operator ? "#{name}.#{operator}" : name

    if @conditions.has_key?(_name)
      value = @conditions[_name]

      # delete old name
      @conditions.delete(_name)

      # build the new name with the prefix and the operator if there is one
      _name = field.persisted_name + (operator ? ".#{operator}" : '')

      # store the new name
      @conditions[_name] = yield(field, value)
    end
  end
end
slug_to_id(slug, target_id) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 294
def slug_to_id(slug, target_id)
  return nil if slug.blank?

  if _repository = @target_repository.with(target_id)
    _entry = _repository.first { where(_slug: slug).only(:_id) }
    _entry.try(:_id)
  end
end
value_to_date(value, type) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 303
def value_to_date(value, type)
  _value = if value.is_a?(String)
    Chronic.time_class = Time.zone
    Chronic.parse(value)
  else
    value
  end
  type == :date ? _value&.to_date : _value&.to_datetime
end
value_to_id(value, target_id) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 278
def value_to_id(value, target_id)
  _value = if value.is_a?(Hash)
    value['_id'] || value[:_id]
  elsif value.respond_to?(:each) # array
    values_to_ids(value, target_id)
  else
    value.respond_to?(:_id) ? value._id : value
  end

  if (id = @target_repository.adapter.make_id(_value)) == false
    slug_to_id(value, target_id)
  else
    id
  end
end
values_to_ids(value, target_id) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 274
def values_to_ids(value, target_id)
  [*value].map { |_value| value_to_id(_value, target_id) }
end