adjusting fixtures, rake task actaully works now

Oleg committed May 19, 2011
commit 3f0537aaa381db05af667696f79e890a62cfe22f
Showing 6 changed files with 67 additions and 73 deletions
app/controllers/cms_admin/base_controller.rb +1 -1
@@ @@ -35,7 +35,7 @@ protected
def load_fixtures
return unless ComfortableMexicanSofa.config.enable_fixtures
- ComfortableMexicanSofa::Fixtures.sync(@cms_site)
+ ComfortableMexicanSofa::Fixtures.import_all(@cms_site.hostname)
flash.now[:error] = 'CMS Fixtures are enabled. All changes done here will be discarded.'
end
end
app/controllers/cms_content_controller.rb +1 -1
@@ @@ -28,7 +28,7 @@ protected
def load_fixtures
return unless ComfortableMexicanSofa.config.enable_fixtures
- ComfortableMexicanSofa::Fixtures.sync(@cms_site)
+ ComfortableMexicanSofa::Fixtures.import_all(@cms_site.hostname)
end
def load_cms_site
app/models/cms/snippet.rb +1 -1
@@ @@ -32,7 +32,7 @@ protected
# gotta reload every single page. Kinda sucks, but might be ok unless there
# are hundreds of pages.
def clear_cached_page_content
- site.pages.all.each{ |page| page.save! }
+ site.pages.all.each{ |page| page.save }
end
end
comfortable_mexican_sofa/fixtures.rb b/lib/comfortable_mexican_sofa/fixtures.rb +20 -19
@@ @@ -1,14 +1,14 @@
module ComfortableMexicanSofa::Fixtures
- def self.sync(site)
- return unless site
- sync_layouts(site)
- sync_pages(site)
- sync_snippets(site)
+ def self.import_all(to_hostname, from_hostname = nil)
+ import_layouts to_hostname, from_hostname
+ import_pages to_hostname, from_hostname
+ import_snippets to_hostname, from_hostname
end
- def self.sync_layouts(site, path = nil, root = true, parent = nil, layout_ids = [])
- return unless path ||= find_path(site, 'layouts')
+ 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')
Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path|
slug = path.split('/').last
@@ @@ -49,7 +49,7 @@ module ComfortableMexicanSofa::Fixtures
layout_ids << layout.id
# checking for nested fixtures
- layout_ids += sync_layouts(site, path, false, layout, layout_ids)
+ layout_ids += import_layouts(to_hostname, from_hostname, path, false, layout, layout_ids)
end
# removing all db entries that are not in fixtures
@@ @@ -59,8 +59,9 @@ module ComfortableMexicanSofa::Fixtures
layout_ids
end
- def self.sync_pages(site, path = nil, root = true, parent = nil, page_ids = [])
- return unless path ||= find_path(site, 'pages')
+ def self.import_pages(to_hostname, from_hostname = nil, path = nil, root = true, parent = nil, page_ids = [])
+ return unless site = Cms::Site.find_by_hostname(to_hostname)
+ return unless path ||= find_fixtures_path((from_hostname || to_hostname), 'pages')
Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path|
slug = path.split('/').last
@@ @@ -102,7 +103,7 @@ module ComfortableMexicanSofa::Fixtures
page_ids << page.id
# checking for nested fixtures
- page_ids += sync_pages(site, path, false, page, page_ids)
+ page_ids += import_pages(to_hostname, from_hostname, path, false, page, page_ids)
end
# removing all db entries that are not in fixtures
@@ @@ -112,8 +113,9 @@ module ComfortableMexicanSofa::Fixtures
page_ids
end
- def self.sync_snippets(site)
- return unless path = find_path(site, 'snippets')
+ def self.import_snippets(to_hostname, from_hostname = nil)
+ return unless site = Cms::Site.find_by_hostname(to_hostname)
+ return unless path = find_fixtures_path((from_hostname || to_hostname), 'snippets')
snippet_ids = []
Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path|
@@ @@ -146,12 +148,11 @@ module ComfortableMexicanSofa::Fixtures
site.snippets.where('id NOT IN (?)', snippet_ids).each{ |s| s.destroy }
end
- def self.find_path(site, dir)
- path = nil
- File.exists?(path = File.join(ComfortableMexicanSofa.config.fixtures_path, site.hostname, dir)) ||
- !ComfortableMexicanSofa.config.enable_multiple_sites &&
- File.exists?(path = File.join(ComfortableMexicanSofa.config.fixtures_path, dir))
- return path
+ protected
+
+ def self.find_fixtures_path(hostname, dir)
+ path = File.join(ComfortableMexicanSofa.config.fixtures_path, hostname, dir)
+ File.exists?(path) ? path : nil
end
end
\ No newline at end of file
tasks/comfortable_mexican_sofa.rake b/lib/tasks/comfortable_mexican_sofa.rake +6 -9
@@ @@ -6,18 +6,15 @@ end
namespace :comfortable_mexican_sofa do
namespace :fixtures do
- desc 'Import Fixture data into database (options: SITE=example.com)'
+ desc 'Import Fixture data into database (options: FROM=example.local TO=example.com)'
task :import => :environment do |task, args|
- site = if ComfortableMexicanSofa.config.enable_multiple_sites
- Cms::Site.find_by_hostname(args[:site])
- else
- Cms::Site.first
- end
- abort 'SITE is not found. Aborting.' if !site
- puts "Syncing for #{site.hostname}"
- ComfortableMexicanSofa::Fixtures.sync(site)
+ hostname = args[:to] || args[:from]
+ 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 'Done!'
end
end
test/unit/fixtures_test.rb +38 -42
@@ @@ -2,30 +2,12 @@ require File.expand_path('../test_helper', File.dirname(__FILE__))
class FixturesTest < ActiveSupport::TestCase
- def setup
- @site = cms_sites(:default)
- @site.update_attribute(:hostname, 'example.com')
- end
-
- def test_sync
- Cms::Page.destroy_all
- Cms::Layout.destroy_all
- Cms::Snippet.destroy_all
-
- assert_difference 'Cms::Layout.count', 2 do
- assert_difference 'Cms::Page.count', 2 do
- assert_difference 'Cms::Snippet.count', 1 do
- ComfortableMexicanSofa::Fixtures.sync(@site)
- end
- end
- end
- end
-
- def test_sync_layouts_creating
+ def test_import_layouts_creating
Cms::Layout.delete_all
assert_difference 'Cms::Layout.count', 2 do
- ComfortableMexicanSofa::Fixtures.sync_layouts(@site)
+ ComfortableMexicanSofa::Fixtures.import_layouts('test.host', 'example.com')
+
assert layout = Cms::Layout.find_by_slug('default')
assert_equal 'Default Fixture Layout', layout.label
assert_equal "<html>\n <body>\n {{ cms:page:content }}\n </body>\n</html>", layout.content
@@ @@ -41,7 +23,7 @@ class FixturesTest < ActiveSupport::TestCase
end
end
- def test_sync_layouts_updating_and_deleting
+ def test_import_layouts_updating_and_deleting
layout = cms_layouts(:default)
nested_layout = cms_layouts(:nested)
@@ @@ -51,7 +33,7 @@ class FixturesTest < ActiveSupport::TestCase
child_layout.update_attribute(:updated_at, 10.years.ago)
assert_difference 'Cms::Layout.count', -1 do
- ComfortableMexicanSofa::Fixtures.sync_layouts(@site)
+ ComfortableMexicanSofa::Fixtures.import_layouts('test.host', 'example.com')
layout.reload
assert_equal 'Default Fixture Layout', layout.label
@@ @@ -70,9 +52,9 @@ class FixturesTest < ActiveSupport::TestCase
end
end
- def test_sync_layouts_ignoring
+ def test_import_layouts_ignoring
layout = cms_layouts(:default)
- layout_path = File.join(ComfortableMexicanSofa.config.fixtures_path, @site.hostname, 'layouts', 'default')
+ layout_path = File.join(ComfortableMexicanSofa.config.fixtures_path, 'example.com', 'layouts', 'default')
attr_file_path = File.join(layout_path, '_default.yml')
content_file_path = File.join(layout_path, 'content.html')
css_file_path = File.join(layout_path, 'css.css')
@@ @@ -83,7 +65,7 @@ class FixturesTest < ActiveSupport::TestCase
assert layout.updated_at >= File.mtime(css_file_path)
assert layout.updated_at >= File.mtime(js_file_path)
- ComfortableMexicanSofa::Fixtures.sync_layouts(@site)
+ ComfortableMexicanSofa::Fixtures.import_layouts('test.host', 'example.com')
layout.reload
assert_equal 'default', layout.slug
assert_equal 'Default Layout', layout.label
@@ @@ -92,7 +74,7 @@ class FixturesTest < ActiveSupport::TestCase
assert_equal 'default_js', layout.js
end
- def test_sync_pages_creating
+ def test_import_pages_creating
Cms::Page.delete_all
layout = cms_layouts(:default)
@@ @@ -102,7 +84,7 @@ class FixturesTest < ActiveSupport::TestCase
nested.update_attribute(:content, '<html>{{cms:page:left}}<br/>{{cms:page:right}}</html>')
assert_difference 'Cms::Page.count', 2 do
- ComfortableMexicanSofa::Fixtures.sync_pages(@site)
+ ComfortableMexicanSofa::Fixtures.import_pages('test.host', 'example.com')
assert page = Cms::Page.find_by_full_path('/')
assert_equal layout, page.layout
@@ @@ -118,7 +100,7 @@ class FixturesTest < ActiveSupport::TestCase
end
end
- def test_sync_pages_updating_and_deleting
+ def test_import_pages_updating_and_deleting
page = cms_pages(:default)
page.update_attribute(:updated_at, 10.years.ago)
assert_equal 'Default Page', page.label
@@ @@ -127,7 +109,7 @@ class FixturesTest < ActiveSupport::TestCase
child.update_attribute(:slug, 'old')
assert_no_difference 'Cms::Page.count' do
- ComfortableMexicanSofa::Fixtures.sync_pages(@site)
+ ComfortableMexicanSofa::Fixtures.import_pages('test.host', 'example.com')
page.reload
assert_equal 'Home Fixture Page', page.label
@@ @@ -136,27 +118,27 @@ class FixturesTest < ActiveSupport::TestCase
end
end
- def test_sync_pages_ignoring
+ def test_import_pages_ignoring
page = cms_pages(:default)
- page_path = File.join(ComfortableMexicanSofa.config.fixtures_path, @site.hostname, 'pages', 'index')
+ page_path = File.join(ComfortableMexicanSofa.config.fixtures_path, 'example.com', 'pages', 'index')
attr_file_path = File.join(page_path, '_index.yml')
content_file_path = File.join(page_path, 'content.html')
assert page.updated_at >= File.mtime(attr_file_path)
assert page.updated_at >= File.mtime(content_file_path)
- ComfortableMexicanSofa::Fixtures.sync_pages(@site)
+ ComfortableMexicanSofa::Fixtures.import_pages('test.host', 'example.com')
page.reload
assert_equal nil, page.slug
assert_equal 'Default Page', page.label
assert_equal "\nlayout_content_a\ndefault_page_text_content_a\ndefault_snippet_content\ndefault_page_text_content_b\nlayout_content_b\ndefault_snippet_content\nlayout_content_c", page.content
end
- def test_sync_snippets_creating
+ def test_import_snippets_creating
Cms::Snippet.delete_all
assert_difference 'Cms::Snippet.count' do
- ComfortableMexicanSofa::Fixtures.sync_snippets(@site)
+ ComfortableMexicanSofa::Fixtures.import_snippets('test.host', 'example.com')
assert snippet = Cms::Snippet.last
assert_equal 'default', snippet.slug
assert_equal 'Default Fixture Snippet', snippet.label
@@ @@ -164,7 +146,7 @@ class FixturesTest < ActiveSupport::TestCase
end
end
- def test_sync_snippets_updating
+ def test_import_snippets_updating
snippet = cms_snippets(:default)
snippet.update_attribute(:updated_at, 10.years.ago)
assert_equal 'default', snippet.slug
@@ @@ -172,7 +154,7 @@ class FixturesTest < ActiveSupport::TestCase
assert_equal 'default_snippet_content', snippet.content
assert_no_difference 'Cms::Snippet.count' do
- ComfortableMexicanSofa::Fixtures.sync_snippets(@site)
+ ComfortableMexicanSofa::Fixtures.import_snippets('test.host', 'example.com')
snippet.reload
assert_equal 'default', snippet.slug
assert_equal 'Default Fixture Snippet', snippet.label
@@ @@ -180,12 +162,12 @@ class FixturesTest < ActiveSupport::TestCase
end
end
- def test_sync_snippets_deleting
+ def test_import_snippets_deleting
snippet = cms_snippets(:default)
snippet.update_attribute(:slug, 'old')
assert_no_difference 'Cms::Snippet.count' do
- ComfortableMexicanSofa::Fixtures.sync_snippets(@site)
+ ComfortableMexicanSofa::Fixtures.import_snippets('test.host', 'example.com')
assert snippet = Cms::Snippet.last
assert_equal 'default', snippet.slug
assert_equal 'Default Fixture Snippet', snippet.label
@@ @@ -195,20 +177,34 @@ class FixturesTest < ActiveSupport::TestCase
end
end
- def test_sync_snippets_ignoring
+ def test_import_snippets_ignoring
snippet = cms_snippets(:default)
- snippet_path = File.join(ComfortableMexicanSofa.config.fixtures_path, @site.hostname, 'snippets', 'default')
+ snippet_path = File.join(ComfortableMexicanSofa.config.fixtures_path, 'example.com', 'snippets', 'default')
attr_file_path = File.join(snippet_path, '_default.yml')
content_file_path = File.join(snippet_path, 'content.html')
assert snippet.updated_at >= File.mtime(attr_file_path)
assert snippet.updated_at >= File.mtime(content_file_path)
- ComfortableMexicanSofa::Fixtures.sync_snippets(@site)
+ ComfortableMexicanSofa::Fixtures.import_snippets('test.host', 'example.com')
snippet.reload
assert_equal 'default', snippet.slug
assert_equal 'Default Snippet', snippet.label
assert_equal 'default_snippet_content', snippet.content
end
+ def test_import_all
+ Cms::Page.destroy_all
+ Cms::Layout.destroy_all
+ Cms::Snippet.destroy_all
+
+ assert_difference 'Cms::Layout.count', 2 do
+ assert_difference 'Cms::Page.count', 2 do
+ assert_difference 'Cms::Snippet.count', 1 do
+ ComfortableMexicanSofa::Fixtures.import_all('test.host', 'example.com')
+ end
+ end
+ end
+ end
+
end
\ No newline at end of file