Class: ComfortableMexicanSofa::Fixture::Layout::Importer

Inherits:
Importer
  • Object
show all
Defined in:
lib/comfortable_mexican_sofa/fixture/layout.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!(path = self.path, parent = nil) ⇒ 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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/comfortable_mexican_sofa/fixture/layout.rb', line 4

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