Class: ComfortableMexicanSofa::Fixture::File::Importer

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

Instance Attribute Summary

Attributes inherited from Importer

#fixture_ids, #force_import, #from, #path, #site, #to

Instance Method Summary collapse

Methods inherited from Importer

#fresh_fixture?, #get_attributes, #initialize, #save_categorizations!

Constructor Details

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

Instance Method Details

#import!Object



3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/comfortable_mexican_sofa/fixture/file.rb', line 3

def import!
  Dir["#{self.path}[^_]*"].each do |file_path|
    filename = ::File.basename(file_path)
    file = self.site.files.where(:file_file_name => filename).first || self.site.files.new
    
    # setting attributes
    categories = []
    if File.exists?(attrs_path = File.join(self.path, "_#{filename}.yml"))
      if fresh_fixture?(file, attrs_path)
        attrs = get_attributes(attrs_path)
        
        block = if (attrs['page'] && attrs['block']) && (page = self.site.pages.find_by_slug(attrs['page']))
          page.blocks.find_by_identifier(attrs['block'])
        end
        
        file.label        = attrs['label']
        file.description  = attrs['description']
        categories        = attrs['categories']
        file.block        = block
      end
    end
    
    # setting actual file
    if fresh_fixture?(file, file_path)
      file.file = open(file_path)
    end
    
    if file.changed? || self.force_import
      if file.save
        save_categorizations!(file, categories)
        ComfortableMexicanSofa.logger.info("[FIXTURES] Imported File \t #{file.file_file_name}")
      else
        ComfortableMexicanSofa.logger.warn("[FIXTURES] Failed to import File \n#{file.errors.inspect}")
      end
    end
    
    self.fixture_ids << file.id
  end
  
  # cleaning up
  self.site.files.where('id NOT IN (?) AND block_id IS NULL', fixture_ids).each{ |s| s.destroy }
end