Class: Locomotive::ContentEntry

Class Method Summary collapse

Instance Method Summary collapse

Methods included from Locomotive::Concerns::ContentEntry::NextPrevious

#next, #previous

Methods included from Locomotive::Concerns::ContentEntry::Authentication

#with_authentication?

Methods included from Locomotive::Concerns::ContentEntry::Localized

#localized?, #translated?, #translated_field?, #translated_in

Methods included from Locomotive::Concerns::ContentEntry::Csv

#to_values

Class Method Details

Find a content entry by its permalink or its id

Parameters:

  • The (String)

    id_or_permalink Permalink (slug) or id

Returns:

  • (Object)

    The content entry matching the permalink/_id or nil if not found



105
106
107
# File 'app/models/locomotive/content_entry.rb', line 105

def self.find_by_id_or_permalink(id_or_permalink)
  any_of({ _id: id_or_permalink }, { _slug: id_or_permalink }).first
end

Find a content entry by its permalink

Parameters:

Returns:

  • (Object)

    The content entry matching the permalink or nil if not found



95
96
97
# File 'app/models/locomotive/content_entry.rb', line 95

def self.find_by_permalink(permalink)
  self.where(_slug: permalink).first
end

.sort_entries!(ids, column = :_position) ⇒ Object

Sort the content entries from an ordered array of content entry ids. Their new positions are persisted.

Parameters:

  • content_type (Array)

    The content type describing the entries

  • The (Array)

    ordered array of ids



115
116
117
118
119
120
121
122
# File 'app/models/locomotive/content_entry.rb', line 115

def self.sort_entries!(ids, column = :_position)
  list = self.any_in(_id: ids.map { |id| BSON::ObjectId.from_string(id.to_s) }).to_a
  ids.each_with_index do |id, position|
    if entry = list.detect { |e| e._id.to_s == id.to_s }
      entry.set column => position
    end
  end
end

Instance Method Details

#_label(type = nil) ⇒ String Also known as: to_label

Any content entry owns a label property which is used in the back-office UI to represent it. The field used as the label is defined within the parent content type.

Parameters:

  • (optional) (Object)

    The content type to avoid to run another MongoDB and useless query.

Returns:

  • (String)

    The “label” of the content entry



73
74
75
76
77
# File 'app/models/locomotive/content_entry.rb', line 73

def _label(type = nil)
  value = self.send(_label_field_name(type))

  value.respond_to?(:to_label) ? value.to_label : value.to_s
end

#_slugObject Also known as: _permalink

validations ##



20
# File 'app/models/locomotive/content_entry.rb', line 20

field :_slug,             localize: true

#as_json(*args) ⇒ Object



85
86
87
# File 'app/models/locomotive/content_entry.rb', line 85

def as_json(*args)
  super.tap { |json| json['_label'] = _label }
end

#content_typeObject

associations ##



29
# File 'app/models/locomotive/content_entry.rb', line 29

belongs_to  :content_type,  class_name: 'Locomotive::ContentType', inverse_of: :entries, custom_fields_parent_klass: true

#content_type_slugObject



81
82
83
# File 'app/models/locomotive/content_entry.rb', line 81

def content_type_slug
  self.content_type.try(:slug)
end

#to_liquid(type = nil) ⇒ Object



128
129
130
# File 'app/models/locomotive/content_entry.rb', line 128

def to_liquid(type = nil)
  to_steam(type).to_liquid
end

#to_steam(type = nil) ⇒ Object



124
125
126
# File 'app/models/locomotive/content_entry.rb', line 124

def to_steam(type = nil)
  (type || self.content_type).to_steam_entry(self)
end

#touch_site_attributeObject



132
133
134
# File 'app/models/locomotive/content_entry.rb', line 132

def touch_site_attribute
  :content_version
end

#visibleObject

named scopes ##



37
# File 'app/models/locomotive/content_entry.rb', line 37

scope :visible, -> { where(_visible: true) }