categories are scoped on sites
Oleg
committed Dec 09, 2011
commit ccaa375c38091df7331b1574c7267fdea0ac3f9f
Showing 13
changed files with
31 additions
and 21 deletions
app/controllers/cms_admin/categories_controller.rb
+2
-2
| @@ | @@ -7,7 +7,7 @@ class CmsAdmin::CategoriesController < CmsAdmin::BaseController |
| end | |
| def create | |
| - | @category = Cms::Category.create!(params[:category]) |
| + | @category = @site.categories.create!(params[:category]) |
| rescue ActiveRecord::RecordInvalid | |
| logger.detailed_error($!) | |
| render :nothing => true | |
| @@ | @@ -27,7 +27,7 @@ class CmsAdmin::CategoriesController < CmsAdmin::BaseController |
| protected | |
| def load_category | |
| - | @category = Cms::Category.find(params[:id]) |
| + | @category = @site.categories.find(params[:id]) |
| rescue ActiveRecord::RecordNotFound | |
| render :nothing => true | |
| end | |
app/models/cms/category.rb
+3
-0
| @@ | @@ -5,10 +5,13 @@ class Cms::Category < ActiveRecord::Base |
| set_table_name :cms_categories | |
| # -- Relationships -------------------------------------------------------- | |
| + | belongs_to :site |
| has_many :categorizations, | |
| :dependent => :destroy | |
| # -- Validations ---------------------------------------------------------- | |
| + | validates :site_id, |
| + | :presence => true |
| validates :label, | |
| :presence => true, | |
| :uniqueness => { :scope => :categorized_type } | |
app/models/cms/site.rb
+5
-4
| @@ | @@ -5,10 +5,11 @@ class Cms::Site < ActiveRecord::Base |
| set_table_name :cms_sites | |
| # -- Relationships -------------------------------------------------------- | |
| - | has_many :layouts, :dependent => :destroy |
| - | has_many :pages, :dependent => :destroy |
| - | has_many :snippets, :dependent => :destroy |
| - | has_many :files, :dependent => :destroy |
| + | has_many :layouts, :dependent => :destroy |
| + | has_many :pages, :dependent => :destroy |
| + | has_many :snippets, :dependent => :destroy |
| + | has_many :files, :dependent => :destroy |
| + | has_many :categories, :dependent => :destroy |
| # -- Callbacks ------------------------------------------------------------ | |
| before_validation :assign_label | |
app/views/cms_admin/categories/_form.html.erb
+1
-1
| @@ | @@ -1,5 +1,5 @@ |
| <% object = form.object %> | |
| - | <% if (categories = Cms::Category.of_type(object.class.to_s)).present? %> |
| + | <% if (categories = @site.categories.of_type(object.class.to_s)).present? %> |
| <%= form.simple_field t('.label'), nil, :class => 'categories' do %> | |
| <% categories.each do |category| %> | |
| <%= hidden_field_tag "#{object.class.to_s.demodulize.downcase}[category_ids][#{category.id}]", 0, :id => nil %> | |
app/views/cms_admin/categories/_index.html.erb
+2
-2
| @@ | @@ -11,13 +11,13 @@ |
| <div class='category all <%= (params[:category].blank?? 'active' : nil ) %>'> | |
| <%= link_to t('.all'), :category => nil %> | |
| </div> | |
| - | <% Cms::Category.of_type(type).each do |category| %> |
| + | <% @site.categories.of_type(type).each do |category| %> |
| <%= render :partial => 'cms_admin/categories/show', :object => category, :locals => { :read => true } %> | |
| <% end %> | |
| </div> | |
| <div class='categories editable' style='display:none'> | |
| - | <% Cms::Category.of_type(type).each do |category| %> |
| + | <% @site.categories.of_type(type).each do |category| %> |
| <%= render :partial => 'cms_admin/categories/show', :object => category %> | |
| <% end %> | |
| <div class='category new'> | |
migrate/01_create_cms.rb b/db/migrate/01_create_cms.rb
+6
-5
| @@ | @@ -31,13 +31,13 @@ class CreateCms < ActiveRecord::Migration |
| # -- Pages -------------------------------------------------------------- | |
| create_table :cms_pages do |t| | |
| - | t.integer :site_id |
| + | t.integer :site_id, :null => false |
| t.integer :layout_id | |
| t.integer :parent_id | |
| t.integer :target_page_id | |
| t.string :label | |
| t.string :slug | |
| - | t.string :full_path |
| + | t.string :full_path, :null => false |
| t.text :content | |
| t.integer :position, :null => false, :default => 0 | |
| t.integer :children_count, :null => false, :default => 0 | |
| @@ | @@ -98,10 +98,11 @@ class CreateCms < ActiveRecord::Migration |
| # -- Categories --------------------------------------------------------- | |
| create_table :cms_categories, :force => true do |t| | |
| - | t.string :label |
| - | t.string :categorized_type |
| + | t.string :site_id, :null => false |
| + | t.string :label, :null => false |
| + | t.string :categorized_type, :null => false |
| end | |
| - | add_index :cms_categories, [:categorized_type, :label], :unique => true |
| + | add_index :cms_categories, [:site_id, :categorized_type, :label], :unique => true |
| create_table :cms_categorizations, :force => true do |t| | |
| t.integer :category_id | |
comfortable_mexican_sofa/extensions/is_categorized.rb b/lib/comfortable_mexican_sofa/extensions/is_categorized.rb
+3
-1
| @@ | @@ -22,7 +22,9 @@ module ComfortableMexicanSofa::IsCategorized |
| scope :for_category, lambda { |*categories| | |
| if (categories = [categories].flatten.compact).present? | |
| - | select("DISTINCT #{table_name}.*").joins(:categorizations => :category).where('cms_categories.label' => categories) |
| + | select("DISTINCT #{table_name}.*"). |
| + | joins(:categorizations => :category). |
| + | where('cms_categories.label' => categories) |
| end | |
| } | |
| end | |
test/fixtures/cms/categories.yml
+1
-0
| @@ | @@ -1,3 +1,4 @@ |
| default: | |
| + | site: default |
| label: Default | |
| categorized_type: Cms::File | |
| \ No newline at end of file | |
test/functional/cms_admin/pages_controller_test.rb
+1
-1
| @@ | @@ -17,7 +17,7 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase |
| end | |
| def test_get_index_with_category | |
| - | category = Cms::Category.create!(:label => 'Test Category', :categorized_type => 'Cms::Page') |
| + | category = cms_sites(:default).categories.create!(:label => 'Test Category', :categorized_type => 'Cms::Page') |
| category.categorizations.create!(:categorized => cms_pages(:child)) | |
| get :index, :site_id => cms_sites(:default), :category => category.label | |
test/functional/cms_admin/snippets_controller_test.rb
+1
-1
| @@ | @@ -17,7 +17,7 @@ class CmsAdmin::SnippetsControllerTest < ActionController::TestCase |
| end | |
| def test_get_index_with_category | |
| - | category = Cms::Category.create!(:label => 'Test Category', :categorized_type => 'Cms::Snippet') |
| + | category = cms_sites(:default).categories.create!(:label => 'Test Category', :categorized_type => 'Cms::Snippet') |
| category.categorizations.create!(:categorized => cms_snippets(:default)) | |
| get :index, :site_id => cms_sites(:default), :category => category.label | |
test/unit/models/categorization_test.rb
+1
-1
| @@ | @@ -67,7 +67,7 @@ class CmsCategorizationTest < ActiveSupport::TestCase |
| assert_equal 1, Cms::File.for_category(category.label, 'invalid').count | |
| assert_equal 1, Cms::File.for_category(nil).count | |
| - | new_category = Cms::Category.create!(:label => 'Test Category', :categorized_type => 'Cms::File') |
| + | new_category = cms_sites(:default).categories.create!(:label => 'Test Category', :categorized_type => 'Cms::File') |
| new_category.categorizations.create!(:categorized => cms_files(:default)) | |
| assert_equal 1, Cms::File.for_category(category.label, new_category.label).all.size | |
| assert_equal 1, Cms::File.for_category(category.label, new_category.label).count('cms_files.id', :distinct => true) | |
test/unit/models/category_test.rb
+2
-2
| @@ | @@ -11,12 +11,12 @@ class CmsCategoryTest < ActiveSupport::TestCase |
| def test_validation | |
| category = Cms::Category.new | |
| assert category.invalid? | |
| - | assert_has_errors_on category, [:label, :categorized_type] |
| + | assert_has_errors_on category, [:site_id, :label, :categorized_type] |
| end | |
| def test_creation | |
| assert_difference 'Cms::Category.count' do | |
| - | Cms::Category.create( |
| + | cms_sites(:default).categories.create( |
| :label => 'Test Category', | |
| :categorized_type => 'Cms::Snippet' | |
| ) | |
test/unit/models/site_test.rb
+3
-1
| @@ | @@ -65,7 +65,9 @@ class CmsSiteTest < ActiveSupport::TestCase |
| assert_difference 'Cms::Layout.count', -3 do | |
| assert_difference 'Cms::Page.count', -2 do | |
| assert_difference 'Cms::Snippet.count', -1 do | |
| - | cms_sites(:default).destroy |
| + | assert_difference 'Cms::Category.count', -1 do |
| + | cms_sites(:default).destroy |
| + | end |
| end | |
| end | |
| end | |