Class: Locomotive::Mounter::Models::Page
- Defined in:
- lib/locomotive/mounter/models/page.rb
Instance Attribute Summary collapse
-
#children ⇒ Object
other accessors ##.
-
#content_entry ⇒ Object
other accessors ##.
-
#content_type_id ⇒ Object
other accessors ##.
-
#filepath ⇒ Object
path to the file of the template (if mounted from a FS) ##.
-
#parent_id ⇒ String
Get the id of the parent page.
-
#templatized_from_parent ⇒ Object
other accessors ##.
Attributes inherited from Base
#_id, #created_at, #mounting_point, #updated_at
Instance Method Summary collapse
-
#add_child(page) ⇒ Object
Add a child to the page.
-
#depth ⇒ Integer
Depth of the page in the site tree.
-
#depth_and_position ⇒ Integer
Depth and position in the site tree.
-
#extends_template? ⇒ Boolean
Tell if the page extends the template of another page.
-
#find_editable_element(block, slug) ⇒ Object
Find an editable element from its block and slug (the couple is unique).
-
#fullpath_in_default_locale ⇒ String
Return the fullpath in the default locale no matter the current locale is.
-
#fullpath_or_default ⇒ String
Return the fullpath in the current locale.
-
#fullpath_with_setting_slug=(fullpath) ⇒ Object
Modified setter in order to set correctly the slug.
-
#index? ⇒ Boolean
Tell if the page is either the index page.
-
#index_or_404? ⇒ Boolean
Tell if the page is either the index or the 404 page.
-
#localize_fullpath(locales = nil) ⇒ Object
Localize the fullpath based on the parent fullpath in the locales passed in parameter.
-
#model=(content_type) ⇒ Object
Set the content type, attribute required for templatized page.
-
#parent ⇒ Object
fields ##.
-
#raw_template=(content) ⇒ Object
Set the source of the page without any pre-rendering.
-
#redirect? ⇒ Boolean
Is it a redirect page ?.
-
#safe_fullpath(wildcard = true) ⇒ String
Return the fullpath dasherized and with the “*” character for the slug of templatized page.
-
#set_default_template_for_each_locale(default_locale) ⇒ Object
Assign a default template for each locale which has an empty template.
-
#set_editable_elements(attributes) ⇒ Object
Build or update the list of editable elements from a hash whose keys are the couple “[block]/” and the values the content of the editable elements OR an array of attributes.
-
#source ⇒ String
Return the Liquid template based on the raw_template property of the page.
-
#template_fullpath ⇒ String
Return the fullpath of the page whose template is extended in the current template.
-
#to_params ⇒ Hash
Return the params used for the API.
- #to_s ⇒ Object
-
#to_safe_params ⇒ Hash
Return the params used for the API but without all the params.
-
#to_yaml ⇒ String
Return the YAML front matters of the page.
-
#translated_in=(locales) ⇒ Object
Force the translations of a page.
Methods inherited from Base
Methods included from Fields
#[], #attributes, #attributes_with_translations, #initialize, #localized_field?, #to_hash, #translated_in, #translated_in?, #write_attributes
Constructor Details
This class inherits a constructor from Locomotive::Mounter::Models::Base
Instance Attribute Details
#children ⇒ Object
other accessors ##
35 36 37 |
# File 'lib/locomotive/mounter/models/page.rb', line 35 def children @children end |
#content_entry ⇒ Object
other accessors ##
35 36 37 |
# File 'lib/locomotive/mounter/models/page.rb', line 35 def content_entry @content_entry end |
#content_type_id ⇒ Object
other accessors ##
35 36 37 |
# File 'lib/locomotive/mounter/models/page.rb', line 35 def content_type_id @content_type_id end |
#filepath ⇒ Object
path to the file of the template (if mounted from a FS) ##
38 39 40 |
# File 'lib/locomotive/mounter/models/page.rb', line 38 def filepath @filepath end |
#parent_id ⇒ String
Get the id of the parent page.
35 36 37 |
# File 'lib/locomotive/mounter/models/page.rb', line 35 def parent_id @parent_id end |
#templatized_from_parent ⇒ Object
other accessors ##
35 36 37 |
# File 'lib/locomotive/mounter/models/page.rb', line 35 def templatized_from_parent @templatized_from_parent end |
Instance Method Details
#add_child(page) ⇒ Object
Add a child to the page. It also sets the parent of the child. If the parent page is a templatized one, give the same properties to the child.
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 |
# File 'lib/locomotive/mounter/models/page.rb', line 200 def add_child(page) page.parent = self if self.templatized? page.templatized_from_parent = true # copy properties from the parent %w(templatized content_type content_type_id).each do |name| page.send(:"#{name}=", self.send(name.to_sym)) end end (self.children ||= []) << page self.children.sort! { |a, b| (a.position || 999) <=> (b.position || 999) } page end |
#depth ⇒ Integer
Depth of the page in the site tree. Both the index and 404 pages are 0-depth.
150 151 152 153 |
# File 'lib/locomotive/mounter/models/page.rb', line 150 def depth return 0 if %w(index 404).include?(self.fullpath) self.fullpath_or_default.split('/').size end |
#depth_and_position ⇒ Integer
Depth and position in the site tree
159 160 161 |
# File 'lib/locomotive/mounter/models/page.rb', line 159 def depth_and_position self.depth * 1000 + (self.position || 199) end |
#extends_template? ⇒ Boolean
Tell if the page extends the template of another page. Basically, we check if the template of the page includes the “extends” liquid tag.
169 170 171 |
# File 'lib/locomotive/mounter/models/page.rb', line 169 def extends_template? !self.template_fullpath.nil? end |
#find_editable_element(block, slug) ⇒ Object
Find an editable element from its block and slug (the couple is unique)
256 257 258 259 260 |
# File 'lib/locomotive/mounter/models/page.rb', line 256 def find_editable_element(block, slug) (self.editable_elements || []).detect do |el| el.block.to_s == block.to_s && el.slug.to_s == slug.to_s end end |
#fullpath_in_default_locale ⇒ String
Return the fullpath in the default locale no matter the current locale is.
101 102 103 |
# File 'lib/locomotive/mounter/models/page.rb', line 101 def fullpath_in_default_locale self.fullpath_translations[self.mounting_point.default_locale] end |
#fullpath_or_default ⇒ String
Return the fullpath in the current locale. If it does not exist, return the one of the main locale.
93 94 95 |
# File 'lib/locomotive/mounter/models/page.rb', line 93 def fullpath_or_default self.fullpath || self.fullpath_in_default_locale end |
#fullpath_with_setting_slug=(fullpath) ⇒ Object
Modified setter in order to set correctly the slug
135 136 137 138 139 140 141 |
# File 'lib/locomotive/mounter/models/page.rb', line 135 def fullpath_with_setting_slug=(fullpath) if fullpath && self.slug.nil? self.slug = File.basename(fullpath) end self.fullpath_without_setting_slug = fullpath end |
#index? ⇒ Boolean
Tell if the page is either the index page.
53 54 55 |
# File 'lib/locomotive/mounter/models/page.rb', line 53 def index? self.depth == 0 && 'index' == self.slug end |
#index_or_404? ⇒ Boolean
Tell if the page is either the index or the 404 page.
61 62 63 |
# File 'lib/locomotive/mounter/models/page.rb', line 61 def index_or_404? self.depth == 0 && %w(index 404).include?(self.slug) end |
#localize_fullpath(locales = nil) ⇒ Object
Localize the fullpath based on the parent fullpath in the locales passed in parameter.
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 |
# File 'lib/locomotive/mounter/models/page.rb', line 267 def localize_fullpath(locales = nil) locales ||= self.translated_in _parent_fullpath = self.parent.try(:fullpath) _fullpath, _slug = self.fullpath.try(:clone), self.slug.to_s.clone locales.each do |locale| Locomotive::Mounter.with_locale(locale) do if %w(index 404).include?(_slug) && (_fullpath.nil? || _fullpath == _slug) self.fullpath = _slug self.slug = _slug elsif _parent_fullpath == 'index' self.fullpath = self.slug || _slug else self.fullpath = File.join(parent.fullpath || _parent_fullpath, self.slug || _slug) end end end end |
#model=(content_type) ⇒ Object
Set the content type, attribute required for templatized page.
126 127 128 129 |
# File 'lib/locomotive/mounter/models/page.rb', line 126 def model=(content_type) Locomotive::Mounter.logger.warn 'The model attribute is deprecated. Use content_type instead.' self.content_type = content_type end |
#parent ⇒ Object
fields ##
9 |
# File 'lib/locomotive/mounter/models/page.rb', line 9 field :parent, association: true |
#raw_template=(content) ⇒ Object
Set the source of the page without any pre-rendering. Used by the API reader.
314 315 316 317 |
# File 'lib/locomotive/mounter/models/page.rb', line 314 def raw_template=(content) @source ||= {} @source[Locomotive::Mounter.locale] = content end |
#redirect? ⇒ Boolean
Is it a redirect page ?
189 190 191 |
# File 'lib/locomotive/mounter/models/page.rb', line 189 def redirect? !self.redirect_url.blank? end |
#safe_fullpath(wildcard = true) ⇒ String
Return the fullpath dasherized and with the “*” character for the slug of templatized page.
72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 |
# File 'lib/locomotive/mounter/models/page.rb', line 72 def safe_fullpath(wildcard = true) if self.index_or_404? self.slug else base = self.parent.safe_fullpath(wildcard) _slug = if self.templatized? && !self.templatized_from_parent wildcard ? '*' : self.slug elsif !self.translated_in?(Locomotive::Mounter.locale) self.slug_translations[self.mounting_point.default_locale] else self.slug end (base == 'index' ? _slug : File.join(base, _slug)).dasherize end end |
#set_default_template_for_each_locale(default_locale) ⇒ Object
Assign a default template for each locale which has an empty template. This default template is the one defined in the default locale.
292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 |
# File 'lib/locomotive/mounter/models/page.rb', line 292 def set_default_template_for_each_locale(default_locale) default_template = self.template_translations[default_locale.to_sym] return if self.template_blank?(default_template) self.translated_in.each do |locale| next if locale.to_s == default_locale.to_s # current template _template = self.template_translations[locale] # is it blank ? if self.template_blank?(_template) self.template_translations[locale] = default_template end end end |
#set_editable_elements(attributes) ⇒ Object
Build or update the list of editable elements from a hash whose keys are the couple “[block]/” and the values the content of the editable elements OR an array of attributes
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 |
# File 'lib/locomotive/mounter/models/page.rb', line 225 def set_editable_elements(attributes) return if attributes.blank? self.editable_elements ||= [] attributes.to_a.each do |_attributes| if _attributes.is_a?(Array) # attributes is maybe a Hash _splashed = _attributes.first.split('/') block, slug = _splashed[0..-2].join('/'), _splashed.last block = nil if block.blank? _attributes = { 'block' => block, 'slug' => slug, 'content' => _attributes.last } end # does an editable element exist with the same couple block/slug ? if editable_element = self.find_editable_element(_attributes['block'], _attributes['slug']) editable_element.content = _attributes['content'] else self.editable_elements << Locomotive::Mounter::Models::EditableElement.new(_attributes) end end end |
#source ⇒ String
Return the Liquid template based on the raw_template property of the page. If the template is HAML or SLIM, then a pre-rendering to Liquid is done.
324 325 326 327 328 329 330 331 332 333 334 |
# File 'lib/locomotive/mounter/models/page.rb', line 324 def source @source ||= {} if @source[Locomotive::Mounter.locale] @source[Locomotive::Mounter.locale] # memoization elsif self.template @source[Locomotive::Mounter.locale] = self.template.source else nil end end |
#template_fullpath ⇒ String
Return the fullpath of the page whose template is extended in the current template.
178 179 180 181 182 183 |
# File 'lib/locomotive/mounter/models/page.rb', line 178 def template_fullpath return nil if self.source.nil? || self.source.strip.blank? self.source =~ /\{%\s*extends\s+\'?([[\w|\-|\_]|\/]+)\'?\s*%\}/ $1 end |
#to_params ⇒ Hash
Return the params used for the API
370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 |
# File 'lib/locomotive/mounter/models/page.rb', line 370 def to_params params = self.filter_attributes %w(title parent_id slug redirect_url redirect_type handle listed is_layout allow_layout published searchable cache_strategy response_type position templatized seo_title meta_description meta_keywords) # slug params.delete(:slug) if self.depth == 0 # redirect_url params[:redirect] = true unless self.redirect_url.blank? # parent_id params[:parent_id] = self.parent_id unless self.parent_id.blank? # content_type params[:target_klass_slug] = self.content_type.slug if self.templatized && self.content_type # editable_elements params[:editable_elements] = (self.editable_elements || []).map(&:to_params) # raw_template params[:raw_template] = self.source rescue nil params end |
#to_s ⇒ Object
420 421 422 |
# File 'lib/locomotive/mounter/models/page.rb', line 420 def to_s self.fullpath_or_default end |
#to_safe_params ⇒ Hash
Return the params used for the API but without all the params. This can be explained by the fact that for instance the update should preserve the content.
402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 |
# File 'lib/locomotive/mounter/models/page.rb', line 402 def to_safe_params fields = %w(title slug listed is_layout allow_layout published searchable handle cache_strategy redirect_url response_type templatized content_type_id position seo_title meta_description meta_keywords) params = self.attributes.delete_if do |k, v| !fields.include?(k.to_s) || (!v.is_a?(FalseClass) && v.blank?) end.deep_symbolize_keys # redirect_url params[:redirect] = true unless self.redirect_url.blank? # raw_template params[:raw_template] = self.source rescue nil params end |
#to_yaml ⇒ String
Return the YAML front matters of the page
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 |
# File 'lib/locomotive/mounter/models/page.rb', line 340 def to_yaml fields = %w(title slug redirect_url redirect_type handle published listed allow_layout is_layout searchable cache_strategy response_type position seo_title meta_description meta_keywords) _attributes = self.attributes.delete_if do |k, v| !fields.include?(k.to_s) || (!v.is_a?(FalseClass) && v.blank?) end.deep_stringify_keys # useless attributes _attributes.delete('redirect_type') if self.redirect_url.blank? # templatized page _attributes['content_type'] = self.content_type.slug if self.templatized? && !self.templatized_from_parent # editable elements _attributes['editable_elements'] = {} (self.editable_elements || []).each do |editable_element| _attributes['editable_elements'].merge!(editable_element.to_yaml) end _attributes.delete('editable_elements') if _attributes['editable_elements'].empty? _attributes.delete('slug') if self.depth == 0 "#{_attributes.to_yaml}---\n#{self.source}" end |
#translated_in=(locales) ⇒ Object
Force the translations of a page
117 118 119 |
# File 'lib/locomotive/mounter/models/page.rb', line 117 def translated_in=(locales) self._locales = locales.map(&:to_sym) end |