Class: ComfortableMexicanSofa::Fixture::Page::Importer
- Inherits:
-
Importer
- Object
- Importer
- ComfortableMexicanSofa::Fixture::Page::Importer
- Defined in:
- lib/comfortable_mexican_sofa/fixture/page.rb
Instance Attribute Summary collapse
-
#target_pages ⇒ Object
Returns the value of attribute target_pages.
Instance Method Summary collapse
Instance Attribute Details
#target_pages ⇒ Object
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 |
# 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'] page.page_workflow = Cms::PageWorkflow.new(attrs['page_workflow']) if page.page_workflow.nil? 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 = [ ] Dir.glob("#{path}/*.html").each do |block_path| identifier = block_path.split('/').last.gsub(/\.html\z/, '') blocks_to_clear.delete(identifier) if fresh_fixture?(page, block_path) blocks_attributes << { :identifier => identifier, :content => read_as_haml(block_path) } 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.warn("[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 |