more wip
Oleg
committed May 17, 2011
commit 33d508fbf87edffe53f6b7dccff52f6672571380
Showing 5
changed files with
96 additions
and 19 deletions
Gemfile
+0
-1
| @@ | @@ -11,5 +11,4 @@ end |
| group :test do | |
| gem 'sqlite3' | |
| gem 'jeweler', '>=1.4.0' | |
| - | gem 'turn' |
| end | |
app/models/cms/page.rb
+1
-1
| @@ | @@ -3,7 +3,7 @@ class Cms::Page < ActiveRecord::Base |
| set_table_name :cms_pages | |
| acts_as_tree :counter_cache => :children_count | |
| - | # is_mirrored |
| + | is_mirrored |
| has_revisions_for :blocks_attributes | |
| attr_accessor :tags, | |
comfortable_mexican_sofa/is_mirrored.rb b/lib/comfortable_mexican_sofa/is_mirrored.rb
+39
-10
| @@ | @@ -7,28 +7,56 @@ module ComfortableMexicanSofa::IsMirrored |
| module ClassMethods | |
| def is_mirrored | |
| - | include ComfortableMexicanSofa::IsMirrored::InstanceMethods |
| - | |
| - | attr_accessor :is_mirrored |
| - | |
| - | after_save :sync_mirror |
| - | after_destroy :destroy_mirror |
| + | if ComfortableMexicanSofa.config.enable_mirror_sites |
| + | include ComfortableMexicanSofa::IsMirrored::InstanceMethods |
| + | |
| + | attr_accessor :is_mirrored |
| + | |
| + | after_save :sync_mirror |
| + | after_destroy :destroy_mirror |
| + | end |
| end | |
| end | |
| module InstanceMethods | |
| + | # Mirrors of the object found on other sites |
| + | def mirrors |
| + | (Cms::Site.all - [self.site]).collect do |site| |
| + | case self |
| + | when Cms::Layout then site.layouts.find_by_slug(self.slug) |
| + | when Cms::Page then site.pages.find_by_full_path(self.full_path) |
| + | when Cms::Snippet then site.snippets.find_by_slug(self.slug) |
| + | end |
| + | end |
| + | end |
| + | |
| + | # Creating or updating a mirror object. Relationships are mirrored |
| + | # but content is unique. When updating need to grab mirrors based on |
| + | # self.slug_was, new objects will use self.slug. |
| def sync_mirror | |
| return if self.is_mirrored | |
| - | Cms::Site.all.each do |site| |
| + | (Cms::Site.all - [self.site]).each do |site| |
| mirror = case self | |
| when Cms::Layout | |
| - | site.layouts.find_by_slug(self.slug) || site.layouts.new(:slug => self.slug) |
| + | m = site.layouts.find_by_slug(self.slug_was || self.slug) || site.layouts.new |
| + | m.attributes = { |
| + | :slug => self.slug, |
| + | :parent => site.layouts.find_by_slug(self.parent.try(:slug)) |
| + | } |
| + | m |
| when Cms::Page | |
| - | |
| + | m = site.pages.find_by_full_path(self.full_path_was || self.full_path) || site.pages.new |
| + | m.attributes = { |
| + | :slug => self.slug, |
| + | :parent => site.pages.find_by_full_path(self.parent.try(:full_path)), |
| + | :layout => site.layouts.find_by_slug(self.layout.slug) |
| + | } |
| + | m |
| when Cms::Snippet | |
| - | site.snippets.find_by_slug(self.slug) || site.snippets.new(:slug => self.slug) |
| + | puts self.slug_change.to_yaml |
| + | site.snippets.find_by_slug(self.slug_was || self.slug) || site.snippets.new(:slug => self.slug) |
| end | |
| mirror.is_mirrored = true | |
| @@ | @@ -36,6 +64,7 @@ module ComfortableMexicanSofa::IsMirrored |
| end | |
| end | |
| + | # Mirrors should be destroyed |
| def destroy_mirror | |
| return if self.is_mirrored | |
tasks/comfortable_mexican_sofa.rake b/lib/tasks/comfortable_mexican_sofa.rake
+1
-1
| @@ | @@ -6,7 +6,7 @@ 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: SITE=example.com)' |
| task :import => :environment do |task, args| | |
| site = if ComfortableMexicanSofa.config.enable_multiple_sites | |
| Cms::Site.find_by_hostname(args[:site]) | |
test/unit/mirror_sites_test.rb
+55
-6
| @@ | @@ -4,6 +4,9 @@ class MirrorSitesTest < ActiveSupport::TestCase |
| def setup | |
| ComfortableMexicanSofa.config.enable_mirror_sites = true | |
| + | load(File.expand_path('app/models/cms/layout.rb', Rails.root)) |
| + | load(File.expand_path('app/models/cms/page.rb', Rails.root)) |
| + | load(File.expand_path('app/models/cms/snippet.rb', Rails.root)) |
| Cms::Site.delete_all | |
| @site_a = Cms::Site.create!(:label => 'Site A', :hostname => 'site-a.host') | |
| @site_b = Cms::Site.create!(:label => 'Site B', :hostname => 'site-b.host') | |
| @@ | @@ -11,31 +14,77 @@ class MirrorSitesTest < ActiveSupport::TestCase |
| def test_layout_creation | |
| assert_difference 'Cms::Layout.count', 2 do | |
| - | @site_a.layouts.create!(:slug => 'default') |
| - | @site_b.reload |
| - | assert layout = @site_b.layouts.first |
| - | assert_equal 'default', layout.slug |
| + | layout = @site_a.layouts.create!(:slug => 'test') |
| + | assert_equal 1, layout.mirrors.size |
| + | assert_equal 'test', layout.mirrors.first.slug |
| end | |
| end | |
| def test_page_creation | |
| + | layout = @site_a.layouts.create!(:slug => 'test') |
| + | assert_difference 'Cms::Page.count', 2 do |
| + | page = @site_a.pages.create!( |
| + | :layout => layout, |
| + | :slug => 'test' |
| + | ) |
| + | assert_equal 1, page.mirrors.size |
| + | assert_equal 'test', page.mirrors.first.slug |
| + | end |
| end | |
| def test_snippet_creation | |
| - | |
| + | assert_difference 'Cms::Snippet.count', 2 do |
| + | snippet = @site_a.snippets.create(:slug => 'test') |
| + | assert_equal 1, snippet.mirrors.size |
| + | assert_equal 'test', snippet.mirrors.first.slug |
| + | end |
| end | |
| def test_layout_update | |
| + | layout_1a = @site_a.layouts.create!(:slug => 'test_a') |
| + | layout_1b = @site_a.layouts.create!(:slug => 'test_b') |
| + | layout_1c = @site_a.layouts.create!(:slug => 'nested', :parent => layout_1a) |
| + | |
| + | layout_2a = layout_1a.mirrors.first |
| + | layout_2b = layout_1b.mirrors.first |
| + | layout_2c = layout_1c.mirrors.first |
| + | assert_equal layout_2a, layout_2c.parent |
| + | layout_1c.update_attributes!( |
| + | :parent => layout_1b, |
| + | :content => 'updated content' |
| + | ) |
| + | layout_2c.reload |
| + | assert_equal layout_2b, layout_2c.parent |
| + | assert_not_equal 'updated content', layout_2c |
| end | |
| def test_page_update | |
| + | layout_1a = @site_a.layouts.create!(:slug => 'test_a') |
| + | layout_1b = @site_a.layouts.create!(:slug => 'test_b') |
| + | |
| + | page_1r = @site_a.pages.create!(:slug => 'root', :layout => layout_1a) |
| + | page_1a = @site_a.pages.create!(:slug => 'test_a', :layout => layout_1a) |
| + | page_1b = @site_a.pages.create!(:slug => 'test_b', :layout => layout_1a) |
| + | assert_equal page_1r, page_1b.parent |
| + | |
| + | page_1b.update_attributes!( |
| + | |
| + | ) |
| end | |
| def test_snippet_update | |
| - | |
| + | snippet_1 = @site_a.snippets.create(:slug => 'test') |
| + | snippet_2 = snippet_1.mirrors.first |
| + | snippet_1.update_attributes!( |
| + | :slug => 'updated', |
| + | :content => 'updated content' |
| + | ) |
| + | snippet_2.reload |
| + | assert_equal 'updated', snippet_2.slug |
| + | assert_not_equal 'updated content', snippet_2.content |
| end | |
| def test_layout_destroy | |