fixtures for pages are almost there
Oleg
committed Apr 29, 2011
commit 5d368fe05699f24195e9b723d472b3782f90cfb1
Showing 11
changed files with
89 additions
and 18 deletions
cms_fixtures/example.com/pages/index/_index.yml b/db/cms_fixtures/example.com/pages/index/_index.yml
+1
-1
| @@ | @@ -1,2 +1,2 @@ |
| - | label: Home Page |
| + | label: Home Fixture Page |
| layout: default | |
| \ No newline at end of file | |
cms_fixtures/example.com/pages/index/child/_child.yml b/db/cms_fixtures/example.com/pages/index/child/_child.yml
+2
-1
| @@ | @@ -1 +1,2 @@ |
| - | label: Child Page |
| \ No newline at end of file | |
| + | label: Child Fixture Page |
| + | layout: nested |
| \ No newline at end of file | |
cms_fixtures/example.com/pages/index/child/content.html b/db/cms_fixtures/example.com/pages/index/child/content.html
+0
-1
| @@ | @@ -1 +0,0 @@ |
| - | Child Page Conte dnt |
| \ No newline at end of file | |
cms_fixtures/example.com/pages/index/child/left.html b/db/cms_fixtures/example.com/pages/index/child/left.html
+1
-0
| @@ | @@ -0,0 +1 @@ |
| + | Child Page Left Fixture Content |
| \ No newline at end of file | |
cms_fixtures/example.com/pages/index/child/right.html b/db/cms_fixtures/example.com/pages/index/child/right.html
+1
-0
| @@ | @@ -0,0 +1 @@ |
| + | Child Page Right Fixture Content |
| \ No newline at end of file | |
cms_fixtures/example.com/pages/index/child/subchild/_subchild.yml b/db/cms_fixtures/example.com/pages/index/child/subchild/_subchild.yml
+0
-2
| @@ | @@ -1,2 +0,0 @@ |
| - | label: Sub-Child Page |
| - | layout: nested |
| \ No newline at end of file | |
cms_fixtures/example.com/pages/index/child/subchild/left.html b/db/cms_fixtures/example.com/pages/index/child/subchild/left.html
+0
-1
| @@ | @@ -1 +0,0 @@ |
| - | Sub Child Page Left Content |
| \ No newline at end of file | |
cms_fixtures/example.com/pages/index/child/subchild/right.html b/db/cms_fixtures/example.com/pages/index/child/subchild/right.html
+0
-1
| @@ | @@ -1 +0,0 @@ |
| - | Sub Child Page Right Content |
| \ No newline at end of file | |
cms_fixtures/example.com/pages/index/content.html b/db/cms_fixtures/example.com/pages/index/content.html
+1
-1
| @@ | @@ -1,2 +1,2 @@ |
| - | Home Page Content |
| + | Home Page Fixture Content |
| {{ cms:snippet:example }} | |
| \ No newline at end of file | |
comfortable_mexican_sofa/fixtures.rb b/lib/comfortable_mexican_sofa/fixtures.rb
+50
-3
| @@ | @@ -48,14 +48,61 @@ module ComfortableMexicanSofa::Fixtures |
| end | |
| # removing all db entries that are not in fixtures | |
| - | Cms::Layout.where('id NOT IN (?)', layout_ids).each{ |s| s.destroy } if root |
| + | Cms::Layout.where('id NOT IN (?)', layout_ids.uniq).each{ |l| l.destroy } if root |
| # returning ids of layouts in fixtures | |
| layout_ids | |
| end | |
| - | def self.sync_pages |
| - | # TODO |
| + | def self.sync_pages(site, path = nil, root = true, parent = nil, page_ids = []) |
| + | return unless path ||= find_path(site, 'pages') |
| + | |
| + | Dir.glob("#{path}/*").select{|f| File.directory?(f)}.each do |path| |
| + | slug = path.split('/').last |
| + | page = if parent |
| + | parent.children.find_by_slug(slug) || parent.children.new(:slug => slug, :site => site) |
| + | else |
| + | site.pages.find_by_slug(slug) || site.pages.new(:slug => slug) |
| + | end |
| + | |
| + | # updating attributes |
| + | if File.exists?(file_path = File.join(path, "_#{slug}.yml")) |
| + | if page.new_record? || File.mtime(file_path) > page.updated_at |
| + | attributes = YAML.load_file(file_path).symbolize_keys! |
| + | page.label = attributes[:label] || slug.titleize |
| + | page.layout = Cms::Layout.find_by_slug(attributes[:layout]) || parent.try(:layout) |
| + | end |
| + | elsif page.new_record? |
| + | page.label = slug.titleize |
| + | page.layout = Cms::Layout.find_by_slug(attributes[:layout]) || parent.try(:layout) |
| + | end |
| + | |
| + | # updating content |
| + | blocks_attributes = [ ] |
| + | Dir.glob("#{path}/*.html").each do |file_path| |
| + | if page.new_record? || File.mtime(file_path) > page.updated_at |
| + | label = file_path.split('/').last.split('.').first |
| + | blocks_attributes << { |
| + | :label => label, |
| + | :content => File.open(file_path, 'rb').read |
| + | } |
| + | end |
| + | end |
| + | |
| + | # saving |
| + | page.blocks_attributes = blocks_attributes if blocks_attributes.present? |
| + | page.save! if page.changed? |
| + | page_ids << page.id |
| + | |
| + | # checking for nested fixtures |
| + | page_ids += sync_pages(site, path, false, page, page_ids) |
| + | end |
| + | |
| + | # removing all db entries that are not in fixtures |
| + | Cms::Page.where('id NOT IN (?)', page_ids.uniq).each{ |p| p.destroy } if root |
| + | |
| + | # returning ids of layouts in fixtures |
| + | page_ids |
| end | |
| def self.sync_snippets(site) | |
test/unit/fixtures_test.rb
+33
-7
| @@ | @@ -79,15 +79,41 @@ class ViewMethodsTest < ActiveSupport::TestCase |
| end | |
| def test_sync_pages_creating | |
| - | flunk |
| - | end |
| - | |
| - | def test_sync_pages_updating |
| - | flunk |
| + | Cms::Page.delete_all |
| + | |
| + | layout = cms_layouts(:default) |
| + | layout.update_attribute(:content, '<html>{{cms:page:content}}</html>') |
| + | |
| + | nested = cms_layouts(:nested) |
| + | 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) |
| + | |
| + | assert page = Cms::Page.find_by_full_path('/') |
| + | assert_equal 'index', page.slug |
| + | assert_equal "<html>Home Page Fixture Content\n</html>", page.content |
| + | |
| + | assert child_page = Cms::Page.find_by_full_path('/child') |
| + | assert_equal page, child_page.parent |
| + | assert_equal 'child', child_page.slug |
| + | assert_equal '<html>Child Page Left Fixture Content<br/>Child Page Right Fixture Content</html>', child_page.content |
| + | end |
| end | |
| - | def test_sync_pages_deleting |
| - | flunk |
| + | def test_sync_pages_updating_and_deleting |
| + | page = cms_pages(:default) |
| + | page.update_attribute(:updated_at, 10.years.ago) |
| + | assert_equal 'Default Page', page.label |
| + | |
| + | child = cms_pages(:child) |
| + | child.update_attribute(:slug, 'old') |
| + | |
| + | assert_no_difference 'Cms::Page.count' do |
| + | ComfortableMexicanSofa::Fixtures.sync_pages(@site) |
| + | raise Cms::Page.all.to_yaml |
| + | end |
| + | |
| end | |
| def test_sync_pages_ignoring | |