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



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'lib/locomotive/mounter/models/content_type.rb', line 59

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)
        else
          entry.send(:"#{k}=", 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.



109
110
111
# File 'lib/locomotive/mounter/models/content_type.rb', line 109

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



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

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



151
152
153
154
155
156
157
# File 'lib/locomotive/mounter/models/content_type.rb', line 151

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



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

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



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

def find_field(name_or_id)
  self.fields.detect { |field| field.name.to_s == name_or_id.to_s || field._id == 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.



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

def non_relationship_fields
  self.fields.select { |field| !field.is_relationship? }
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



187
188
189
190
191
192
193
194
195
196
197
198
199
# File 'lib/locomotive/mounter/models/content_type.rb', line 187

def to_hash
  fields = %w(name slug description label_field_name order_by order_by_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

  # 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



167
168
169
170
171
172
173
174
175
176
177
178
179
180
# File 'lib/locomotive/mounter/models/content_type.rb', line 167

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_by_direction public_submission_enabled raw_item_template)

  # 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



201
202
203
# File 'lib/locomotive/mounter/models/content_type.rb', line 201

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



92
93
94
# File 'lib/locomotive/mounter/models/content_type.rb', line 92

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