Class: Locomotive::Mounter::Models::ContentEntry
- Defined in:
- lib/locomotive/mounter/models/content_entry.rb
Instance Attribute Summary collapse
-
#dynamic_attributes ⇒ Object
Returns the value of attribute dynamic_attributes.
-
#errors ⇒ Object
Returns the value of attribute errors.
-
#main_locale ⇒ Object
Returns the value of attribute main_locale.
Attributes inherited from Base
#_id, #created_at, #mounting_point, #updated_at
Instance Method Summary collapse
- #[](name) ⇒ Object
-
#_label ⇒ String
Return the internal label used to identify a content entry in a YAML file for instance.
-
#_slug ⇒ Object
(also: #_permalink)
fields ##.
-
#dynamic_fields ⇒ Array
Return the list of the fields defined in the content type for which there is a value assigned.
-
#dynamic_getter(name) ⇒ Object
Return the value of a dynamic field and cast it depending on the type of the field (string, date, belongs_to, …etc).
-
#dynamic_setter(name, value) ⇒ Object
Set the value of a dynamic field.
-
#each_dynamic_field(&block) ⇒ Object
Loop over the list of dynamic fields defined in the content type for which there is a value assigned.
-
#initialize ⇒ Object
constructor
callbacks ##.
-
#is_dynamic_field?(name) ⇒ Boolean
Determine if field passed in parameter is one of the dynamic fields.
-
#localized? ⇒ Boolean
By definition, if the label field defined in the content type is localized, then the content entry will be considered as localized.
-
#method_missing(name, *args, &block) ⇒ Object
The magic of dynamic fields happens within this method.
-
#to_hash(nested = true) ⇒ Hash
Return a hash with the label_field value as the key and the other fields as the value.
-
#to_params ⇒ Hash
Return the main default params used for the API, meaning all except the dynamic fields which have to be defined outside the model.
- #to_s ⇒ Object
-
#valid? ⇒ Boolean
Process a minimal validation by checking if the required fields are filled in or not.
-
#write_attributes(attributes) ⇒ Object
(also: #attributes=)
We also have to deal with dynamic attributes so that it does not raise an exception when calling the attributes= method.
Methods inherited from Base
Methods included from Fields
#attributes, #attributes_with_translations, #localized_field?, #to_yaml, #translated_in, #translated_in?
Constructor Details
#initialize ⇒ Object
callbacks ##
23 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 23 set_callback :initialize, :after, :set_default_main_locale |
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(name, *args, &block) ⇒ Object
The magic of dynamic fields happens within this method. It calls the getter/setter of a dynamic field if it is one of them.
194 195 196 197 198 199 200 201 202 203 204 205 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 194 def method_missing(name, *args, &block) if self.is_dynamic_field?(name) if name.to_s.ends_with?('=') name = name.to_s.gsub(/\=$/, '').to_sym self.dynamic_setter(name, args.first) else self.dynamic_getter(name) end else super end end |
Instance Attribute Details
#dynamic_attributes ⇒ Object
Returns the value of attribute dynamic_attributes.
17 18 19 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 17 def dynamic_attributes @dynamic_attributes end |
#errors ⇒ Object
Returns the value of attribute errors.
17 18 19 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 17 def errors @errors end |
#main_locale ⇒ Object
Returns the value of attribute main_locale.
17 18 19 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 17 def main_locale @main_locale end |
Instance Method Details
#[](name) ⇒ Object
184 185 186 187 188 189 190 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 184 def [](name) if is_dynamic_field?(name) self.dynamic_getter(name.to_sym) else super end end |
#_label ⇒ String
Return the internal label used to identify a content entry in a YAML file for instance. It is based on the first field of the related content type.
44 45 46 47 48 49 50 51 52 53 54 55 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 44 def _label field = self.content_type.label_field value = self.dynamic_getter(field.name) if field.type == :belongs_to value.try(:_label) elsif field.type == :file value['url'] else value end end |
#_slug ⇒ Object Also known as: _permalink
fields ##
8 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 8 field :_slug, localized: true |
#dynamic_fields ⇒ Array
Return the list of the fields defined in the content type for which there is a value assigned.
79 80 81 82 83 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 79 def dynamic_fields self.dynamic_attributes.keys.map do |name| self.content_type.find_field(name) end end |
#dynamic_getter(name) ⇒ Object
Return the value of a dynamic field and cast it depending on the type of the field (string, date, belongs_to, …etc).
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 117 def dynamic_getter(name) field = self.content_type.find_field(name) value = self.localized_dynamic_attribute_value(field) case field.type when :date, :date_time value.is_a?(String) ? Chronic.parse(value) : value when :file value.present? ? { 'url' => value } : nil when :belongs_to field.klass.find_entry(value) when :has_many field.klass.find_entries_by(field.inverse_of, [self._label, self._permalink]) when :many_to_many field.klass.find_entries_among(value) else # :string, :text, :select, :boolean, :email, :integer, :float, :tags value end end |
#dynamic_setter(name, value) ⇒ Object
Set the value of a dynamic field. If the value is a hash, it assumes that it represents the translations.
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 144 def dynamic_setter(name, value) self.dynamic_attributes ||= {} self.dynamic_attributes[name.to_sym] ||= {} field = self.content_type.find_field(name) if value.is_a?(Hash) # already localized value.keys.each { |locale| self.add_locale(locale) } self.dynamic_attributes[name.to_sym].merge!(value.symbolize_keys) else if field.is_relationship? || !field.localized self.dynamic_attributes[name.to_sym] = value else self.add_locale(Locomotive::Mounter.locale) self.dynamic_attributes[name.to_sym][Locomotive::Mounter.locale] = value end end end |
#each_dynamic_field(&block) ⇒ Object
Loop over the list of dynamic fields defined in the content type for which there is a value assigned.
@example: each_dynamic_field { |field, value| .… }
90 91 92 93 94 95 96 97 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 90 def each_dynamic_field(&block) return unless block_given? self.dynamic_fields.each do |field| value = self.localized_dynamic_attribute_value(field) block.call(field, value) end end |
#is_dynamic_field?(name) ⇒ Boolean
Determine if field passed in parameter is one of the dynamic fields.
105 106 107 108 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 105 def is_dynamic_field?(name) name = name.to_s.gsub(/\=$/, '').to_sym !self.content_type.try(:find_field, name).nil? end |
#localized? ⇒ Boolean
By definition, if the label field defined in the content type is localized, then the content entry will be considered as localized.
33 34 35 36 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 33 def localized? field = self.content_type.label_field !!field.try(:localized) end |
#to_hash(nested = true) ⇒ Hash
Return a hash with the label_field value as the key and the other fields as the value
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 213 def to_hash(nested = true) # no need of _position and _visible (unless it's false) hash = super.delete_if { |k, v| k == '_position' || (k == '_visible' && v == true) } # also no need of the content type hash.delete('content_type') # dynamic attributes hash.merge!(self.dynamic_attributes.deep_stringify_keys) # no need of the translation of the field name in the current locale label_field = self.content_type.label_field if label_field.localized if !hash[label_field.name].empty? hash[label_field.name].delete(Locomotive::Mounter.locale.to_s) hash.delete(label_field.name) if hash[label_field.name].empty? end else hash.delete(label_field.name) end nested ? { self._label => hash } : hash end |
#to_params ⇒ Hash
Return the main default params used for the API, meaning all except the dynamic fields which have to be defined outside the model.
244 245 246 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 244 def to_params self.filter_attributes %w(_slug _position _visible seo_title meta_keywords meta_description) end |
#to_s ⇒ Object
248 249 250 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 248 def to_s "#{self.content_type.slug} / #{self._slug}" end |
#valid? ⇒ Boolean
Process a minimal validation by checking if the required fields are filled in or not.
62 63 64 65 66 67 68 69 70 71 72 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 62 def valid? self.errors = [] self.content_type.fields.each do |field| if field.required if self.dynamic_getter(field.name).blank? self.errors << field.name end end end self.errors.blank? end |
#write_attributes(attributes) ⇒ Object Also known as: attributes=
We also have to deal with dynamic attributes so that it does not raise an exception when calling the attributes= method.
169 170 171 172 173 174 175 176 177 178 179 180 |
# File 'lib/locomotive/mounter/models/content_entry.rb', line 169 def write_attributes(attributes) _attributes = attributes.select do |name, value| if self.is_dynamic_field?(name) self.dynamic_setter(name, value) false else true end end super(_attributes) end |