Class: Locomotive::Mounter::Models::ContentType
- Defined in:
- lib/locomotive/mounter/models/content_type.rb
Instance Attribute Summary collapse
-
#group_by_field_id ⇒ Object
other accessors ##.
-
#klass_name ⇒ Object
other accessors ##.
Attributes inherited from Base
#_id, #created_at, #mounting_point, #updated_at
Instance Method Summary collapse
-
#build_entry(attributes) ⇒ Object
Build a content entry and add it to the list of entries of the content type.
-
#file_fields ⇒ Array
Return the list of file fields.
-
#find_entries_among(ids) ⇒ Array
Find all the entries whose their _permalink or _label is among the ids passed in parameter.
-
#find_entries_by(name, value) ⇒ Array
Find all the entries by a field and its value.
-
#find_entry(id) ⇒ Object
Find a content entry by its ids (ie: _permalink or _label).
-
#find_field(name_or_id) ⇒ Object
Find a field by its name (string or symbol) or its id (API).
-
#group_by_field ⇒ Object
Return the group_by field.
-
#initialize ⇒ Object
constructor
callbacks ##.
-
#label_field ⇒ Object
Return the label field (by the default the first field).
-
#name ⇒ Object
fields ##.
-
#non_relationship_fields ⇒ Array
Return the list of fields which do not describe a relationship.
-
#to_hash ⇒ Hash
Return a hash with sanitized attributes.
-
#to_params(options = nil) ⇒ Hash
Return the params used for the API.
- #to_s ⇒ Object
-
#with_relationships? ⇒ Boolean
Tell if the content type owns a field which defines a relationship to another content type.
Methods inherited from Base
Methods included from Fields
#attributes, #attributes_with_translations, #localized_field?, #to_yaml, #translated_in, #translated_in?, #write_attributes
Constructor Details
#initialize ⇒ Object
callbacks ##
24 |
# File 'lib/locomotive/mounter/models/content_type.rb', line 24 set_callback :initialize, :after, :sanitize |
Instance Attribute Details
#group_by_field_id ⇒ Object
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_name ⇒ Object
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 .
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_fields ⇒ Array
Return 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.
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.
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)
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)
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_field ⇒ Object
Return 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_field ⇒ Object
Return the label field (by the default the first 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 |
#name ⇒ Object
fields ##
8 |
# File 'lib/locomotive/mounter/models/content_type.rb', line 8 field :name |
#non_relationship_fields ⇒ Array
Return the list of fields which do not describe a relationship.
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_hash ⇒ Hash
Return a hash with sanitized attributes. It will be used to generate the corresponding yaml file.
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.
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( = nil) = { all_fields: false }.merge( || {}) 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 = [:all_fields] ? self.fields : self.non_relationship_fields params[:entries_custom_fields] = _fields.map(&:to_params) params end |
#to_s ⇒ Object
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.
92 93 94 |
# File 'lib/locomotive/mounter/models/content_type.rb', line 92 def with_relationships? self.fields.any? { |field| field.is_relationship? } end |