Class: Locomotive::Steam::ContentEntryRepository

Inherits:
Object
  • Object
show all
Includes:
Models::Repository
Defined in:
lib/locomotive/steam/repositories/content_entry_repository.rb

Defined Under Namespace

Classes: Conditions

Instance Attribute Summary collapse

Attributes included from Models::Repository

#adapter, #local_conditions, #scope

Instance Method Summary collapse

Methods included from Models::Repository

#base_url, #build, #create, #delete, #i18n_value_of, #inc, #initialize_copy, #k, #query, #update

Constructor Details

#initialize(adapter, site = nil, locale = nil, content_type_repository = nil) ⇒ ContentEntryRepository

Returns a new instance of ContentEntryRepository.



10
11
12
13
14
15
16
# 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

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type.



8
9
10
# File 'lib/locomotive/steam/repositories/content_entry_repository.rb', line 8

def content_type
  @content_type
end

#content_type_repositoryObject

Returns the value of attribute content_type_repository.



8
9
10
# File 'lib/locomotive/steam/repositories/content_entry_repository.rb', line 8

def content_type_repository
  @content_type_repository
end

Instance Method Details

#all(conditions = {}, &block) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
52
# 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) ⇒ Object



77
78
79
80
# 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 = {}) ⇒ Object



54
55
56
57
# 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 = {}) ⇒ Boolean

Returns:

  • (Boolean)


72
73
74
75
# 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) ⇒ Object



59
60
61
62
# 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) ⇒ Object



64
65
66
# File 'lib/locomotive/steam/repositories/content_entry_repository.rb', line 64

def first(&block)
  all({}, &block).first
end

#group_by_select_option(name) ⇒ Object



108
109
110
111
112
113
114
115
116
117
118
119
120
# 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) ⇒ Object



68
69
70
# File 'lib/locomotive/steam/repositories/content_entry_repository.rb', line 68

def last(&block)
  all({}, &block).last
end

#next(entry) ⇒ Object



100
101
102
# File 'lib/locomotive/steam/repositories/content_entry_repository.rb', line 100

def next(entry)
  next_or_previous(entry, 'gt', 'lt')
end

#previous(entry) ⇒ Object



104
105
106
# File 'lib/locomotive/steam/repositories/content_entry_repository.rb', line 104

def previous(entry)
  next_or_previous(entry, 'lt', 'gt')
end

#to_liquidObject



122
123
124
# 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 = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# 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) ⇒ Object

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.



28
29
30
31
32
33
34
35
36
37
# 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