Class: Locomotive::Mounter::Models::Page

Inherits:
Base
  • Object
show all
Defined in:
lib/locomotive/mounter/models/page.rb

Instance Attribute Summary collapse

Attributes inherited from Base

#_id, #created_at, #mounting_point, #updated_at

Instance Method Summary collapse

Methods inherited from Base

#initialize, #persisted?

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

#childrenObject

other accessors ##



32
33
34
# File 'lib/locomotive/mounter/models/page.rb', line 32

def children
  @children
end

#content_entryObject

other accessors ##



32
33
34
# File 'lib/locomotive/mounter/models/page.rb', line 32

def content_entry
  @content_entry
end

#content_type_idObject

other accessors ##



32
33
34
# File 'lib/locomotive/mounter/models/page.rb', line 32

def content_type_id
  @content_type_id
end

#parent_idString

Get the id of the parent page.

Returns:

  • (String)

    The _id attribute of the parent page



32
33
34
# File 'lib/locomotive/mounter/models/page.rb', line 32

def parent_id
  @parent_id
end

Instance Method Details

#add_child(page) ⇒ Object

Add a child to the page. It also sets the parent of the child

Parameters:

  • page (Object)

    The child page

Returns:

  • (Object)

    The child page



187
188
189
190
191
192
193
194
195
# File 'lib/locomotive/mounter/models/page.rb', line 187

def add_child(page)
  page.parent = self

  (self.children ||= []) << page

  self.children.sort! { |a, b| (a.position || 999) <=> (b.position || 999) }

  page
end

#depthInteger

Depth of the page in the site tree. Both the index and 404 pages are 0-depth.

Returns:

  • (Integer)

    The depth



138
139
140
141
# File 'lib/locomotive/mounter/models/page.rb', line 138

def depth
  return 0 if %w(index 404).include?(self.fullpath)
  self.fullpath_or_default.split('/').size
end

#depth_and_positionInteger

Depth and position in the site tree

Returns:

  • (Integer)

    An unique id corresponding to the depth and position



147
148
149
# File 'lib/locomotive/mounter/models/page.rb', line 147

def depth_and_position
  self.depth * 100 + (self.position || 100)
end

#find_editable_element(block, slug) ⇒ Object

Find an editable element from its block and slug (the couple is unique)

Parameters:

  • block (String)

    The name of the block

  • slug (String)

    The slug of the element

Returns:

  • (Object)

    The editable element or nil if not found



231
232
233
234
235
# File 'lib/locomotive/mounter/models/page.rb', line 231

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_localeString

Return the fullpath in the default locale no matter the current locale is.

Returns:



89
90
91
# File 'lib/locomotive/mounter/models/page.rb', line 89

def fullpath_in_default_locale
  self.fullpath_translations[self.mounting_point.default_locale]
end

#fullpath_or_defaultString

Return the fullpath in the current locale. If it does not exist, return the one of the main locale.

Returns:

  • (String)

    A non-blank fullpath



81
82
83
# File 'lib/locomotive/mounter/models/page.rb', line 81

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

Parameters:

  • fullpath (String)

    The fullpath



123
124
125
126
127
128
129
# File 'lib/locomotive/mounter/models/page.rb', line 123

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.

Returns:

  • (Boolean)

    True if index page.



45
46
47
# File 'lib/locomotive/mounter/models/page.rb', line 45

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.

Returns:

  • (Boolean)

    True if index or 404 page.



53
54
55
# File 'lib/locomotive/mounter/models/page.rb', line 53

def index_or_404?
  self.depth == 0 && %w(index 404).include?(self.slug)
end

#is_layout?Boolean

A layout is a page which the template does not include the extend keyword. If the template is blank then, it is not considered as a layout

Returns:

  • (Boolean)

    True if the template can be a layout.



157
158
159
# File 'lib/locomotive/mounter/models/page.rb', line 157

def is_layout?
  self.layout.nil?
end

#layoutString

Return the fullpath of the page which is used as a layout for the current page.

Returns:

  • (String)

    The fullpath to the layout



166
167
168
169
170
171
# File 'lib/locomotive/mounter/models/page.rb', line 166

