Module: Locomotive::Steam::Liquid::Scopeable

Included in:
Drops::ProxyCollection, Drops::Site
Defined in:
lib/locomotive/steam/liquid/scopeable.rb

Defined Under Namespace

Classes: Condition

Instance Method Summary collapse

Instance Method Details

#_apply_scope_order(entries, order_by) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/locomotive/steam/liquid/scopeable.rb', line 36

def _apply_scope_order(entries, order_by)
  return entries if order_by.blank?

  name, direction = order_by.split.map(&:to_sym)

  Locomotive::Steam::Logger.info "[with_scope] order_by #{name} #{direction || 'asc'}"

  if direction == :asc || direction.nil?
    entries.sort { |a, b| a.send(name) <=> b.send(name) }
  else
    entries.sort { |a, b| b.send(name) <=> a.send(name) }
  end
end

#apply_scope(entries) ⇒ Object



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
# File 'lib/locomotive/steam/liquid/scopeable.rb', line 6

def apply_scope(entries)
  if @context['with_scope'].blank?
    entries
  else
    # extract the conditions
    _conditions = @context['with_scope'].clone.delete_if { |k, _| %w(order_by per_page page).include?(k) }

    # build the chains of conditions
    conditions = _conditions.map { |name, value| Condition.new(name, value) }

    Locomotive::Steam::Logger.info "[with_scope] conditions: #{conditions.map(&:to_s).join(', ')}"

    # get only the entries matching ALL the conditions
    _entries = entries.find_all do |content|
      accepted = true

      conditions.each do |_condition|
        unless _condition.matches?(content)
          accepted = false
          break # no to go further
        end
      end

      accepted
    end

    self._apply_scope_order(_entries, @context['with_scope']['order_by'])
  end
end