Class: Locomotive::Steam::Liquid::Drops::ContentEntry

Inherits:
I18nBase
  • Object
show all
Defined in:
lib/locomotive/steam/liquid/drops/content_entry.rb

Instance Method Summary collapse

Methods inherited from I18nBase

#context=, #initialize

Methods inherited from Base

#id, #initialize

Constructor Details

This class inherits a constructor from Locomotive::Steam::Liquid::Drops::I18nBase

Instance Method Details

#_idObject



11
12
13
# File 'lib/locomotive/steam/liquid/drops/content_entry.rb', line 11

def _id
  @_source._id.to_s
end

#_labelObject



15
16
17
# File 'lib/locomotive/steam/liquid/drops/content_entry.rb', line 15

def _label
  @_label ||= @_source._label
end

#as_json(options = nil) ⇒ Object



82
83
84
# File 'lib/locomotive/steam/liquid/drops/content_entry.rb', line 82

def as_json(options = nil)
  self.to_hash.as_json(options)
end

#before_method(meth) ⇒ Object



53
54
55
56
57
58
59
60
61
# File 'lib/locomotive/steam/liquid/drops/content_entry.rb', line 53

def before_method(meth)
  return '' if @_source.nil?

  if not @@forbidden_attributes.include?(meth.to_s)
    repository(@_source).value_for(@_source, meth, conditions_for(meth))
  else
    nil
  end
end

#errorsObject



45
46
47
48
49
50
51
# File 'lib/locomotive/steam/liquid/drops/content_entry.rb', line 45

def errors
  if @_source.errors.blank?
    false
  else
    @_source.errors.messages.to_hash.stringify_keys
  end
end

#nextObject

Returns the next content for the parent content type. If no content is found, nil is returned.

Usage:

if article.next % <a href=“path_to article.next %”>Read next article</a> endif %



28
29
30
# File 'lib/locomotive/steam/liquid/drops/content_entry.rb', line 28

def next
  @next ||= repository(@_source).next(@_source).to_liquid
end

#previousObject

Returns the previous content for the parent content type. If no content is found, nil is returned.

Usage:

if article.previous % <a href=“path_to article.previous %”>Read previous article</a> endif %



41
42
43
# File 'lib/locomotive/steam/liquid/drops/content_entry.rb', line 41

def previous
  @previous ||= repository(@_source).previous(@_source).to_liquid
end

#to_hashObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/locomotive/steam/liquid/drops/content_entry.rb', line 63

def to_hash
  @_source.to_hash.tap do |hash|
    hash['id'] = hash['_id']

    @_source.content_type.fields_by_name.each do |name, field|
      case field.type
      when :belongs_to
        hash[name] = liquify_entry(@_source.send(name))._slug if hash["#{name}_id"].present?
      when :many_to_many
        hash[name] = (@_source.send(name) || []).all.map { |e| liquify_entry(e)._slug }.compact
      when :file
        hash[name] = hash["#{name}_url"] = file_field_to_url(hash[name.to_s]) if hash[name.to_s].present?
      when :select
        hash[name] = @_source.send(name) if hash["#{name}_id"].present?
      end
    end
  end
end