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.



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

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



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

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.



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

def content_type
  @content_type
end

Instance Method Details

#_class_nameObject



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

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

#_idObject



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

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

#_labelObject



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

def _label
  self[content_type.label_field_name]
end

#_label_of(name) ⇒ Object



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

def _label_of(name)
  content_type.field_label_of(name)
end

#_slugObject Also known as: _permalink



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

def _slug; self[:_slug]; end

#_visible?Boolean Also known as: visible?

Returns:

  • (Boolean)


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

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

#content_type_idObject



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

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

#content_type_slugObject



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

def content_type_slug
  content_type.slug
end

#localized_attributesObject



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

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



79
80
81
# File 'lib/locomotive/steam/entities/content_entry.rb', line 79

def serialize
  super.merge(content_type_id: content_type_id)
end

#to_hashObject



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
# File 'lib/locomotive/steam/entities/content_entry.rb', line 83

def to_hash
  hash = {}

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

  # stack level too deep raised if the _label field is an association (belongs_to, ...etc)
  unless content_type.fields_by_name[content_type.label_field_name].is_relationship?
    _attributes << :_label
  end

  # 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



107
108
109
# File 'lib/locomotive/steam/entities/content_entry.rb', line 107

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

#valid?Boolean

Returns:

  • (Boolean)


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

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