Class: Locomotive::Mounter::Writer::Api::ContentAssetsWriter

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

Overview

Push content assets to a remote LocomotiveCMS engine.

The assets come from editable content blocks, for instance, in a the text fields of content entries or within editable_***_text. If an asset with the same filename already exists in the engine, the local version will not pushed unless the :force_assets option is passed

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

#remote_assetsObject

Returns the value of attribute remote_assets.



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

def remote_assets
  @remote_assets
end

Instance Method Details

#prepareObject



17
18
19
20
21
22
23
24
# File 'lib/locomotive/mounter/writer/api/content_assets_writer.rb', line 17

def prepare
  self.remote_assets = {}

  # assign an _id to a local content type if possible
  self.get(:content_assets, nil, true).each do |attributes|
    self.remote_assets[attributes['full_filename']] = attributes
  end
end

#write(local_path) ⇒ Object



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
51
52
53
54
# File 'lib/locomotive/mounter/writer/api/content_assets_writer.rb', line 26

def write(local_path)
  status    = :skipped
  asset     = self.build_asset(local_path)
  response  = self.remote_assets[asset.filename]

  asset._id = response['_id'] if response

  self.output_resource_op asset

  if !asset.exists?
    status = :error
  elsif asset.persisted?
    if asset.size != response['size'].to_i && self.force_assets?
      # update it
      response = self.put :content_assets, asset._id, asset.to_params
      status = self.response_to_status(response)
    end
  else
    # create it
    response = self.post :content_assets, asset.to_params, nil, true
    status = self.response_to_status(response)

    self.remote_assets[response['full_filename']] = response
  end

  self.output_resource_op_status asset, status

  [:success, :skipped].include?(status) ? response['url'] : nil
end