Class: Locomotive::Mounter::Models::ContentType

Inherits:
Base
  • Object
show all
Defined in:
lib/locomotive/mounter/models/content_type.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#_id, #created_at, #mounting_point, #updated_at

Instance Method Summary collapse

Methods inherited from Base

#persisted?

Methods included from Fields

#[], #attributes, #attributes_with_translations, #localized_field?, #to_yaml, #translated_in, #translated_in?, #write_attributes

Constructor Details

#initializeObject

callbacks ##



24
# File 'lib/locomotive/mounter/models/content_type.rb', line 24

set_callback :initialize, :after, :sanitize

Instance Attribute Details

#group_by_field_idObject

other accessors ##



28
29
30
# File 'lib/locomotive/mounter/models/content_type.rb', line 28

def group_by_field_id
  @group_by_field_id
end

#klass_nameObject

other accessors ##



28
29
30
# File 'lib/locomotive/mounter/models/content_type.rb', line 28

def klass_name
  @klass_name
end

Instance Method Details

#build_entry(attributes) ⇒ Object

Build a content entry and add it to the list of entries of the content type. The content type will be referenced into the newly built entry .

Parameters:

  • attributes (Hash)

    Attributes of the new content entry

Returns:

  • (Object)

    The newly built content entry



67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/locomotive/mounter/models/content_type.rb', line 67

def build_entry(attributes)
  ContentEntry.new(content_type: self).tap do |entry|
    # set the link to the mounting point
    entry.mounting_point = self.mounting_point

    # do not forget that we are manipulating dynamic fields
    attributes.each do |k, v|
      begin
        field = self.find_field(k)

        if field.nil? && v.is_a?(Hash) # not a dynamic field but localized (permalink ?)
          entry.send(:"#{k}_translations=", v)
        elsif field.nil?
          entry.send(:"#{k}=", v)
        else
          entry.send(:"#{field.name}=", v)
        end
      rescue NoMethodError => e
        Mounter.logger.error e.backtrace
        raise FieldDoesNotExistException.new("The '#{self.slug}' content type does not have a field named '#{k}'.")
      end
    end

    # force the slug to be defined from its label and in all the locales
    entry.send :set_slug

    (self.entries ||= []) << entry
  end
end

#file_fieldsArray

Return the list of file fields.

Returns:

  • (Array)

    The list of file fields.



119
120
121
# File 'lib/locomotive/mounter/models/content_type.rb', line 119

def file_fields
  self.fields.select { |field| field.type == :file }
end

#find_entries_among(ids) ⇒ Array

Find all the entries whose their _permalink or _label is among the ids passed in parameter.

Parameters:

  • ids (Array)

    List of permalinks or labels

Returns:

  • (Array)

    List of content entries or [] if none



150
151
152
# File 'lib/locomotive/mounter/models/content_type.rb', line 150

def find_entries_among(ids)
  (self.entries || []).find_all { |entry| [*ids].any? { |v| [entry._permalink, entry._label].include?(v) } }
end

#find_entries_by(name, value) ⇒ Array

Find all the entries by a field and its value.

Parameters:

  • name (String)

    Name of the field

  • value (String / Array)

    The different values of the field to test

Returns:

  • (Array)

    List of content entries or [] if none



161
162
163
164
165
166
167
# File 'lib/locomotive/mounter/models/content_type.rb', line 161

def find_entries_by(name, value)
  values = [*value]
  (self.entries || []).find_all do |entry|
    raw_value = entry.send(:localized_dynamic_attribute_value, name)
    values.include?(raw_value)
  end
end

#find_entry(id) ⇒ Object

Find a content entry by its ids (ie: _permalink or _label)

Parameters:

  • id (String)

    A permalink or a label

Returns:

  • (Object)

    The content entry if it exists or nil



139
140
141
# File 'lib/locomotive/mounter/models/content_type.rb', line 139

