adding Site#identifier, upgrade migration file

Oleg committed Dec 09, 2011
commit f93595333cf15f498a04b02fcfdbd8bc2232a3aa
Showing 13 changed files with 92 additions and 52 deletions
app/models/cms/site.rb +5 -1
@@ @@ -16,6 +16,10 @@ class Cms::Site < ActiveRecord::Base
before_save :clean_path
# -- Validations ----------------------------------------------------------
+ validates :identifier,
+ :presence => true,
+ :uniqueness => true,
+ :format => { :with => /^\w[a-z0-9_-]*$/i }
validates :label,
:presence => true
validates :hostname,
@@ @@ -45,7 +49,7 @@ class Cms::Site < ActiveRecord::Base
protected
def assign_label
- self.label = self.label.blank?? self.hostname : self.label
+ self.label = self.label.blank?? self.identifier.try(:titleize) : self.label
end
def clean_path
app/views/cms_admin/sites/_form.html.erb +2 -1
@@ @@ -1,4 +1,5 @@
- <%= form.text_field :label %>
+ <%= form.text_field :label, :id => (@site.new_record?? 'slugify' : nil) %>
+ <%= form.text_field :identifier, :id => 'slug' %>
<%= form.text_field :hostname %>
<%= form.text_field :path %>
<%= form.select :locale, ComfortableMexicanSofa.config.locales.to_a.collect{|l| [l[1], l[0]]} %>
migrate/01_create_cms.rb b/db/migrate/01_create_cms.rb +16 -15
@@ @@ -3,8 +3,9 @@ class CreateCms < ActiveRecord::Migration
def self.up
# -- Sites --------------------------------------------------------------
create_table :cms_sites do |t|
- t.string :label
- t.string :hostname
+ t.string :label, :null => false
+ t.string :identifier, :null => false
+ t.string :hostname, :null => false
t.string :path
t.string :locale, :null => false, :default => 'en'
t.boolean :is_mirrored, :null => false, :default => false
@@ @@ -35,7 +36,7 @@ class CreateCms < ActiveRecord::Migration
t.integer :layout_id
t.integer :parent_id
t.integer :target_page_id
- t.string :label
+ t.string :label, :null => false
t.string :slug
t.string :full_path, :null => false
t.text :content
@@ @@ -72,14 +73,14 @@ class CreateCms < ActiveRecord::Migration
# -- Files --------------------------------------------------------------
create_table :cms_files do |t|
- t.integer :site_id
+ t.integer :site_id, :null => false
t.integer :block_id
- t.string :label
- t.string :file_file_name
- t.string :file_content_type
- t.integer :file_file_size
- t.string :description, :limit => 2048
- t.integer :position, :null => false, :default => 0
+ t.string :label, :null => false
+ t.string :file_file_name, :null => false
+ t.string :file_content_type, :null => false
+ t.integer :file_file_size, :null => false
+ t.string :description, :limit => 2048
+ t.integer :position, :null => false, :default => 0
t.timestamps
end
add_index :cms_files, [:site_id, :label]
@@ @@ -89,8 +90,8 @@ class CreateCms < ActiveRecord::Migration
# -- Revisions -----------------------------------------------------------
create_table :cms_revisions, :force => true do |t|
- t.string :record_type
- t.integer :record_id
+ t.string :record_type, :null => false
+ t.integer :record_id, :null => false
t.text :data
t.datetime :created_at
end
@@ @@ -105,9 +106,9 @@ class CreateCms < ActiveRecord::Migration
add_index :cms_categories, [:site_id, :categorized_type, :label], :unique => true
create_table :cms_categorizations, :force => true do |t|
- t.integer :category_id
- t.string :categorized_type
- t.integer :categorized_id
+ t.integer :category_id, :null => false
+ t.string :categorized_type, :null => false
+ t.integer :categorized_id, :null => false
end
add_index :cms_categorizations, [:category_id, :categorized_type, :categorized_id], :unique => true,
:name => 'index_cms_categorizations_on_cat_id_and_catd_type_and_catd_id'
migrate/upgrades/07_upgrade_to_1_6_0.rb b/db/migrate/upgrades/07_upgrade_to_1_6_0.rb +25 -0
@@ @@ -0,0 +1,25 @@
+ class UpgradeTo150 < ActiveRecord::Migration
+ def self.up
+ add_column :cms_sites, :identifier, :string, :null => false
+ add_index :cms_sites, :identifier
+
+ rename_column :cms_layouts, :slug, :identifier
+ rename_column :cms_blocks, :label, :identifier
+ rename_column :cms_snippets, :slug, :identifier
+
+ add_column :cms_categories, :site_id, :integer, :null => :false
+ add_index :cms_categories, [:site_id, :categorized_type, :label], :unique => true
+ end
+
+ def self.down
+ remove_index :cms_categories, [:site_id, :categorized_type, :label], :unique => true
+ remove_column :cms_categories, :site_id
+
+ rename_column :cms_snippets, :identifier, :slug
+ rename_column :cms_blocks, :identifier, :label
+ rename_column :cms_layouts, :identifier, :slug
+
+ remove_index :cms_sites, :identifier
+ remove_column :cms_sites, :identifier
+ end
+ end
\ No newline at end of file
comfortable_mexican_sofa/error.rb b/lib/comfortable_mexican_sofa/error.rb +2 -2
@@ @@ -4,8 +4,8 @@ module ComfortableMexicanSofa
end
class MissingSite < ComfortableMexicanSofa::Error
- def initialize(slug)
- super "Cannot find CMS Site with slug: #{slug}"
+ def initialize(identifier)
+ super "Cannot find CMS Site with identifier: #{identifier}"
end
end
comfortable_mexican_sofa/render_methods.rb b/lib/comfortable_mexican_sofa/render_methods.rb +3 -3
@@ @@ -37,9 +37,9 @@ module ComfortableMexicanSofa::RenderMethods
def render(options = {}, locals = {}, &block)
# TODO: add slug to Cms::Site as well
- if options.is_a?(Hash) && site_slug = options.delete(:cms_site)
- unless @cms_site = Cms::Site.find_by_label(site_slug)
- raise ComfortableMexicanSofa::MissingSite.new(site_slug)
+ if options.is_a?(Hash) && identifier = options.delete(:cms_site)
+ unless @cms_site = Cms::Site.find_by_identifier(identifier)
+ raise ComfortableMexicanSofa::MissingSite.new(identifier)
end
end
test/fixtures/cms/sites.yml +1 -0
@@ @@ -1,5 +1,6 @@
default:
label: Default Site
+ identifier: default-site
hostname: test.host
path:
is_mirrored: false
\ No newline at end of file
test/functional/cms_admin/sites_controller_test.rb +3 -2
@@ @@ -44,8 +44,9 @@ class CmsAdmin::SitesControllerTest < ActionController::TestCase
def test_create
assert_difference 'Cms::Site.count' do
post :create, :site => {
- :label => 'Test Site',
- :hostname => 'test.site.local'
+ :label => 'Test Site',
+ :identifier => 'test-site',
+ :hostname => 'test.site.local'
}
assert_response :redirect
site = Cms::Site.last
test/integration/mirrors_test.rb +1 -1
@@ @@ -5,7 +5,7 @@ class MirrorsTest < ActionDispatch::IntegrationTest
def setup
@site_a = cms_sites(:default)
@site_a.update_attribute(:is_mirrored, true)
- @site_b = Cms::Site.create!(:hostname => 'test-b.host', :is_mirrored => true)
+ @site_b = Cms::Site.create!(:identifier => 'test_b', :hostname => 'test-b.host', :is_mirrored => true)
# making mirrors
Cms::Layout.all.each{ |l| l.save! }
Cms::Page.all.each{ |p| p.save! }
test/integration/render_cms_test.rb +6 -6
@@ @@ -21,8 +21,8 @@ class RenderCmsTest < ActionDispatch::IntegrationTest
def create_site_b
site = Cms::Site.create!(
- :label => 'SiteB',
- :hostname => 'site-b.test')
+ :identifier => 'site-b',
+ :hostname => 'site-b.test')
layout = site.layouts.create!(
:identifier => 'default',
:content => 'site-b {{cms:page:content}}')
@@ @@ -57,7 +57,7 @@ class RenderCmsTest < ActionDispatch::IntegrationTest
when 'page_explicit_with_status'
render :cms_page => '/test-page', :status => 404
when 'page_explicit_with_site'
- render :cms_page => '/', :cms_site => 'SiteB'
+ render :cms_page => '/', :cms_site => 'site-b'
else
raise 'Invalid or no param[:type] provided'
end
@@ @@ -79,7 +79,7 @@ class RenderCmsTest < ActionDispatch::IntegrationTest
when 'layout_invalid'
render :cms_layout => 'invalid'
when 'layout_defaults_with_site'
- render :cms_layout => 'default', :cms_site => 'SiteB'
+ render :cms_layout => 'default', :cms_site => 'site-b'
else
raise 'Invalid or no param[:type] provided'
end
@@ @@ -155,7 +155,7 @@ class RenderCmsTest < ActionDispatch::IntegrationTest
get '/render-page?type=page_explicit_with_site'
assert_response :success
assert assigns(:cms_site)
- assert_equal 'SiteB', assigns(:cms_site).label
+ assert_equal 'site-b', assigns(:cms_site).identifier
assert_equal 'site-b SiteBContent', response.body
end
@@ @@ -205,7 +205,7 @@ class RenderCmsTest < ActionDispatch::IntegrationTest
get '/render-layout?type=layout_defaults_with_site'
assert_response :success
assert assigns(:cms_site)
- assert_equal 'SiteB', assigns(:cms_site).label
+ assert_equal 'site-b', assigns(:cms_site).identifier
assert_equal 'site-b TestTemplate TestValue', response.body
end
test/integration/sites_test.rb +3 -3
@@ @@ -28,9 +28,9 @@ class SitesTest < ActionDispatch::IntegrationTest
def test_get_public_page_with_sites_with_different_paths
Cms::Site.delete_all
- site_a = Cms::Site.create!(:label => 'Site A', :hostname => 'test.host', :path => '')
- site_b = Cms::Site.create!(:label => 'Site B', :hostname => 'test.host', :path => 'path-b')
- site_c = Cms::Site.create!(:label => 'Site C', :hostname => 'test.host', :path => 'path-c/child')
+ site_a = Cms::Site.create!(:identifier => 'site-a', :hostname => 'test.host', :path => '')
+ site_b = Cms::Site.create!(:identifier => 'site-b', :hostname => 'test.host', :path => 'path-b')
+ site_c = Cms::Site.create!(:identifier => 'site-c', :hostname => 'test.host', :path => 'path-c/child')
%w(/ /path-a /path-a/child /path-c).each do |path|
get path
test/unit/mirrors_test.rb +2 -2
@@ @@ -4,8 +4,8 @@ class MirrorsTest < ActiveSupport::TestCase
def setup
Cms::Site.delete_all
- @site_a = Cms::Site.create!(:label => 'Site A', :hostname => 'site-a.host', :is_mirrored => true)
- @site_b = Cms::Site.create!(:label => 'Site B', :hostname => 'site-b.host', :is_mirrored => true)
+ @site_a = Cms::Site.create!(:identifier => 'site_a', :hostname => 'site-a.host', :is_mirrored => true)
+ @site_b = Cms::Site.create!(:identifier => 'site_b', :hostname => 'site-b.host', :is_mirrored => true)
end
def test_layout_creation
test/unit/models/site_test.rb +23 -16
@@ @@ -11,51 +11,58 @@ class CmsSiteTest < ActiveSupport::TestCase
def test_validation
site = Cms::Site.new
assert site.invalid?
- assert_has_errors_on site, [:label, :hostname]
+ assert_has_errors_on site, [:identifier, :label, :hostname]
- site = Cms::Site.new(:label => 'My Site', :hostname => 'http://my-site.host')
+ site = Cms::Site.new(:identifier => 'test', :hostname => 'http://site.host')
assert site.invalid?
assert_has_errors_on site, :hostname
- site = Cms::Site.new(:label => 'My Site', :hostname => 'my-site.host')
- assert site.valid?
+ site = Cms::Site.new(:identifier => cms_sites(:default).identifier, :hostname => 'site.host')
+ assert site.invalid?
+ assert_has_errors_on site, :identifier
+
+ site = Cms::Site.new(:identifier => 'test', :hostname => 'site.host')
+ assert site.valid?, site.errors.to_yaml
end
def test_validation_path_uniqueness
s1 = cms_sites(:default)
s2 = Cms::Site.new(
- :hostname => s1.hostname,
- :path => s1.path
+ :identifier => 'test',
+ :hostname => s1.hostname,
+ :path => s1.path
)
assert s2.invalid?
assert_has_errors_on s2, :hostname
s2 = Cms::Site.new(
- :hostname => s1.hostname,
- :path => '/en'
+ :identifier => 'test',
+ :hostname => s1.hostname,
+ :path => '/en'
)
assert s2.valid?
end
def test_label_assignment
- site = Cms::Site.new(:hostname => 'my-site.host')
+ site = Cms::Site.new(:identifier => 'test', :hostname => 'my-site.host')
assert site.valid?
- assert_equal 'my-site.host', site.label
+ assert_equal 'Test', site.label
end
def test_clean_path
- site = Cms::Site.create!(:hostname => 'test.host', :path => '/en///test//')
+ site = Cms::Site.create!(:identifier => 'test_a', :hostname => 'test.host', :path => '/en///test//')
assert_equal '/en/test', site.path
- site = Cms::Site.create!(:hostname => 'my-site.host', :path => '/')
+ site = Cms::Site.create!(:identifier => 'test_b', :hostname => 'my-site.host', :path => '/')
assert_equal '', site.path
end
def test_creation
assert_difference 'Cms::Site.count' do
Cms::Site.create!(
- :label => 'Test Site',
- :hostname => 'test.test'
+ :identifier => 'test',
+ :label => 'Test Site',
+ :hostname => 'test.test'
)
end
end
@@ @@ -91,8 +98,8 @@ class CmsSiteTest < ActiveSupport::TestCase
assert_equal site_a, Cms::Site.find_site('test.host', '/some/path')
assert_equal site_a, Cms::Site.find_site('test99.host', '/some/path')
- site_b = Cms::Site.create!(:hostname => 'test2.host', :path => 'en')
- site_c = Cms::Site.create!(:hostname => 'test2.host', :path => 'fr')
+ site_b = Cms::Site.create!(:identifier => 'test_a', :hostname => 'test2.host', :path => 'en')
+ site_c = Cms::Site.create!(:identifier => 'test_b', :hostname => 'test2.host', :path => 'fr')
assert_equal site_a, Cms::Site.find_site('test.host')
assert_equal site_a, Cms::Site.find_site('test.host', '/some/path')