category models are in

Oleg committed Aug 22, 2011
commit 582cbd1f11d7e75571c91f3492acd710394bf923
Showing 13 changed files with 156 additions and 11 deletions
app/models/cms/categorization.rb +17 -0
@@ @@ -0,0 +1,17 @@
+ class Cms::Categorization < ActiveRecord::Base
+
+ set_table_name :cms_categorizations
+
+ # -- Relationships --------------------------------------------------------
+ belongs_to :category
+ belongs_to :categorized,
+ :polymorphic => true
+
+ # -- Validations ----------------------------------------------------------
+ validates :categorized_type, :categorized_id,
+ :presence => true
+ validates :category_id,
+ :presence => true,
+ :uniqueness => { :scope => [:categorized_type, :categorized_id] }
+
+ end
app/models/cms/category.rb +21 -0
@@ @@ -0,0 +1,21 @@
+ class Cms::Category < ActiveRecord::Base
+
+ set_table_name :cms_categories
+
+ # -- Relationships --------------------------------------------------------
+ has_many :categorizations,
+ :dependent => :destroy
+
+ # -- Validations ----------------------------------------------------------
+ validates :label,
+ :presence => true,
+ :uniqueness => true
+ validates :categorized_type,
+ :presence => true
+
+ # -- Scopes ---------------------------------------------------------------
+ scope :of_type, lambda { |type|
+ where(:categorized_type => type)
+ }
+
+ end
\ No newline at end of file
app/models/cms/file.rb +1 -1
@@ @@ -1,7 +1,7 @@
class Cms::File < ActiveRecord::Base
set_table_name :cms_files
-
+
# -- AR Extensions --------------------------------------------------------
has_attached_file :file, ComfortableMexicanSofa.config.upload_file_options
app/views/layouts/cms_admin/_head.html.erb +2 -2
@@ @@ -21,9 +21,9 @@
'comfortable_mexican_sofa/typography',
'comfortable_mexican_sofa/form',
'comfortable_mexican_sofa/content',
- 'comfortable_mexican_sofa/widgets',
'comfortable_mexican_sofa/codemirror',
- 'comfortable_mexican_sofa/jquery_ui' %>
+ 'comfortable_mexican_sofa/jquery_ui',
+ 'comfortable_mexican_sofa/widgets' %>
<%= javascript_include_tag 'comfortable_mexican_sofa/jquery',
'comfortable_mexican_sofa/jquery_ui',
'comfortable_mexican_sofa/rails',
config/database.yml +1 -7
@@ @@ -13,10 +13,4 @@ test:
adapter: sqlite3
database: db/test.sqlite3
pool: 5
- timeout: 10000
-
- production:
- adapter: sqlite3
- database: db/production.sqlite3
- pool: 5
- timeout: 10000
+ timeout: 10000
\ No newline at end of file
migrate/01_create_cms.rb b/db/migrate/01_create_cms.rb +16 -0
@@ @@ -90,6 +90,20 @@ class CreateCms < ActiveRecord::Migration
end
add_index :cms_revisions, [:record_type, :record_id, :created_at]
+ # -- Categories ---------------------------------------------------------
+ create_table :cms_categories, :force => true do |t|
+ t.string :label
+ t.string :categorized_type
+ end
+ add_index :cms_categories, [: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
+ 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'
end
def self.down
@@ @@ -100,5 +114,7 @@ class CreateCms < ActiveRecord::Migration
drop_table :cms_blocks
drop_table :cms_files
drop_table :cms_revisions
+ drop_table :cms_categories
+ drop_table :cms_categorizations
end
end
migrate/upgrades/05_upgrade_to_1_4_0.rb b/db/migrate/upgrades/05_upgrade_to_1_4_0.rb +18 -0
@@ @@ -4,6 +4,21 @@ class UpgradeTo130 < ActiveRecord::Migration
add_column :cms_files, :label, :string
add_column :cms_files, :description, :string, :limit => 2048
add_index :cms_files, [:site_id, :label]
+
+ # -- Categories ---------------------------------------------------------
+ create_table :cms_categories, :force => true do |t|
+ t.string :label
+ t.string :categorized_type
+ end
+ add_index :cms_categories, [: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
+ 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'
end
def self.down
@@ @@ -11,5 +26,8 @@ class UpgradeTo130 < ActiveRecord::Migration
remove_column :cms_files, :description
remove_column :cms_files, :label
rename_table :cms_files, :cms_uploads
+
+ drop_table :cms_categories
+ drop_table :cms_categorizations
end
end
\ No newline at end of file
test/fixtures/cms/categories.yml +3 -0
@@ @@ -0,0 +1,3 @@
+ default:
+ label: Default
+ categorized_type: Cms::File
\ No newline at end of file
test/fixtures/cms/categorizations.yml +3 -0
@@ @@ -0,0 +1,3 @@
+ default:
+ category: default
+ categorized: default (Cms::File)
\ No newline at end of file
test/unit/models/block_test.rb +1 -1
@@ @@ -1,6 +1,6 @@
require File.expand_path('../../test_helper', File.dirname(__FILE__))
- class BlockTest < ActiveSupport::TestCase
+ class CmsBlockTest < ActiveSupport::TestCase
def test_fixtures_validity
Cms::Block.all.each do |block|
test/unit/models/categorization_test.rb +25 -0
@@ @@ -0,0 +1,25 @@
+ require File.expand_path('../../test_helper', File.dirname(__FILE__))
+
+ class CmsCategorizationTest < ActiveSupport::TestCase
+
+ def test_fixtures_validity
+ Cms::Categorization.all.each do |categorization|
+ assert categorization.valid?, categorization.errors.full_messages.to_s
+ end
+ end
+
+ def test_validation
+ category = Cms::Categorization.new
+ assert category.invalid?
+ assert_has_errors_on category, [:categorized_type, :categorized_id]
+ end
+
+ def test_creation
+ assert_difference 'Cms::Categorization.count' do
+ categorization = cms_categories(:default).categorizations.create!(
+ :categorized => cms_snippets(:default)
+ )
+ end
+ end
+
+ end
\ No newline at end of file
test/unit/models/category_test.rb +39 -0
@@ @@ -0,0 +1,39 @@
+ require File.expand_path('../../test_helper', File.dirname(__FILE__))
+
+ class CmsCategoryTest < ActiveSupport::TestCase
+
+ def test_fixtures_validity
+ Cms::Category.all.each do |category|
+ assert category.valid?, category.errors.full_messages.to_s
+ end
+ end
+
+ def test_validation
+ category = Cms::Category.new
+ assert category.invalid?
+ assert_has_errors_on category, [:label, :categorized_type]
+ end
+
+ def test_creation
+ assert_difference 'Cms::Category.count' do
+ Cms::Category.create(
+ :label => 'Test Category',
+ :categorized_type => 'Cms::Snippet'
+ )
+ end
+ end
+
+ def test_destruction
+ category = cms_categories(:default)
+ assert_equal 1, category.categorizations.count
+ assert_difference ['Cms::Category.count', 'Cms::Categorization.count'], -1 do
+ category.destroy
+ end
+ end
+
+ def test_scope_of_type
+ assert_equal 1, Cms::Category.of_type('Cms::File').count
+ assert_equal 0, Cms::Category.of_type('Invalid').count
+ end
+
+ end
\ No newline at end of file
test/unit/models/site_test.rb +9 -0
@@ @@ -51,6 +51,15 @@ class CmsSiteTest < ActiveSupport::TestCase
assert_equal '', site.path
end
+ def test_creation
+ assert_difference 'Cms::Site.count' do
+ Cms::Site.create!(
+ :label => 'Test Site',
+ :hostname => 'test.test'
+ )
+ end
+ end
+
def test_cascading_destroy
assert_difference 'Cms::Site.count', -1 do
assert_difference 'Cms::Layout.count', -3 do