Class: Locomotive::Mounter::Writer::Api::PagesWriter

Inherits:
Base
  • Object
show all
Defined in:
lib/locomotive/mounter/writer/api/pages_writer.rb

Overview

Push pages to a remote LocomotiveCMS engine.

New pages are created and existing ones are partially updated.

If the :force option is passed, the existing pages are fully updated (title, …etc). But in any cases, the content of the page will be destroyed, unless the layout of the page changes.

Constant Summary collapse

MAX_ATTEMPTS =
5

Instance Attribute Summary collapse

Attributes inherited from Base

#mounting_point, #runner

Instance Method Summary collapse

Methods inherited from Base

#absolute_path, #data?, #each_locale, #get, #initialize, #path_to_file, #post, #put, #replace_content_assets!, #safe_attributes

Constructor Details

This class inherits a constructor from Locomotive::Mounter::Writer::Api::Base

Instance Attribute Details

#new_pagesObject

Returns the value of attribute new_pages.



18
19
20
# File 'lib/locomotive/mounter/writer/api/pages_writer.rb', line 18

def new_pages
  @new_pages
end

#remote_translationsObject

Returns the value of attribute remote_translations.



19
20
21
# File 'lib/locomotive/mounter/writer/api/pages_writer.rb', line 19

def remote_translations
  @remote_translations
end

Instance Method Details

#prepareObject



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/locomotive/mounter/writer/api/pages_writer.rb', line 21

def prepare
  super

  self.new_pages, self.remote_translations = [], {}

  # set the unique identifier to each local page
  self.get(:pages, nil, true).each do |attributes|
    page = self.pages.values.find do |_page|
      _page.safe_fullpath(false) == attributes['fullpath'].dasherize
    end

    self.remote_translations[attributes['fullpath']] = attributes['translated_in']

    page._id = attributes['id'] if page
  end

  # assign the parent_id and the content_type_id to all the pages
  self.pages.values.each do |page|
    next if page.index_or_404?

    page.parent_id = page.parent._id
  end
end

#writeObject

Write all the pages to the remote destination



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
# File 'lib/locomotive/mounter/writer/api/pages_writer.rb', line 46

def write
  self.each_locale do |locale|
    self.output_locale

    done, attempts = {}, 0

    # begin by the layouts (pages inside the layouts folder)
    _write_layouts(pages, done)

    while done.size < pages.length - 1 && attempts < MAX_ATTEMPTS
      _write(pages['index'], done, attempts > 0)

      # keep track of the attempts because we don't want to get an infinite loop.
      attempts += 1
    end

    write_page(pages['404'])

    if done.size < pages.length - 1
      self.log %{Warning: NOT all the pages were pushed.\n\tCheck that the pages inheritance was done right OR that you translated all your pages.\n}.colorize(color: :red)
    end
  end
end