Class: Locomotive::Steam::ContentType

Inherits:
Object
  • Object
show all
Extended by:
Forwardable
Includes:
Models::Entity
Defined in:
lib/locomotive/steam/entities/content_type.rb

Instance Attribute Summary

Attributes included from Models::Entity

#associations, #attributes, #base_url, #localized_attributes

Instance Method Summary collapse

Methods included from Models::Entity

#[], #[]=, #_id, #change, #method_missing, #respond_to?, #serialize

Methods included from Models::Concerns::ToJson

#as_json, #to_hash, #to_json

Methods included from Models::Concerns::Validation

#errors, #valid?

Constructor Details

#initialize(attributes = {}) ⇒ ContentType

Returns a new instance of ContentType.



14
15
16
17
18
19
20
# File 'lib/locomotive/steam/entities/content_type.rb', line 14

def initialize(attributes = {})
  super({
    order_by:           '_position',
    order_direction:    'asc',
    recaptcha_required: false
  }.merge(attributes))
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class Locomotive::Steam::Models::Entity

Instance Method Details

#field_label_of(name) ⇒ Object



57
58
59
# File 'lib/locomotive/steam/entities/content_type.rb', line 57

def field_label_of(name)
  fields_by_name[name].label.downcase
end

#fieldsObject



22
23
24
25
# File 'lib/locomotive/steam/entities/content_type.rb', line 22

def fields
  # Note: this returns an instance of the ContentTypeFieldRepository class
  self.entries_custom_fields
end

#fields_by_nameObject



27
28
29
30
31
32
# File 'lib/locomotive/steam/entities/content_type.rb', line 27

def fields_by_name
  @fields_by_name ||= (fields.all.inject({}) do |memo, field|
    memo[field.name] = field
    memo
  end).with_indifferent_access
end

#label_field_nameObject



53
54
55
# File 'lib/locomotive/steam/entities/content_type.rb', line 53

def label_field_name
  (self[:label_field_name] || fields.first.name).to_sym
end

#localized?Boolean

Returns:

  • (Boolean)


40
41
42
# File 'lib/locomotive/steam/entities/content_type.rb', line 40

def localized?
  !fields.localized_names.blank?
end

#localized_namesObject



34
35
36
37
38
# File 'lib/locomotive/steam/entities/content_type.rb', line 34

def localized_names
  # FIXME: select type fields are a bit specific. The label of the options is localized
  # even if the select itself is not (see the _cast_select method in the content_entry entity class)
  fields.localized_names + select_fields.map(&:name)
end

#order_byObject



61
62
63
64
65
66
67
68
69
70
# File 'lib/locomotive/steam/entities/content_type.rb', line 61

def order_by
  name = self[:order_by] == 'manually' ? '_position' : self[:order_by]

  # check if name is an id of field
  if field = fields.find(name)
    name = field.name
  end

  { name.to_sym => self.order_direction.to_s }
end

#persisted_field_namesObject



44
45
46
47
48
49
50
51
# File 'lib/locomotive/steam/entities/content_type.rb', line 44

def persisted_field_names
  [].tap do |names|
    fields_by_name.each do |name, field|
      _name = field.persisted_name
      names << _name if _name
    end
  end
end

#recaptcha_required?Boolean

Returns:

  • (Boolean)


72
73
74
# File 'lib/locomotive/steam/entities/content_type.rb', line 72

def recaptcha_required?
  !!self.recaptcha_required
end