Class: ComfortableMexicanSofa::Fixture::Page::Importer

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

Instance Attribute Summary collapse

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 Attribute Details

#target_pagesObject

Returns the value of attribute target_pages.



4
5
6
# File 'lib/comfortable_mexican_sofa/fixture/page.rb', line 4

def target_pages
  @target_pages
end

Instance Method Details

#import!(path = self.path, parent = nil) ⇒ Object



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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/comfortable_mexican_sofa/fixture/page.rb', line 6

def import!(path = self.path, parent = nil)
  Dir["#{path}*/"].each do |path|
    slug = path.split('/').last
    
    page = if parent
      parent.children.where(:slug => slug).first || site.pages.new(:parent => parent, :slug => slug)
    else
      site.pages.root || site.pages.new(:slug => slug)
    end
    
    # setting attributes
    categories = []
    if File.exists?(attrs_path = File.join(path, 'attributes.yml'))
      if fresh_fixture?(page, attrs_path)
        attrs = get_attributes(attrs_path)
        
        page.label        = attrs['label']
        page.layout       = site.layouts.where(:identifier => attrs['layout']).first || parent.try(:layout)
        page.is_published = attrs['is_published'].nil?? true : attrs['is_published']
        page.position     = attrs['position'] if attrs['position']
        
        categories        = attrs['categories']
        
        if attrs['target_page']
          self.target_pages ||= {}
          self.target_pages[page] = attrs['target_page']
        end
      end
    end
    
    # setting content
    blocks_to_clear = page.blocks.collect(&:identifier)
    blocks_attributes = [ ]
    file_extentions = %w(html haml jpg png gif)
    Dir.glob("#{path}/*.{#{file_extentions.join(',')}}").each do |block_path|
      extention = File.extname(block_path)[1..-1]
      identifier = block_path.split('/').last.gsub(/\.(#{file_extentions.join('|')})\z/, '')
      blocks_to_clear.delete(identifier)
      if fresh_fixture?(page, block_path)
        content = case extention
        when 'jpg', 'png', 'gif'
          ::File.open(block_path)
        when 'haml'
          Haml::Engine.new(::File.open(block_path).read).render.rstrip
        else
          ::File.open(block_path).read
        end
        blocks_attributes << {
          :identifier => identifier,
          :content    => content
        }
      end
    end
    
    # deleting removed blocks
    page.blocks.where(:identifier => blocks_to_clear).destroy_all
    
    page.blocks_attributes = blocks_attributes if blocks_attributes.present?
    
    # saving
    if page.changed? || page.blocks_attributes_changed || self.force_import
      if page.save
        save_categorizations!(page, categories)
        ComfortableMexicanSofa.logger.info("[FIXTURES] Imported Page \t #{page.full_path}")
      else
        ComfortableMexicanSofa.logger.warn("[FIXTURES] Failed to import Page \n#{page.errors.inspect}")
      end
    end
    
    self.fixture_ids << page.id
    
    # importing child pages
    import!(path, page)
  end
  
  # linking up target pages
  if self.target_pages.present?
    self.target_pages.each do |page, target|
      if target_page = self.site.pages.where(:full_path => target).first
        page.target_page = target_page
        page.save
      end
    end
  end
  
  # cleaning up
  unless parent
    self.site.pages.where('id NOT IN (?)', self.fixture_ids).each{ |s| s.destroy }
  end
end