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.
-
#order_by_field ⇒ Object
Return the order_by field.
-
#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 .
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_fields ⇒ Array
Return 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.
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.
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)
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)
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_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.
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_field ⇒ Object
Return 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_hash ⇒ Hash
Return a hash with sanitized attributes. It will be used to generate the corresponding yaml file.
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.
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( = nil) = { all_fields: false }.merge( || {}) 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 = [:all_fields] ? self.fields : self.non_relationship_fields params[:entries_custom_fields] = _fields.map(&:to_params) params end |
#to_s ⇒ Object
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.
102 103 104 |
# File 'lib/locomotive/mounter/models/content_type.rb', line 102 def with_relationships? self.fields.any? { |field| field.is_relationship? } end |