sync for layouts is working

Oleg committed Apr 29, 2011
commit d2f741afaf6ce94674ec8113ccff1f11f472e078
Showing 4 changed files with 118 additions and 19 deletions
cms_fixtures/example.com/layouts/default/_default.yml b/db/cms_fixtures/example.com/layouts/default/_default.yml +1 -1
@@ @@ -1 +1 @@
- label: Default Layout
\ No newline at end of file
+ label: Default Fixture Layout
\ No newline at end of file
cms_fixtures/example.com/layouts/default/nested/_nested.yml b/db/cms_fixtures/example.com/layouts/default/nested/_nested.yml +1 -1
@@ @@ -1 +1 @@
- label: Default Layout
\ No newline at end of file
+ label: Default Fixture Nested Layout
\ No newline at end of file
comfortable_mexican_sofa/fixtures.rb b/lib/comfortable_mexican_sofa/fixtures.rb +51 -7
@@ @@ -4,36 +4,80 @@ module ComfortableMexicanSofa::Fixtures
end
- def self.sync_layouts(site)
- return unless path = find_path(site, 'layouts')
+ def self.sync_layouts(site, path = nil, root = true, parent = nil, layout_ids = [])
+ return unless path ||= find_path(site, 'layouts')
+ Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path|
+ slug = path.split('/').last
+ layout = site.layouts.find_by_slug(slug) || site.layouts.new(:slug => slug)
+
+ # updating attributes
+ if File.exists?(file_path = File.join(path, "_#{slug}.yml"))
+ if layout.new_record? || File.mtime(file_path) > layout.updated_at
+ attributes = YAML.load_file(file_path).symbolize_keys!
+ layout.label = attributes[:label] || slug.titleize
+ end
+ elsif layout.new_record?
+ layout.label = slug.titleize
+ end
+
+ # updating content
+ if File.exists?(file_path = File.join(path, 'content.html'))
+ if layout.new_record? || File.mtime(file_path) > layout.updated_at
+ layout.content = File.open(file_path, 'rb').read
+ end
+ end
+ if File.exists?(file_path = File.join(path, 'css.css'))
+ if layout.new_record? || File.mtime(file_path) > layout.updated_at
+ layout.css = File.open(file_path, 'rb').read
+ end
+ end
+ if File.exists?(file_path = File.join(path, 'js.js'))
+ if layout.new_record? || File.mtime(file_path) > layout.updated_at
+ layout.js = File.open(file_path, 'rb').read
+ end
+ end
+
+ # saving
+ layout.parent = parent
+ layout.save! if layout.changed?
+ layout_ids << layout.id
+
+ # checking for nested fixtures
+ layout_ids += sync_layouts(site, path, false, layout, layout_ids)
+ end
+ # removing all db entries that are not in fixtures
+ Cms::Layout.where('id NOT IN (?)', layout_ids).each{ |s| s.destroy } if root
+
+ # returning ids of layouts in fixtures
+ layout_ids
end
def self.sync_pages
-
+ # TODO
end
def self.sync_snippets(site)
return unless path = find_path(site, 'snippets')
snippet_ids = []
- Dir.glob("#{path}/*").select{|s| File.directory?(s)}.each do |path|
+ Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path|
slug = path.split('/').last
snippet = site.snippets.find_by_slug(slug) || site.snippets.new(:slug => slug)
# updating attributes
if File.exists?(file_path = File.join(path, "_#{slug}.yml"))
if snippet.new_record? || File.mtime(file_path) > snippet.updated_at
- snippet_attributes = YAML.load_file(file_path).symbolize_keys!
- snippet.label = snippet_attributes[:label] || slug.titleize
+ attributes = YAML.load_file(file_path).symbolize_keys!
+ snippet.label = attributes[:label] || slug.titleize
end
elsif snippet.new_record?
snippet.label = slug.titleize
end
# updating content
- if File.exists?(file_path = File.join(path, "content.html"))
+ if File.exists?(file_path = File.join(path, 'content.html'))
if snippet.new_record? || File.mtime(file_path) > snippet.updated_at
snippet.content = File.open(file_path, 'rb').read
end
test/unit/fixtures_test.rb +65 -10
@@ @@ -8,19 +8,74 @@ class ViewMethodsTest < ActiveSupport::TestCase
end
def test_sync_layouts_creating
- ComfortableMexicanSofa::Fixtures.sync_layouts(@site)
- end
-
- def test_sync_layouts_updating
- flunk
+ Cms::Layout.delete_all
+
+ assert_difference 'Cms::Layout.count', 2 do
+ ComfortableMexicanSofa::Fixtures.sync_layouts(@site)
+ 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
+ assert_equal 'body{color: red}', layout.css
+ assert_equal '// default js', layout.js
+
+ assert nested_layout = Cms::Layout.find_by_slug('nested')
+ assert_equal layout, nested_layout.parent
+ assert_equal 'Default Fixture Nested Layout', nested_layout.label
+ assert_equal "<div class='left'> {{ cms:page:left }} </div>\n<div class='right'> {{ cms:page:right }} </div>", nested_layout.content
+ assert_equal 'div{float:left}', nested_layout.css
+ assert_equal '// nested js', nested_layout.js
+ end
end
- def test_sync_layouts_deleting
- flunk
+ def test_sync_layouts_updating_and_deleting
+
+ layout = cms_layouts(:default)
+ nested_layout = cms_layouts(:nested)
+ child_layout = cms_layouts(:child)
+ layout.update_attribute(:updated_at, 10.years.ago)
+ nested_layout.update_attribute(:updated_at, 10.years.ago)
+ child_layout.update_attribute(:updated_at, 10.years.ago)
+
+ assert_difference 'Cms::Layout.count', -1 do
+ ComfortableMexicanSofa::Fixtures.sync_layouts(@site)
+
+ layout.reload
+ assert_equal 'Default Fixture Layout', layout.label
+ assert_equal "<html>\n <body>\n {{ cms:page:content }}\n </body>\n</html>", layout.content
+ assert_equal 'body{color: red}', layout.css
+ assert_equal '// default js', layout.js
+
+ nested_layout.reload
+ assert_equal layout, nested_layout.parent
+ assert_equal 'Default Fixture Nested Layout', nested_layout.label
+ assert_equal "<div class='left'> {{ cms:page:left }} </div>\n<div class='right'> {{ cms:page:right }} </div>", nested_layout.content
+ assert_equal 'div{float:left}', nested_layout.css
+ assert_equal '// nested js', nested_layout.js
+
+ assert_nil Cms::Layout.find_by_slug('child')
+ end
end
def test_sync_layouts_ignoring
- flunk
+ layout = cms_layouts(:default)
+ layout_path = File.join(ComfortableMexicanSofa.config.fixtures_path, @site.hostname, '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')
+ js_file_path = File.join(layout_path, 'js.js')
+
+ assert layout.updated_at >= File.mtime(attr_file_path)
+ assert layout.updated_at >= File.mtime(content_file_path)
+ assert layout.updated_at >= File.mtime(css_file_path)
+ assert layout.updated_at >= File.mtime(js_file_path)
+
+ ComfortableMexicanSofa::Fixtures.sync_layouts(@site)
+ layout.reload
+ assert_equal 'default', layout.slug
+ assert_equal 'Default Layout', layout.label
+ assert_equal "{{cms:field:default_field_text:text}}\nlayout_content_a\n{{cms:page:default_page_text:text}}\nlayout_content_b\n{{cms:snippet:default}}\nlayout_content_c", layout.content
+ assert_equal 'default_css', layout.css
+ assert_equal 'default_js', layout.js
end
def test_sync_pages_creating
@@ @@ -84,8 +139,8 @@ class ViewMethodsTest < ActiveSupport::TestCase
def test_sync_snippets_ignoring
snippet = cms_snippets(:default)
- snippet_path = File.join(ComfortableMexicanSofa.config.fixtures_path, @site.hostname, 'snippets', 'default')
- attr_file_path = File.join(snippet_path, '_default.yml')
+ snippet_path = File.join(ComfortableMexicanSofa.config.fixtures_path, @site.hostname, '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)