def find_entry(id)
  (self.entries || []).detect { |entry| [entry._permalink, entry._label].include?(id) }
end

#find_field(name_or_id) ⇒ Object

Find a field by its name (string or symbol) or its id (API)

Parameters:

  • name_or_id (String / Symbol)

    Name or Id of the field

Returns:

  • (Object)

    The field if it exists or nil



129
130
131
# File 'lib/locomotive/mounter/models/content_type.rb', line 129

def find_field(name_or_id)
  self.fields.detect { |field| field.matches?(name_or_id) }
end

#group_by_fieldObject

Return the group_by field

Returns:

  • (Object)

    The group_by field



48
49
50
# File 'lib/locomotive/mounter/models/content_type.rb', line 48

def group_by_field
  self.find_field(self.group_by_field_name)
end

#label_fieldObject

Return the label field (by the default the first field)

Returns:

  • (Object)

    The label field



40
41
42
# File 'lib/locomotive/mounter/models/content_type.rb', line 40

def label_field
  self.find_field(self.label_field_name) || self.fields.first
end

#nameObject

fields ##



8
# File 'lib/locomotive/mounter/models/content_type.rb', line 8

field :name

#non_relationship_fieldsArray

Return the list of fields which do not describe a relationship.

Returns:

  • (Array)

    The list of fields.



111
112
113
# File 'lib/locomotive/mounter/models/content_type.rb', line 111

def non_relationship_fields
  self.fields.select { |field| !field.is_relationship? }
end

#order_by_fieldObject

Return the order_by field

Returns:

  • (Object)

    The order_by field



56
57
58
# File 'lib/locomotive/mounter/models/content_type.rb', line 56

def order_by_field
  self.find_field(self.order_by)
end

#to_hashHash

Return a hash with sanitized attributes. It will be used to generate the corresponding yaml file.

Returns:

  • (Hash)

    A hash used by the to_yaml method



200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/locomotive/mounter/models/content_type.rb', line 200

def to_hash
  fields = %w(name slug description label_field_name order_by order_direction public_submission_enabled public_submission_accounts raw_item_template)

  _attributes = self.attributes.delete_if { |k, v| !fields.include?(k.to_s) || v.blank? }.deep_stringify_keys

  # group by
  _attributes['group_by'] = self.group_by_field.name if self.group_by_field

  # order by
  _attributes['order_by'] = self.order_by_field.name if self.order_by_field
  _attributes['order_by'] = 'manually' if self.order_by == '_position'

  # custom fields
  _attributes['fields'] = self.fields

  _attributes
end

#to_params(options = nil) ⇒ Hash

Return the params used for the API. The options parameter can be used to get all the fields even the ones describing a relationship with another content type.

Parameters:

  • options (Hash) (defaults to: nil)

    Default values: { all_fields: false }

Returns:

  • (Hash)

    The params



177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
# File 'lib/locomotive/mounter/models/content_type.rb', line 177

def to_params(options = nil)
  options = { all_fields: false }.merge(options || {})

  params = self.filter_attributes %w(name slug description label_field_name group_by_field_name order_by order_direction public_submission_enabled raw_item_template)

  # group by
  params[:group_by_field_name] = self.group_by_field.name if self.group_by_field

  # order by
  params[:order_by] = '_position' if self.order_by == 'manually'

  # fields
  _fields = options[:all_fields] ? self.fields : self.non_relationship_fields
  params[:entries_custom_fields] = _fields.map(&:to_params)

  params
end

#to_sObject



218
219
220
# File 'lib/locomotive/mounter/models/content_type.rb', line 218

def to_s
  self.name
end

#with_relationships?Boolean

Tell if the content type owns a field which defines a relationship to another content type.

Returns:

  • (Boolean)

    True if a relationship field exists



102
103
104
# File 'lib/locomotive/mounter/models/content_type.rb', line 102

def with_relationships?
  self.fields.any? { |field| field.is_relationship? }
end