Class: Writefully::Tools::Pencil

Inherits:
Stationery show all
Defined in:
lib/writefully/tools/pencil.rb

Instance Attribute Summary

Attributes inherited from Stationery

#asset, #content, #index, #site_id

Instance Method Summary collapse

Methods inherited from Stationery

#initialize, #perform

Constructor Details

This class inherits a constructor from Writefully::Tools::Stationery

Instance Method Details

#can_update_db?(uploaded) ⇒ Boolean

Returns:

  • (Boolean)


31
32
33
34
35
36
37
# File 'lib/writefully/tools/pencil.rb', line 31

def can_update_db? uploaded
  if asset.names.count == uploaded.count
    true
  else 
    raise SomeAssetsNotUploaded, "Some assets was not uploaded"
  end
end

#computed_attributesObject



11
12
13
14
15
16
17
# File 'lib/writefully/tools/pencil.rb', line 11

def computed_attributes
  content.meta.merge({ 
    "content" => asset.convert_for(content.body),
    "details" => asset.convert_for(content.details),
    "trashed" => false
  })
end

#upload_assetsObject



25
26
27
28
29
# File 'lib/writefully/tools/pencil.rb', line 25

def upload_assets
  asset.names.map do |name|
    Celluloid::Actor[:pigeons].future.upload(asset.endpoint, asset.path, name)
  end
end

#useObject



5
6
7
8
9
# File 'lib/writefully/tools/pencil.rb', line 5

def use
  assets_uploaded = upload_assets.map(&:value).compact
  written_to_db   = future.write if can_update_db?(assets_uploaded)
  terminate if written_to_db.value
end

#writeObject



19
20
21
22
23
# File 'lib/writefully/tools/pencil.rb', line 19

def write
  compute_type.by_site(site_id).where(slug: content.slug)
                .first_or_initialize
                  .update_attributes(computed_attributes)
end