categorized indexes are done

Oleg committed Aug 25, 2011
commit 63a4c005376ace57a76abeaf146995385d4ea3e0
Showing 10 changed files with 86 additions and 9 deletions
README.md +1 -1
@@ @@ -1,4 +1,4 @@
- # ComfortableMexicanSofa (CMS Engine) [![Build Status](https://secure.travis-ci.org/twg/comfortable-mexican-sofa.png)](http://travis-ci.org/twg/comfortable-mexican-sofa)
+ # ComfortableMexicanSofa (Rails 3 CMS Engine) [![Build Status](https://secure.travis-ci.org/twg/comfortable-mexican-sofa.png)](http://travis-ci.org/twg/comfortable-mexican-sofa)
ComfortableMexicanSofa is a powerful CMS Engine for your Rails 3 applications.
app/controllers/cms_admin/files_controller.rb +1 -1
@@ @@ -6,7 +6,7 @@ class CmsAdmin::FilesController < CmsAdmin::BaseController
def index
return redirect_to :action => :new if @site.files.count == 0
- @files = @site.files.all(:order => 'label')
+ @files = @site.files.for_category(params[:category]).all(:order => 'cms_files.label')
end
def new
app/controllers/cms_admin/pages_controller.rb +5 -1
@@ @@ -8,7 +8,11 @@ class CmsAdmin::PagesController < CmsAdmin::BaseController
def index
return redirect_to :action => :new if @site.pages.count == 0
- @pages = [@site.pages.root].compact
+ if params[:category].present?
+ @pages = @site.pages.for_category(params[:category]).all(:order => 'label')
+ else
+ @pages = [@site.pages.root].compact
+ end
end
def new
app/controllers/cms_admin/snippets_controller.rb +1 -1
@@ @@ -5,7 +5,7 @@ class CmsAdmin::SnippetsController < CmsAdmin::BaseController
def index
return redirect_to :action => :new if @site.snippets.count == 0
- @snippets = @site.snippets.all(:order => 'label')
+ @snippets = @site.snippets.for_category(params[:category]).all(:order => 'label')
end
def new
app/views/cms_admin/pages/_index_branch.html.erb +5 -4
@@ @@ -3,24 +3,25 @@
has_children = page.children.present?
has_siblings = page.siblings.present?
branch_open = (session[:cms_page_tree] || []).member?(page.id.to_s) || page.root?
+ category_view = params[:category].present?
%>
<li id='<%= dom_id(page) %>'>
<div class='item'>
<div class='toggle <%= 'open' if branch_open %>'>
<%=
- if has_children && !page.root?
+ if !category_view && has_children && !page.root?
link_to span_tag(t('.toggle')), toggle_branch_cms_admin_site_page_path(@site, page), :remote => true
end
%>
</div>
<div class='icon'>
- <% if has_siblings %>
+ <% if !category_view && has_siblings %>
<div class='dragger'><span><%= t('cms.views.pages.drag') %></span></div>
<% end %>
</div>
<div class='action_links'>
- <%= link_to t('.add_page'), new_cms_admin_site_page_path(@site, :parent_id => page.id) %>
+ <%= link_to t('.add_child_page'), new_cms_admin_site_page_path(@site, :parent_id => page.id) %>
<%= link_to t('.edit'), edit_cms_admin_site_page_path(@site, page) %>
<%= link_to t('.delete'), cms_admin_site_page_path(@site, page), :method => :delete, :confirm => t('.are_you_sure') %>
</div>
@@ @@ -32,7 +33,7 @@
</div>
</div>
</div>
- <% if has_children && branch_open %>
+ <% if !category_view && has_children && branch_open %>
<ul>
<%= render :partial => 'index_branch', :collection => page.children %>
</ul>
comfortable_mexican_sofa/extensions/is_categorized.rb b/lib/comfortable_mexican_sofa/extensions/is_categorized.rb +8 -0
@@ @@ -17,6 +17,14 @@ module ComfortableMexicanSofa::IsCategorized
attr_accessor :category_ids
after_save :sync_categories
+
+ scope :for_category, lambda { |*categories|
+ if (categories = [categories].flatten.compact).present?
+ select("DISTINCT #{table_name}.*")
+ .joins(:categorizations => :category)
+ .where('cms_categories.label' => categories)
+ end
+ }
end
end
test/functional/cms_admin/files_controller_test.rb +15 -0
@@ @@ -16,6 +16,21 @@ class CmsAdmin::FilesControllerTest < ActionController::TestCase
assert_redirected_to :action => :new
end
+ def test_get_index_with_category
+ get :index, :site_id => cms_sites(:default), :category => cms_categories(:default).label
+ assert_response :success
+ assert assigns(:files)
+ assert_equal 1, assigns(:files).count
+ assert assigns(:files).first.categories.member? cms_categories(:default)
+ end
+
+ def test_get_index_with_category_invalid
+ get :index, :site_id => cms_sites(:default), :category => 'invalid'
+ assert_response :success
+ assert assigns(:files)
+ assert_equal 0, assigns(:files).count
+ end
+
def test_get_new
site = cms_sites(:default)
get :new, :site_id => site
test/functional/cms_admin/pages_controller_test.rb +19 -1
@@ @@ -15,7 +15,25 @@ class CmsAdmin::PagesControllerTest < ActionController::TestCase
assert_response :redirect
assert_redirected_to :action => :new
end
-
+
+ def test_get_index_with_category
+ category = Cms::Category.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
+ assert_response :success
+ assert assigns(:pages)
+ assert_equal 1, assigns(:pages).count
+ assert assigns(:pages).first.categories.member? category
+ end
+
+ def test_get_index_with_category_invalid
+ get :index, :site_id => cms_sites(:default), :category => 'invalid'
+ assert_response :success
+ assert assigns(:pages)
+ assert_equal 0, assigns(:pages).count
+ end
+
def test_get_new
site = cms_sites(:default)
get :new, :site_id => site
test/functional/cms_admin/snippets_controller_test.rb +18 -0
@@ @@ -15,6 +15,24 @@ class CmsAdmin::SnippetsControllerTest < ActionController::TestCase
assert_response :redirect
assert_redirected_to :action => :new
end
+
+ def test_get_index_with_category
+ category = Cms::Category.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
+ assert_response :success
+ assert assigns(:snippets)
+ assert_equal 1, assigns(:snippets).count
+ assert assigns(:snippets).first.categories.member? category
+ end
+
+ def test_get_index_with_category_invalid
+ get :index, :site_id => cms_sites(:default), :category => 'invalid'
+ assert_response :success
+ assert assigns(:snippets)
+ assert_equal 0, assigns(:snippets).count
+ end
def test_get_new
site = cms_sites(:default)
test/unit/models/categorization_test.rb +13 -0
@@ @@ -60,4 +60,17 @@ class CmsCategorizationTest < ActiveSupport::TestCase
assert_equal 0, snippet.categories.count
end
+ def test_scope_for_category
+ category = cms_categories(:default)
+ assert_equal 1, Cms::File.for_category(category.label).count
+ assert_equal 0, Cms::File.for_category('invalid').count
+ 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.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(:distinct => true)
+ end
+
end
\ No newline at end of file