fixtures export is done

Oleg committed May 19, 2011
commit 2895282730075bf1d71480ea5cd8e75468b10fb5
Showing 3 changed files with 189 additions and 6 deletions
comfortable_mexican_sofa/fixtures.rb b/lib/comfortable_mexican_sofa/fixtures.rb +80 -0
@@ @@ -6,6 +6,12 @@ module ComfortableMexicanSofa::Fixtures
import_snippets to_hostname, from_hostname
end
+ def self.export_all(from_hostname, to_hostname = nil)
+ export_layouts from_hostname, to_hostname
+ export_pages from_hostname, to_hostname
+ export_snippets from_hostname, to_hostname
+ end
+
def self.import_layouts(to_hostname, from_hostname = nil, path = nil, root = true, parent = nil, layout_ids = [])
return unless site = Cms::Site.find_by_hostname(to_hostname)
return unless path ||= find_fixtures_path((from_hostname || to_hostname), 'layouts')
@@ @@ -148,6 +154,80 @@ module ComfortableMexicanSofa::Fixtures
site.snippets.where('id NOT IN (?)', snippet_ids).each{ |s| s.destroy }
end
+ def self.export_layouts(from_hostname, to_hostname = nil)
+ return unless site = Cms::Site.find_by_hostname(from_hostname)
+ path = File.join(ComfortableMexicanSofa.config.fixtures_path, (to_hostname || site.hostname), 'layouts')
+ FileUtils.rm_rf(path)
+ FileUtils.mkdir_p(path)
+
+ site.layouts.each do |layout|
+ layout_path = File.join(path, layout.ancestors.reverse.collect{|l| l.slug}, layout.slug)
+ FileUtils.mkdir_p(layout_path)
+
+ open(File.join(layout_path, "_#{layout.slug}.yml"), 'w') do |f|
+ f.write({
+ 'label' => layout.label,
+ 'app_layout' => layout.app_layout,
+ 'parent' => layout.parent.try(:slug)
+ }.to_yaml)
+ end
+ open(File.join(layout_path, 'content.html'), 'w') do |f|
+ f.write(layout.content)
+ end
+ open(File.join(layout_path, 'css.css'), 'w') do |f|
+ f.write(layout.css)
+ end
+ open(File.join(layout_path, 'js.js'), 'w') do |f|
+ f.write(layout.js)
+ end
+ end
+ end
+
+ def self.export_pages(from_hostname, to_hostname = nil)
+ return unless site = Cms::Site.find_by_hostname(from_hostname)
+ path = File.join(ComfortableMexicanSofa.config.fixtures_path, (to_hostname || site.hostname), 'pages')
+ FileUtils.rm_rf(path)
+ FileUtils.mkdir_p(path)
+
+ site.pages.each do |page|
+ page.slug = 'index' if page.slug.blank?
+ page_path = File.join(path, page.ancestors.reverse.collect{|p| p.slug.blank?? 'index' : p.slug}, page.slug)
+ FileUtils.mkdir_p(page_path)
+
+ open(File.join(page_path, "_#{page.slug}.yml"), 'w') do |f|
+ f.write({
+ 'label' => page.label,
+ 'layout' => page.layout.try(:slug),
+ 'parent' => page.parent && (page.parent.slug.present?? page.parent.slug : 'index'),
+ 'target_page' => page.target_page.try(:slug),
+ 'is_published' => page.is_published
+ }.to_yaml)
+ end
+ page.blocks_attributes.each do |block|
+ open(File.join(page_path, "#{block[:label]}.html"), 'w') do |f|
+ f.write(block[:content])
+ end
+ end
+ end
+ end
+
+ def self.export_snippets(from_hostname, to_hostname = nil)
+ return unless site = Cms::Site.find_by_hostname(from_hostname)
+ path = File.join(ComfortableMexicanSofa.config.fixtures_path, (to_hostname || site.hostname), 'snippets')
+ FileUtils.rm_rf(path)
+ FileUtils.mkdir_p(path)
+
+ site.snippets.each do |snippet|
+ FileUtils.mkdir_p(snippet_path = File.join(path, snippet.slug))
+ open(File.join(snippet_path, "_#{snippet.slug}.yml"), 'w') do |f|
+ f.write({'label' => snippet.label}.to_yaml)
+ end
+ open(File.join(snippet_path, 'content.html'), 'w') do |f|
+ f.write(snippet.content)
+ end
+ end
+ end
+
protected
def self.find_fixtures_path(hostname, dir)
tasks/comfortable_mexican_sofa.rake b/lib/tasks/comfortable_mexican_sofa.rake +14 -5
@@ @@ -6,15 +6,24 @@ end
namespace :comfortable_mexican_sofa do
namespace :fixtures do
- desc 'Import Fixture data into database (options: FROM=example.local TO=example.com)'
+ desc 'Import Fixture data into database (options: FOLDER=example.local SITE=example.com)'
task :import => :environment do |task, args|
-
- hostname = args[:to] || args[:from]
+ hostname = args[:site] || args[:folder]
site = Cms::Site.find_by_hostname(hostname)
abort "Site with hostname [#{hostname}] not found. Aborting." if !site
- puts "Syncing for #{site.hostname}"
- ComfortableMexicanSofa::Fixtures.import_all(site.hostname, args[:from] || site.hostname)
+ puts "Importing for #{site.hostname}"
+ ComfortableMexicanSofa::Fixtures.import_all(site.hostname, (args[:site] || site.hostname))
+ puts 'Done!'
+ end
+
+ desc 'Export database data into Fixtures (options: SITE=example.com FOLDER=example.local)'
+ task :export => :environment do |task, args|
+ site = Cms::Site.find_by_hostname(args[:folder])
+ abort "Site with hostname [#{hostname}] not found. Aborting." if !site
+
+ puts "Exporting for #{site.hostname}"
+ ComfortableMexicanSofa::Fixtures.export_all((args[:site] || site.hostname), site.hostname)
puts 'Done!'
end
end
test/unit/fixtures_test.rb +95 -1
@@ @@ -24,7 +24,6 @@ class FixturesTest < ActiveSupport::TestCase
end
def test_import_layouts_updating_and_deleting
-
layout = cms_layouts(:default)
nested_layout = cms_layouts(:nested)
child_layout = cms_layouts(:child)
@@ @@ -207,4 +206,99 @@ class FixturesTest < ActiveSupport::TestCase
end
end
+ def test_export_layouts
+ host_path = File.join(ComfortableMexicanSofa.config.fixtures_path, 'test.test')
+ layout_1_attr_path = File.join(host_path, 'layouts/nested/_nested.yml')
+ layout_1_content_path = File.join(host_path, 'layouts/nested/content.html')
+ layout_1_css_path = File.join(host_path, 'layouts/nested/css.css')
+ layout_1_js_path = File.join(host_path, 'layouts/nested/js.js')
+ layout_2_attr_path = File.join(host_path, 'layouts/nested/child/_child.yml')
+ layout_2_content_path = File.join(host_path, 'layouts/nested/child/content.html')
+ layout_2_css_path = File.join(host_path, 'layouts/nested/child/css.css')
+ layout_2_js_path = File.join(host_path, 'layouts/nested/child/js.js')
+
+ ComfortableMexicanSofa::Fixtures.export_layouts('test.host', 'test.test')
+
+ assert File.exists?(layout_1_attr_path)
+ assert File.exists?(layout_1_content_path)
+ assert File.exists?(layout_1_css_path)
+ assert File.exists?(layout_1_js_path)
+
+ assert File.exists?(layout_2_attr_path)
+ assert File.exists?(layout_2_content_path)
+ assert File.exists?(layout_2_css_path)
+ assert File.exists?(layout_2_js_path)
+
+ assert_equal ({
+ 'label' => 'Nested Layout',
+ 'app_layout' => nil,
+ 'parent' => nil
+ }), YAML.load_file(layout_1_attr_path)
+ assert_equal cms_layouts(:nested).content, IO.read(layout_1_content_path)
+ assert_equal cms_layouts(:nested).css, IO.read(layout_1_css_path)
+ assert_equal cms_layouts(:nested).js, IO.read(layout_1_js_path)
+
+ assert_equal ({
+ 'label' => 'Child Layout',
+ 'app_layout' => nil,
+ 'parent' => 'nested'
+ }), YAML.load_file(layout_2_attr_path)
+ assert_equal cms_layouts(:child).content, IO.read(layout_2_content_path)
+ assert_equal cms_layouts(:child).css, IO.read(layout_2_css_path)
+ assert_equal cms_layouts(:child).js, IO.read(layout_2_js_path)
+
+ FileUtils.rm_rf(host_path)
+ end
+
+ def test_export_pages
+ host_path = File.join(ComfortableMexicanSofa.config.fixtures_path, 'test.test')
+ page_1_attr_path = File.join(host_path, 'pages/index/_index.yml')
+ page_1_block_a_path = File.join(host_path, 'pages/index/default_field_text.html')
+ page_1_block_b_path = File.join(host_path, 'pages/index/default_page_text.html')
+ page_2_attr_path = File.join(host_path, 'pages/index/child-page/_child-page.yml')
+
+ ComfortableMexicanSofa::Fixtures.export_pages('test.host', 'test.test')
+
+ assert_equal ({
+ 'label' => 'Default Page',
+ 'layout' => 'default',
+ 'parent' => nil,
+ 'target_page' => nil,
+ 'is_published' => true
+ }), YAML.load_file(page_1_attr_path)
+ assert_equal cms_blocks(:default_field_text).content, IO.read(page_1_block_a_path)
+ assert_equal cms_blocks(:default_page_text).content, IO.read(page_1_block_b_path)
+
+ assert_equal ({
+ 'label' => 'Child Page',
+ 'layout' => 'default',
+ 'parent' => 'index',
+ 'target_page' => nil,
+ 'is_published' => true
+ }), YAML.load_file(page_2_attr_path)
+
+ FileUtils.rm_rf(host_path)
+ end
+
+ def test_export_snippets
+ host_path = File.join(ComfortableMexicanSofa.config.fixtures_path, 'test.test')
+ attr_path = File.join(host_path, 'snippets/default/_default.yml')
+ content_path = File.join(host_path, 'snippets/default/content.html')
+
+ ComfortableMexicanSofa::Fixtures.export_snippets('test.host', 'test.test')
+
+ assert File.exists?(attr_path)
+ assert File.exists?(content_path)
+ assert_equal ({'label' => 'Default Snippet'}), YAML.load_file(attr_path)
+ assert_equal cms_snippets(:default).content, IO.read(content_path)
+
+ FileUtils.rm_rf(host_path)
+ end
+
+ def test_export_all
+ host_path = File.join(ComfortableMexicanSofa.config.fixtures_path, 'test.test')
+ ComfortableMexicanSofa::Fixtures.export_all('test.host', 'test.test')
+ FileUtils.rm_rf(host_path)
+ end
+
end
\ No newline at end of file