Class: ComfortableMexicanSofa::Fixture::Snippet::Importer

Inherits:
Importer
  • Object
show all
Defined in:
lib/comfortable_mexican_sofa/fixture/snippet.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



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
45
46
# File 'lib/comfortable_mexican_sofa/fixture/snippet.rb', line 4

def import!
  Dir["#{self.path}*/"].each do |path|
    identifier = path.split('/').last
    snippet = self.site.snippets.find_or_initialize_by(:identifier => identifier)
    
    # setting attributes
    categories = []
    if File.exists?(attrs_path = File.join(path, 'attributes.yml'))
      if fresh_fixture?(snippet, attrs_path)
        attrs = get_attributes(attrs_path)
        
        snippet.label = attrs['label']
        categories    = attrs['categories']
      end
    end
    
    # setting content
    %w(html haml).each do |extension|
      if File.exists?(content_path = File.join(path, "content.#{extension}"))
        if fresh_fixture?(snippet, content_path)
          snippet.content = extension == "html" ? 
            ::File.open(content_path).read : 
            Haml::Engine.new(::File.open(content_path).read).render.rstrip
        end
      end
    end
    
    # saving
    if snippet.changed? || self.force_import
      if snippet.save
        save_categorizations!(snippet, categories)
        ComfortableMexicanSofa.logger.info("[FIXTURES] Imported Snippet \t #{snippet.identifier}")
      else
        ComfortableMexicanSofa.logger.warn("[FIXTURES] Failed to import Snippet \n#{snippet.errors.inspect}")
      end
    end
    
    self.fixture_ids << snippet.id
  end
  
  # cleaning up
  self.site.snippets.where('id NOT IN (?)', fixture_ids).each{ |s| s.destroy }
end