starting on mirror functionality, auto-populating labels too

Oleg committed May 12, 2011
commit f3f3bbb06136fa18eee1ca63fdc3d72ec5583b61
Showing 16 changed files with 126 additions and 15 deletions
app/models/cms/layout.rb +5 -0
@@ @@ -11,6 +11,7 @@ class Cms::Layout < ActiveRecord::Base
has_many :pages, :dependent => :nullify
# -- Callbacks ------------------------------------------------------------
+ before_validation :assign_label
after_save :clear_cache, :clear_cached_page_content
after_destroy :clear_cache, :clear_cached_page_content
@@ @@ -69,6 +70,10 @@ class Cms::Layout < ActiveRecord::Base
protected
+ def assign_label
+ self.label = self.label.blank?? self.slug.try(:titleize) : self.label
+ end
+
def check_content_tag_presence
ComfortableMexicanSofa::Tag.process_content((test_page = site.pages.new), content)
if test_page.tags.select{|t| t.is_cms_block?}.blank?
app/models/cms/page.rb +6 -1
@@ @@ -19,7 +19,8 @@ class Cms::Page < ActiveRecord::Base
:autosave => true
# -- Callbacks ------------------------------------------------------------
- before_validation :assign_parent,
+ before_validation :assigns_label,
+ :assign_parent,
:assign_full_path
before_validation :assign_position,
:on => :create
@@ @@ -119,6 +120,10 @@ class Cms::Page < ActiveRecord::Base
protected
+ def assigns_label
+ self.label = self.label.blank?? self.slug.try(:titleize) : self.label
+ end
+
def assign_parent
return unless site
self.parent ||= site.pages.root unless self == site.pages.root || site.pages.count == 0
app/models/cms/site.rb +10 -2
@@ @@ -8,10 +8,12 @@ class Cms::Site < ActiveRecord::Base
has_many :snippets, :dependent => :destroy
has_many :uploads, :dependent => :destroy
+ # -- Callbacks ------------------------------------------------------------
+ before_validation :assign_label
+
# -- Validations ----------------------------------------------------------
validates :label,
- :presence => true,
- :uniqueness => true
+ :presence => true
validates :hostname,
:presence => true,
:uniqueness => true,
@@ @@ -22,4 +24,10 @@ class Cms::Site < ActiveRecord::Base
Cms::Site.all.collect{|s| ["#{s.label} (#{s.hostname})", s.id]}
end
+ protected
+
+ def assign_label
+ self.label = self.label.blank?? self.hostname : self.label
+ end
+
end
\ No newline at end of file
app/models/cms/snippet.rb +5 -0
@@ @@ -8,6 +8,7 @@ class Cms::Snippet < ActiveRecord::Base
belongs_to :site
# -- Callbacks ------------------------------------------------------------
+ before_validation :assign_label
after_save :clear_cached_page_content
after_destroy :clear_cached_page_content
@@ @@ -23,6 +24,10 @@ class Cms::Snippet < ActiveRecord::Base
protected
+ def assign_label
+ self.label = self.label.blank?? self.slug.try(:titleize) : self.label
+ end
+
# Note: This might be slow. We have no idea where the snippet is used, so
# gotta reload every single page. Kinda sucks, but might be ok unless there
# are hundreds of pages.
config/initializers/comfortable_mexican_sofa.rb +5 -0
@@ @@ -23,6 +23,11 @@ ComfortableMexicanSofa.configure do |config|
# of sites with their own layouts and pages.
# config.enable_multiple_sites = false
+ # In cases when you need sites with identical page tree structure, like different
+ # language versions. This will automatically create/destroy resources across all sites and
+ # will keep slugs/paths synced.
+ # config.enable_mirror_sites = false
+
# By default you cannot have irb code inside your layouts/pages/snippets.
# Generally this is to prevent putting something like this:
# <% User.delete_all %> but if you really want to allow it...
comfortable_mexican_sofa/configuration.rb b/lib/comfortable_mexican_sofa/configuration.rb +4 -0
@@ @@ -21,6 +21,9 @@ class ComfortableMexicanSofa::Configuration
# Are you running multiple sites from single install? Default assumption is 'No'
attr_accessor :enable_multiple_sites
+ # All resources across sites are kept in sync
+ attr_accessor :enable_mirror_sites
+
# Not allowing irb code to be run inside page content. False by default.
attr_accessor :allow_irb
@@ @@ -49,6 +52,7 @@ class ComfortableMexicanSofa::Configuration
@admin_route_redirect = 'pages'
@content_route_prefix = ''
@enable_multiple_sites = false
+ @enable_mirror_sites = false
@allow_irb = false
@enable_caching = true
@upload_file_options = {}
test/functional/cms_admin/layouts_controller_test.rb +2 -2
@@ @@ -82,12 +82,12 @@ class CmsAdmin::LayoutsControllerTest < ActionController::TestCase
def test_update_failure
layout = cms_layouts(:default)
put :update, :id => layout, :cms_layout => {
- :label => ''
+ :slug => ''
}
assert_response :success
assert_template :edit
layout.reload
- assert_not_equal '', layout.label
+ assert_not_equal '', layout.slug
assert_equal 'Failed to update layout', flash[:error]
end
test/functional/cms_admin/sites_controller_test.rb +2 -2
@@ @@ -80,12 +80,12 @@ class CmsAdmin::SitesControllerTest < ActionController::TestCase
def test_update_failure
site = cms_sites(:default)
put :update, :id => site, :cms_site => {
- :label => ''
+ :hostname => ''
}
assert_response :success
assert_template :edit
site.reload
- assert_not_equal '', site.label
+ assert_not_equal '', site.hostname
assert_equal 'Failed to update site', flash[:error]
end
test/functional/cms_admin/snippets_controller_test.rb +2 -2
@@ @@ -81,12 +81,12 @@ class CmsAdmin::SnippetsControllerTest < ActionController::TestCase
def test_update_failure
snippet = cms_snippets(:default)
put :update, :id => snippet, :cms_snippet => {
- :label => ''
+ :slug => ''
}
assert_response :success
assert_template :edit
snippet.reload
- assert_not_equal '', snippet.label
+ assert_not_equal '', snippet.slug
assert_equal 'Failed to update snippet', flash[:error]
end
test/test_helper.rb +1 -0
@@ @@ -20,6 +20,7 @@ class ActiveSupport::TestCase
config.content_route_prefix = ''
config.admin_route_redirect = 'pages'
config.enable_multiple_sites = false
+ config.enable_mirror_sites = false
config.allow_irb = false
config.enable_caching = true
config.enable_fixtures = false
test/unit/configuration_test.rb +1 -0
@@ @@ -10,6 +10,7 @@ class ConfigurationTest < ActiveSupport::TestCase
assert_equal '', config.content_route_prefix
assert_equal 'pages', config.admin_route_redirect
assert_equal false, config.enable_multiple_sites
+ assert_equal false, config.enable_mirror_sites
assert_equal false, config.allow_irb
assert_equal true, config.enable_caching
assert_equal false, config.enable_fixtures
test/unit/mirror_sites_test.rb +47 -0
@@ @@ -0,0 +1,47 @@
+ require File.expand_path('../test_helper', File.dirname(__FILE__))
+
+ class MirrorSitesTest < ActiveSupport::TestCase
+
+ def setup
+ Cms::Site.destroy_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')
+ end
+
+ def test_layout_creation
+
+ end
+
+ def test_page_creation
+
+ end
+
+ def test_snippet_creation
+
+ end
+
+ def test_layout_update
+
+ end
+
+ def test_page_update
+
+ end
+
+ def test_snippet_update
+
+ end
+
+ def test_layout_destroy
+
+ end
+
+ def test_page_destroy
+
+ end
+
+ def test_snippet_destroy
+
+ end
+
+ end
\ No newline at end of file
test/unit/models/layout_test.rb +6 -0
@@ @@ -36,6 +36,12 @@ class CmsLayoutTest < ActiveSupport::TestCase
assert layout.valid?
end
+ def test_label_assignment
+ layout = cms_sites(:default).layouts.new(:slug => 'test', :content => '{{cms:page:content}}')
+ assert layout.valid?
+ assert_equal 'Test', layout.label
+ end
+
def test_creation
assert_difference 'Cms::Layout.count' do
layout = cms_sites(:default).layouts.create(
test/unit/models/page_test.rb +14 -4
@@ @@ -44,13 +44,23 @@ class CmsPageTest < ActiveSupport::TestCase
assert_has_errors_on page, :target_page_id
end
+ def test_label_assignment
+ page = cms_sites(:default).pages.new(
+ :slug => 'test',
+ :parent => cms_pages(:default),
+ :layout => cms_layouts(:default)
+ )
+ assert page.valid?
+ assert_equal 'Test', page.label
+ end
+
def test_creation
assert_difference ['Cms::Page.count', 'Cms::Block.count'] do
page = cms_sites(:default).pages.create!(
- :label => 'test',
- :slug => 'test',
- :parent_id => cms_pages(:default).id,
- :layout_id => cms_layouts(:default).id,
+ :label => 'test',
+ :slug => 'test',
+ :parent => cms_pages(:default),
+ :layout => cms_layouts(:default),
:blocks_attributes => [
{ :label => 'default_page_text',
:content => 'test' }
test/unit/models/site_test.rb +8 -2
@@ @@ -13,14 +13,20 @@ class CmsSiteTest < ActiveSupport::TestCase
assert site.invalid?
assert_has_errors_on site, [:label, :hostname]
- site = Cms::Site.new(:label => 'My Site', :hostname => 'http://mysite.com')
+ site = Cms::Site.new(:label => 'My Site', :hostname => 'http://my-site.host')
assert site.invalid?
assert_has_errors_on site, :hostname
- site = Cms::Site.new(:label => 'My Site', :hostname => 'mysite.com')
+ site = Cms::Site.new(:label => 'My Site', :hostname => 'my-site.host')
assert site.valid?
end
+ def test_label_assignment
+ site = Cms::Site.new(:hostname => 'my-site.host')
+ assert site.valid?
+ assert_equal 'my-site.host', site.label
+ end
+
def test_cascading_destroy
assert_difference 'Cms::Site.count', -1 do
assert_difference 'Cms::Layout.count', -3 do
test/unit/models/snippet_test.rb +8 -0
@@ @@ -15,6 +15,14 @@ class CmsSnippetTest < ActiveSupport::TestCase
assert_has_errors_on snippet, [:label, :slug]
end
+ def test_label_assignment
+ snippet = cms_sites(:default).snippets.new(
+ :slug => 'test'
+ )
+ assert snippet.valid?
+ assert_equal 'Test', snippet.label
+ end
+
def test_update_forces_page_content_reload
snippet = cms_snippets(:default)
page = cms_pages(:default)