drying up database config code
Oleg
committed Sep 01, 2011
commit 5c7e273cc6b426d26ae69955f0346040458f4fa7
Showing 16
changed files with
29 additions
and 60 deletions
README.md
+1
-1
| @@ | @@ -1,4 +1,4 @@ |
| - | # ComfortableMexicanSofa (Rails 3 CMS Engine) [](http://travis-ci.org/twg/comfortable-mexican-sofa) |
| + | # ComfortableMexicanSofa (Rails 3 CMS Engine) [](http://travis-ci.org/twg/comfortable-mexican-sofa) |
| ComfortableMexicanSofa is a powerful CMS Engine for your Rails 3 applications. | |
app/models/cms/block.rb
+1
-3
| @@ | @@ -1,8 +1,6 @@ |
| class Cms::Block < ActiveRecord::Base | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| set_table_name :cms_blocks | |
app/models/cms/categorization.rb
+1
-3
| @@ | @@ -1,8 +1,6 @@ |
| class Cms::Categorization < ActiveRecord::Base | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| set_table_name :cms_categorizations | |
app/models/cms/category.rb
+1
-3
| @@ | @@ -1,8 +1,6 @@ |
| class Cms::Category < ActiveRecord::Base | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| set_table_name :cms_categories | |
app/models/cms/file.rb
+1
-3
| @@ | @@ -1,8 +1,6 @@ |
| class Cms::File < ActiveRecord::Base | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| set_table_name :cms_files | |
app/models/cms/layout.rb
+1
-3
| @@ | @@ -1,8 +1,6 @@ |
| class Cms::Layout < ActiveRecord::Base | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| set_table_name :cms_layouts | |
app/models/cms/page.rb
+1
-3
| @@ | @@ -1,8 +1,6 @@ |
| class Cms::Page < ActiveRecord::Base | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| set_table_name :cms_pages | |
app/models/cms/revision.rb
+1
-3
| @@ | @@ -1,8 +1,6 @@ |
| class Cms::Revision < ActiveRecord::Base | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| set_table_name :cms_revisions | |
app/models/cms/site.rb
+1
-3
| @@ | @@ -1,8 +1,6 @@ |
| class Cms::Site < ActiveRecord::Base | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| set_table_name :cms_sites | |
app/models/cms/snippet.rb
+1
-3
| @@ | @@ -1,8 +1,6 @@ |
| class Cms::Snippet < ActiveRecord::Base | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| set_table_name :cms_snippets | |
migrate/01_create_cms.rb b/db/migrate/01_create_cms.rb
+3
-7
| @@ | @@ -2,9 +2,7 @@ class CreateCms < ActiveRecord::Migration |
| def self.up | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| # -- Sites -------------------------------------------------------------- | |
| create_table :cms_sites do |t| | |
| @@ | @@ -104,7 +102,7 @@ class CreateCms < ActiveRecord::Migration |
| create_table :cms_categorizations, :force => true do |t| | |
| t.integer :category_id | |
| - | t.string :categorized_type |
| + | t.string :categorized_type |
| t.integer :categorized_id | |
| end | |
| add_index :cms_categorizations, [:category_id, :categorized_type, :categorized_id], :unique => true, | |
| @@ | @@ -113,9 +111,7 @@ class CreateCms < ActiveRecord::Migration |
| def self.down | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| drop_table :cms_sites | |
| drop_table :cms_layouts | |
migrate/upgrades/02_upgrade_to_1_1_0.rb b/db/migrate/upgrades/02_upgrade_to_1_1_0.rb
+2
-6
| @@ | @@ -1,8 +1,6 @@ |
| class UpgradeTo110 < ActiveRecord::Migration | |
| def self.up | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| rename_column :cms_layouts, :cms_site_id, :site_id | |
| rename_column :cms_pages, :cms_site_id, :site_id | |
| rename_column :cms_pages, :cms_layout_id, :layout_id | |
| @@ | @@ -12,9 +10,7 @@ class UpgradeTo110 < ActiveRecord::Migration |
| end | |
| def self.down | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| rename_column :cms_uploads, :site_id, :cms_site_id | |
| rename_column :cms_snippets, :site_id, :cms_site_id | |
| rename_column :cms_blocks, :page_id, :cms_page_id | |
migrate/upgrades/03_upgrade_to_1_2_0.rb b/db/migrate/upgrades/03_upgrade_to_1_2_0.rb
+2
-6
| @@ | @@ -1,8 +1,6 @@ |
| class UpgradeTo120 < ActiveRecord::Migration | |
| def self.up | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| create_table :cms_revisions, :force => true do |t| | |
| t.string :record_type | |
| t.integer :record_id | |
| @@ | @@ -13,9 +11,7 @@ class UpgradeTo120 < ActiveRecord::Migration |
| end | |
| def self.down | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| drop_table :cms_revisions | |
| end | |
| end | |
| \ No newline at end of file | |
migrate/upgrades/04_upgrade_to_1_3_0.rb b/db/migrate/upgrades/04_upgrade_to_1_3_0.rb
+2
-6
| @@ | @@ -1,8 +1,6 @@ |
| class UpgradeTo130 < ActiveRecord::Migration | |
| def self.up | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| add_column :cms_sites, :is_mirrored, :boolean, :null => false, :default => false | |
| add_column :cms_sites, :path, :string | |
| add_column :cms_sites, :locale, :string, :null => false, :default => 'en' | |
| @@ | @@ -14,9 +12,7 @@ class UpgradeTo130 < ActiveRecord::Migration |
| end | |
| def self.down | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| remove_index :cms_sites, :is_mirrored | |
| remove_column :cms_sites, :path | |
| remove_column :cms_sites, :is_mirrored | |
migrate/upgrades/05_upgrade_to_1_4_0.rb b/db/migrate/upgrades/05_upgrade_to_1_4_0.rb
+3
-7
| @@ | @@ -1,8 +1,6 @@ |
| class UpgradeTo140 < ActiveRecord::Migration | |
| def self.up | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| rename_table :cms_uploads, :cms_files | |
| add_column :cms_files, :label, :string | |
| add_column :cms_files, :description, :string, :limit => 2048 | |
| @@ | @@ -17,7 +15,7 @@ class UpgradeTo140 < ActiveRecord::Migration |
| create_table :cms_categorizations, :force => true do |t| | |
| t.integer :category_id | |
| - | t.string :categorized_type |
| + | t.string :categorized_type |
| t.integer :categorized_id | |
| end | |
| add_index :cms_categorizations, [:category_id, :categorized_type, :categorized_id], :unique => true, | |
| @@ | @@ -25,9 +23,7 @@ class UpgradeTo140 < ActiveRecord::Migration |
| end | |
| def self.down | |
| - | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| - | establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| - | end |
| + | ComfortableMexicanSofa.establish_connection(self) |
| remove_index :cms_files, [:site_id, :label] | |
| remove_column :cms_files, :description | |
| remove_column :cms_files, :label | |
comfortable_mexican_sofa.rb b/lib/comfortable_mexican_sofa.rb
+7
-0
| @@ | @@ -50,5 +50,12 @@ module ComfortableMexicanSofa |
| Rails::VERSION::MAJOR == 3 && Rails::VERSION::MINOR >= 1 && Rails.configuration.assets.enabled | |
| end | |
| + | # Establishing database connection if custom one is defined |
| + | def establish_connection(klass) |
| + | if ComfortableMexicanSofa.config.database_config && !Rails.env.test? |
| + | klass.establish_connection "#{ComfortableMexicanSofa.config.database_config}_#{Rails.env}" |
| + | end |
| + | end |
| + | |
| end | |
| end | |
| \ No newline at end of file | |