def layout
  return false if self.source.nil? || self.source.strip.blank?

  self.source =~ /\{%\s*extends\s+\'?([[\w|\-|\_]|\/]+)\'?\s*%\}/
  $1
end

#localize_fullpath(locales = nil) ⇒ Object

Localize the fullpath based on the parent fullpath in the locales passed in parameter.

Parameters:

  • locales (Array) (defaults to: nil)

    The list of locales the fullpath will be translated to. Can be nil (will use the locales returned by translated_in)



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
# File 'lib/locomotive/mounter/models/page.rb', line 242

def localize_fullpath(locales = nil)
  locales ||= self.translated_in
  _parent_fullpath  = self.parent.try(:fullpath)
  _fullpath, _slug  = self.fullpath.try(:clone), self.slug.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.

Parameters:

  • content_type (Object)

    The content type



114
115
116
117
# File 'lib/locomotive/mounter/models/page.rb', line 114

def model=(content_type)
  Locomotive::Mounter.logger.warn 'The model attribute is deprecated. Use content_type instead.'
  self.content_type = content_type
end

#parentObject

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.

Parameters:

  • content (String)

    The HTML raw template



289
290
291
292
# File 'lib/locomotive/mounter/models/page.rb', line 289

def raw_template=(content)
  @source ||= {}
  @source[Locomotive::Mounter.locale] = content
end

#redirect?Boolean

Is it a redirect page ?

Returns:

  • (Boolean)

    True if the redirect_url property is set



177
178
179
# File 'lib/locomotive/mounter/models/page.rb', line 177

def redirect?
  !self.redirect_url.blank?
end

#safe_fullpathString

Return the fullpath dasherized and with the “*” character for the slug of templatized page.

Returns:

  • (String)

    The safe full path or nil if the page is not translated in the current locale



62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/locomotive/mounter/models/page.rb', line 62

def safe_fullpath
  return nil unless self.translated_in?(Locomotive::Mounter.locale)

  # puts "[safe_fullpath] page = #{self.slug.inspect} / #{self.fullpath.inspect} / #{self.parent.inspect}"

  if self.index_or_404?
    self.slug
  else
    base  = self.parent.safe_fullpath
    _slug = self.templatized? ? '*' : self.slug
    (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.

Parameters:

  • default_locale (Symbol / String)

    The default locale



267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# File 'lib/locomotive/mounter/models/page.rb', line 267

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

Parameters:

  • attributes (Hash / Array)

    The attributes of the editable elements



203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
# File 'lib/locomotive/mounter/models/page.rb', line 203

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
      block, slug = _attributes.first.split('/')
      block, slug = nil, block if slug.nil?
      _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

#sourceString

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.

Returns:

  • (String)

    The liquid template or nil if not template has been provided



299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
# File 'lib/locomotive/mounter/models/page.rb', line 299

def source
  @source ||= {}

  if @source[Locomotive::Mounter.locale]
    @source[Locomotive::Mounter.locale] # memoization
  elsif self.template
    if self.template.is_a?(Exception) # comes from the parsing
      # we do not know how to render the page so rethrow the exception
      raise self.template
    end
    source = self.template.need_for_prerendering? ? self.template.render : self.template.data
    @source[Locomotive::Mounter.locale] = source
  else
    nil
  end
end

#to_paramsHash

Return the params used for the API

Returns:

  • (Hash)

    The params



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
# File 'lib/locomotive/mounter/models/page.rb', line 348

def to_params
  params = self.filter_attributes %w(title parent_id slug redirect_url redirect_type handle listed published 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_sObject



397
398
399
# File 'lib/locomotive/mounter/models/page.rb', line 397

def to_s
  self.fullpath_or_default
end

#to_safe_paramsHash

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.

Returns:

  • (Hash)

    The safe params



379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
# File 'lib/locomotive/mounter/models/page.rb', line 379

def to_safe_params
  fields = %w(title slug listed published 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_yamlString

Return the YAML front matters of the page

Returns:

  • (String)

    The YAML version of the page



320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
# File 'lib/locomotive/mounter/models/page.rb', line 320

def to_yaml
  fields = %w(title slug redirect_url redirect_type handle published listed cache_strategy response_type position seo_title meta_description meta_keywords)

  _attributes = self.attributes.delete_if { |k, v| !fields.include?(k.to_s) || v.blank? }.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?

  # 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

Parameters:

  • locales (Array)

    List of locales (Symbol or String)



105
106
107
# File 'lib/locomotive/mounter/models/page.rb', line 105

def translated_in=(locales)
  self._locales = locales.map(&:to_sym)
end