class ComfortableMexicanSofa::Seeds::File::Exporter

Public Class Methods

new(from, to = from) click to toggle source
# File lib/comfortable_mexican_sofa/seeds/file/exporter.rb, line 6
def initialize(from, to = from)
  super
  self.path = ::File.join(ComfortableMexicanSofa.config.seeds_path, to, "files/")
end

Public Instance Methods

export!() click to toggle source
# File lib/comfortable_mexican_sofa/seeds/file/exporter.rb, line 11
def export!
  prepare_folder!(path)

  site.files.each do |file|
    file_path = File.join(path, file.attachment.filename.to_s)

    # writing attributes
    ::File.open(::File.join(path, "_#{file.attachment.filename}.yml"), "w") do |f|
      f.write({
        "label"       => file.label,
        "description" => file.description,
        "categories"  => file.categories.map(&:label)
      }.to_yaml)
    end

    # writing content
    begin
      ::File.open(::File.join(path, ::File.basename(file_path)), "wb") do |f|
        f.write(file.attachment.download)
      end
    rescue Errno::ENOENT, OpenURI::HTTPError
      message = "[CMS SEEDS] No physical File \t #{file.attachment.filename}"
      ComfortableMexicanSofa.logger.warn(message)
      next
    end

    message = "[CMS SEEDS] Exported File \t #{file.attachment.filename}"
    ComfortableMexicanSofa.logger.info(message)
  end
end