Class: ComfortableMexicanSofa::Fixture::File::Exporter

Inherits:
Exporter
  • Object
show all
Defined in:
lib/comfortable_mexican_sofa/fixture/file.rb

Instance Attribute Summary

Attributes inherited from Exporter

#from, #path, #site, #to

Instance Method Summary collapse

Methods inherited from Exporter

#initialize, #prepare_folder!

Constructor Details

This class inherits a constructor from ComfortableMexicanSofa::Fixture::Exporter

Instance Method Details

#export!Object



48
49
50
51
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
# File 'lib/comfortable_mexican_sofa/fixture/file.rb', line 48

def export!
  prepare_folder!(self.path)
  
  self.site.files.each do |file|
    file_path = File.join(self.path, file.file_file_name)
    block = file.block
    page = block.present?? block.page : nil
    
    # writing attributes
    open(::File.join(self.path, "_#{file.file_file_name}.yml"), 'w') do |f|
      f.write({
        'label'       => file.label,
        'description' => file.description,
        'categories'  => file.categories.map{|c| c.label},
        'page'        => page.present? ? page.slug : nil,
        'block'       => block.present? ? block.identifier : nil
      }.to_yaml)
    end
    
    # writing content
    data_path = file.file.options[:storage] == :filesystem ?
      file.file.path :
      file.file.url
    
    unless ::File.exists?(data_path)
      ComfortableMexicanSofa.logger.warn("[FIXTURES] No physical File \t #{file.file_file_name}")
      next
    end
      
    open(::File.join(self.path, ::File.basename(file_path)), 'wb') do |f|
      open(data_path) { |src| f.write(src.read) }
    end
    
    ComfortableMexicanSofa.logger.info("[FIXTURES] Exported File \t #{file.file_file_name}")
  end
end