Class: Locomotive::Mounter::Writer::Api::ThemeAssetsWriter

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

Overview

Push theme assets to a remote LocomotiveCMS engine.

New assets are automatically pushed. Existing ones are not pushed unless the :force option is passed OR if the size of the asset (if not a javascript or stylesheet) has changed.

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

#cached_compiled_assetsObject

cache the compiled theme assets to avoid to perform compilation more than once



24
25
26
# File 'lib/locomotive/mounter/writer/api/theme_assets_writer.rb', line 24

def cached_compiled_assets
  @cached_compiled_assets
end

#checksumsObject

store checksums of remote assets. needed to check if an asset has to be updated or not



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

def checksums
  @checksums
end

#remote_base_urlObject

the assets stored in the engine have the same base url



21
22
23
# File 'lib/locomotive/mounter/writer/api/theme_assets_writer.rb', line 21

def remote_base_url
  @remote_base_url
end

#tmp_folderObject

Other local attributes



15
16
17
# File 'lib/locomotive/mounter/writer/api/theme_assets_writer.rb', line 15

def tmp_folder
  @tmp_folder
end

Instance Method Details

#prepareObject



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/locomotive/mounter/writer/api/theme_assets_writer.rb', line 26

def prepare
  super

  self.checksums = {}

  self.cached_compiled_assets = {}

  # prepare the place where the assets will be stored temporarily.
  self.create_tmp_folder

  # assign an _id to a local content type if possible
  self.get(:theme_assets, nil, true).each do |attributes|
    remote_path = File.join(attributes['folder'], File.basename(attributes['local_path']))

    if theme_asset = self.theme_assets[remote_path]
      theme_asset._id                 = attributes['id']
      self.checksums[theme_asset._id] = attributes['checksum']
    end

    if remote_base_url.nil?
      attributes['url'] =~ /(.*\/sites\/[0-9a-f]+\/theme)/
      self.remote_base_url = $1
    end
  end
end

#writeObject



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
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
# File 'lib/locomotive/mounter/writer/api/theme_assets_writer.rb', line 52

def write
  self.theme_assets_by_priority.each do |theme_asset|
    # track it in the logs
    self.output_resource_op theme_asset

    status  = :skipped
    errors  = []
    file    = self.build_temp_file(theme_asset)
    params  = theme_asset.to_params.merge(source: file, performing_plain_text: false)

    begin
      if theme_asset.persisted?
        # we only update it if the size has changed or if the force option has been set.
        if self.force? || self.theme_asset_changed?(theme_asset)
          response  = self.put :theme_assets, theme_asset._id, params
          status    = self.response_to_status(response)
        else
          status = :same
        end
      else
        response  = self.post :theme_assets, params, nil, true
        status    = self.response_to_status(response)
      end
    rescue Exception => e
      if self.force?
        status, errors = :error, e.message
      else
        raise e
      end
    end

    # very important. we do not want a huge number of non-closed file descriptor.
    file.close

    # track the status
    self.output_resource_op_status theme_asset, status, errors
  end

  # make the stuff like they were before
  self.remove_tmp_folder
end