Class: Locomotive::Steam::ContentEntry

Inherits:
Object
  • Object
show all
Includes:
Models::Entity
Defined in:
lib/locomotive/steam/entities/content_entry.rb

Defined Under Namespace

Classes: FileField

Instance Attribute Summary collapse

Attributes included from Models::Entity

#associations, #attributes, #base_url

Instance Method Summary collapse

Methods included from Models::Entity

#[], #[]=, #change, #respond_to?

Methods included from Models::Concerns::ToJson

#as_json, #to_json

Methods included from Models::Concerns::Validation

#errors

Constructor Details

#initialize(attributes = {}) ⇒ ContentEntry

Returns a new instance of ContentEntry.



11
12
13
14
15
16
17
18
# File 'lib/locomotive/steam/entities/content_entry.rb', line 11

def initialize(attributes = {})
  super({
    _visible:     true,
    _position:    0,
    created_at:   Time.zone.now,
    updated_at:   Time.zone.now
  }.merge(attributes))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object



28
29
30
31
32
33
34
35
36
# File 'lib/locomotive/steam/entities/content_entry.rb', line 28

def method_missing(name, *args, &block)
  if is_dynamic_attribute?(name)
    cast_value(name)
  elsif attributes.include?(name)
    self[name]
  else
    super
  end
end

Instance Attribute Details

#content_typeObject

Returns the value of attribute content_type.



9
10
11
# File 'lib/locomotive/steam/entities/content_entry.rb', line 9

def content_type
  @content_type
end

Instance Method Details

#_class_nameObject



58
59
60
# File 'lib/locomotive/steam/entities/content_entry.rb', line 58

def _class_name
  "Locomotive::ContentEntry#{content_type_id}"
end

#_idObject



20
# File 'lib/locomotive/steam/entities/content_entry.rb', line 20

def _id; self[:_id] || self[:id]; end

#_labelObject



62
63
64
# File 'lib/locomotive/steam/entities/content_entry.rb', line 62

def _label
  self[content_type.label_field_name]
end

#_slugObject Also known as: _permalink



25
# File 'lib/locomotive/steam/entities/content_entry.rb', line 25

def _slug; self[:_slug]; end

#_visible?Boolean Also known as: visible?

Returns:

  • (Boolean)


22
# File 'lib/locomotive/steam/entities/content_entry.rb', line 22

def _visible?; !!self[:_visible]; end

#content_type_idObject



50
51
52
# File 'lib/locomotive/steam/entities/content_entry.rb', line 50

def content_type_id
  @content_type.try(&:_id) || attributes[:content_type_id]
end

#content_type_slugObject



54
55
56
# File 'lib/locomotive/steam/entities/content_entry.rb', line 54

def content_type_slug
  content_type.slug
end

#localized_attributesObject



66
67
68
69
70
71
72
# File 'lib/locomotive/steam/entities/content_entry.rb', line 66

def localized_attributes
  @localized_attributes.tap do |hash|
    if hash && hash.has_key?(content_type.label_field_name.to_sym)
      hash[:_label] = true
    end
  end
end

#serializeObject



74
75
76
# File 'lib/locomotive/steam/entities/content_entry.rb', line 74

def serialize
  super.merge(content_type_id: content_type_id)
end

#to_hashObject



78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/locomotive/steam/entities/content_entry.rb', line 78

def to_hash
  hash = {}

  # default attributes
  _attributes = %i(_id _slug _label _visible _position content_type_slug created_at updated_at)

  # dynamic attributes
  _attributes += content_type.persisted_field_names

  _attributes.each do |name|
    hash[name.to_s] = send(name) rescue nil
  end

  # errors?
  hash['errors'] = self.errors.to_hash.stringify_keys unless self.errors.empty?

  hash
end

#to_liquidObject



97
98
99
# File 'lib/locomotive/steam/entities/content_entry.rb', line 97

def to_liquid
  Locomotive::Steam::Liquid::Drops::ContentEntry.new(self)
end

#valid?Boolean

Returns:

  • (Boolean)


38
39
40
41
42
43
44
# File 'lib/locomotive/steam/entities/content_entry.rb', line 38

def valid?
  errors.clear
  content_type.fields.required.each do |field|
    errors.add_on_blank(field.name.to_sym)
  end
  errors.empty?
end