class Locomotive::Steam::ContentEntryRepository

Attributes

content_type[RW]
content_type_repository[RW]

Public Class Methods

new(adapter, site = nil, locale = nil, content_type_repository = nil) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 10
def initialize(adapter, site = nil, locale = nil, content_type_repository = nil)
  @adapter  = adapter
  @scope    = Locomotive::Steam::Models::Scope.new(site, locale)
  @content_type_repository = content_type_repository
  @local_conditions = {}
  @memoized_mappers = {}
end

Public Instance Methods

all(conditions = {}, &block) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 39
def all(conditions = {}, &block)
  conditions, order_by = conditions_without_order_by(conditions)

  # priority:
  # 1/ order_by passed in the conditions parameter
  # 2/ the default order (_position) defined in the content type
  order_by ||= content_type.order_by

  query {
    (block_given? ? instance_eval(&block) : where).
      where(conditions).
        order_by(order_by)
  }.all
end
by_slug(slug) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 77
def by_slug(slug)
  conditions, _ = conditions_without_order_by(_slug: slug)
  first { where(conditions) }
end
count(conditions = {}) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 54
def count(conditions = {})
  conditions, _ = conditions_without_order_by(conditions)
  super() { where(conditions) }
end
exists?(conditions = {}) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 72
def exists?(conditions = {})
  conditions, _ = conditions_without_order_by(conditions)
  query { where(conditions) }.all.size > 0
end
find(id) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 59
def find(id)
  conditions, _ = conditions_without_order_by(_id: self.adapter.make_id(id))
  first { where(conditions) }
end
first(&block) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 64
def first(&block)
  all({}, &block).first
end
group_by_select_option(name) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 108
def group_by_select_option(name)
  return {} if name.nil? || content_type.nil? || content_type.fields_by_name[name].type != :select

  # a big one request to get them grouped by the field
  _groups = all.group_by { |entry| i18n_value_of(entry, name) }

  groups_to_array(name, _groups).tap do |groups|
    # entries with a non existing select_option value?
    unless _groups.blank?
      groups << { name: nil, entries: _groups.values.flatten }.with_indifferent_access
    end
  end
end
last(&block) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 68
def last(&block)
  all({}, &block).last
end
next(entry) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 100
def next(entry)
  next_or_previous(entry, 'gt', 'lt')
end
previous(entry) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 104
def previous(entry)
  next_or_previous(entry, 'lt', 'gt')
end
to_liquid() click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 122
def to_liquid
  Locomotive::Steam::Liquid::Drops::ContentEntryCollection.new(content_type, self)
end
value_for(entry, name, conditions = {}) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 82
def value_for(entry, name, conditions = {})
  return nil if entry.nil?

  if field = content_type.fields_by_name[name]
    value = entry.send(name)

    if %i(has_many many_to_many).include?(field.type)
      # a safe copy of the proxy repository is needed here
      value = value.dup

      # like this, we do not modify the original local conditions
      value.local_conditions.merge!(conditions) if conditions
    end

    value
  end
end
with(type) click to toggle source

this is the starting point of all the next methods. type can be either an instance of the ContentTypeRepository class or the id of a content type.

# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 28
def with(type)
  type = self.content_type_repository.find(type) if type.is_a?(String)

  self.content_type = type # used for creating the scope
  self.scope.context[:content_type] = type

  @local_conditions[:content_type_id] = type.try(:_id)

  self # chainable
end

Private Instance Methods

add_associations_to_mapper(mapper) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 169
def add_associations_to_mapper(mapper)
  self.content_type.association_fields.each do |field|
    mapper.association(field.type, field.name, self.class, field.association_options, &method(:prepare_repository_for_association))
  end
end
add_localized_fields_to_mapper(mapper) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 163
def add_localized_fields_to_mapper(mapper)
  unless self.content_type.localized_names.blank?
    mapper.localized_attributes(*self.content_type.localized_names)
  end
end
conditions_without_order_by(conditions = {}) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 139
def conditions_without_order_by(conditions = {})
  _conditions = prepare_conditions(conditions)
  order_by = _conditions.delete(:order_by) || _conditions.delete('order_by')
  [_conditions, order_by]
end
groups_to_array(name, groups) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 203
def groups_to_array(name, groups)
  content_type_repository.select_options(content_type, name).map do |option|
    option_name = i18n_value_of(option, :name)
    { 'name' => option_name, 'entries' => groups.delete(option_name) || [] }.with_indifferent_access
  end
end
mapper() click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 128
def mapper
  key = self.content_type._id.to_s

  return @memoized_mappers[key] if @memoized_mappers[key]

  @memoized_mappers[key] = super(false).tap do |mapper|
    add_localized_fields_to_mapper(mapper)
    add_associations_to_mapper(mapper)
  end
end
next_or_previous(entry, asc_op, desc_op) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 187
def next_or_previous(entry, asc_op, desc_op)
  return nil if entry.nil?

  with(entry.content_type)

  order_by = self.content_type.order_by
  name, direction = order_by.first
  op = direction == 'asc' ? asc_op : desc_op

  conditions = prepare_conditions({ k(name, op) => i18n_value_of(entry, name) })

  public_send(asc_op == 'gt' ? :first : :last) do
    where(conditions).order_by(order_by)
  end
end
prepare_conditions(*conditions) click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 145
def prepare_conditions(*conditions)
  _conditions = Conditions.new(conditions.first, self.content_type.fields, simple_clone).prepare

  super(_conditions).tap do |final_conditions|
    # skip the default visible condition (_visible: true) by just passing nil
    skip_visible = final_conditions.stringify_keys.fetch('_visible', true).nil?

    # clean it
    final_conditions.delete(:_visible) || final_conditions.delete('_visible')

    final_conditions[:_visible] = true unless skip_visible
  end
end
prepare_repository_for_association(repository, options) click to toggle source

This code is executed once when the association proxy object receives a call to any method

# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 176
def prepare_repository_for_association(repository, options)
  # load the target content type
  _content_type = content_type_repository.find(options[:target_id])

  # the target repository uses this content type for all the other inner calls
  repository.with(_content_type)

  # the content type repository is also need by the target repository
  repository.content_type_repository = content_type_repository
end
simple_clone() click to toggle source
# File lib/locomotive/steam/repositories/content_entry_repository.rb, line 159
def simple_clone
  self.class.new(self.adapter, self.site, self.locale, self.content_type_repository)
end