diff --git a/engines/adva_cms/app/controllers/articles_controller.rb b/engines/adva_cms/app/controllers/articles_controller.rb index 28e5b3a91..e1886d14b 100644 --- a/engines/adva_cms/app/controllers/articles_controller.rb +++ b/engines/adva_cms/app/controllers/articles_controller.rb @@ -49,7 +49,7 @@ def adjust_action def set_section; super(Section); end def set_articles - scope = @category ? @category.contents : @section.articles + scope = @category ? @category.all_contents : @section.articles scope = scope.tagged(@tags) unless @tags.blank? scope = @section.is_a?(Blog) ? scope.published(params[:year], params[:month]) : scope.published @articles = scope.paginate(:page => current_page, :limit => @section.contents_per_page) diff --git a/engines/adva_cms/app/models/category.rb b/engines/adva_cms/app/models/category.rb index 1672a7928..f32626e6f 100644 --- a/engines/adva_cms/app/models/category.rb +++ b/engines/adva_cms/app/models/category.rb @@ -7,7 +7,7 @@ class Category < ActiveRecord::Base belongs_to :section, :foreign_key => 'section_id' has_many :contents, :through => :categorizations, :source => :categorizable, :source_type => 'Content' has_many :categorizations, :dependent => :delete_all - + before_save :update_path validates_presence_of :section, :title @@ -21,8 +21,26 @@ def owner section end + def all_contents + section.class.to_s == 'Album' ? scope_by_set : scope_by_content + end + protected + def scope_by_set + scope = Photo.scoped({}) + scope = scope.scoped :include => :sets, :conditions => ["categories.lft >= ? AND categories.rgt <= ?", lft, rgt] + scope + end + + def scope_by_content + scope = Content.scoped({}) + scope = scope.scoped :include => :categories, :conditions => ["categories.lft >= ? AND categories.rgt <= ?", lft, rgt] + scope = scope.scoped :conditions => ["contents.type = ?", section.class.content_type] + scope = scope.scoped :include => :section, :conditions => ["sections.type = ?", section.class.to_s] + scope + end + def update_path if permalink_changed? new_path = build_path diff --git a/engines/adva_cms/app/models/content.rb b/engines/adva_cms/app/models/content.rb index a3ddcb3f3..7c170f93f 100644 --- a/engines/adva_cms/app/models/content.rb +++ b/engines/adva_cms/app/models/content.rb @@ -27,7 +27,8 @@ class Version < ActiveRecord::Base before_validation :set_site - default_scope :order => 'position, published_at' + # more explicit to make nested category contents to work + default_scope :order => 'contents.position, contents.published_at' named_scope :published, Proc.new { |*args| options = args.extract_options! diff --git a/engines/adva_cms/test/unit/models/nested_categories_test.rb b/engines/adva_cms/test/unit/models/nested_categories_test.rb new file mode 100644 index 000000000..85f105bec --- /dev/null +++ b/engines/adva_cms/test/unit/models/nested_categories_test.rb @@ -0,0 +1,34 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../test_helper') + +class NestedCategoriesTest < ActiveSupport::TestCase + def setup + super + @page = Page.first + @category = Category.create!(:title => 'fi', :section => @page) + @child_category = Category.create!(:title => 'joensuu', :section => @page) + @child_category.move_to_child_of(@category) + + @article_1 = @page.articles.build(:title => 'finland', :body => 'polar bears & penguins', :author => User.first).save! + @article_2 = @page.articles.build(:title => 'joensuu', :body => 'north karelian capital', :author => User.first).save! + @article_1 = Article.find_by_title('finland') + @article_2 = Article.find_by_title('joensuu') + + @article_1.categories << @category + @article_2.categories << @child_category + end + + test 'all_contents returns a scope of all the contents of category and its descendants' do + assert_equal [@article_1, @article_2], @category.all_contents + assert_equal [@article_2], @child_category.all_contents + end + + test 'all_contents returns categories of a certain section only' do + # There is a 'a category' named category for a blog, a page and for a wiki, + # we do not want it to return wiki or blog category contents + category = Category.find_by_title('a category') + category.all_contents.each do |content| + assert content.is_a?(Article) + assert content.section.is_a?(Page) + end + end +end \ No newline at end of file diff --git a/engines/adva_photos/app/controllers/albums_controller.rb b/engines/adva_photos/app/controllers/albums_controller.rb index f1fcf816d..b34237f2d 100644 --- a/engines/adva_photos/app/controllers/albums_controller.rb +++ b/engines/adva_photos/app/controllers/albums_controller.rb @@ -32,7 +32,7 @@ def show def set_section; super(Album); end def set_photos - scope = @set ? @set.photos : @section.photos + scope = @set ? @set.all_contents : @section.photos scope = scope.tagged(@tags) unless @tags.blank? limit = request.format == :html ? @section.photos_per_page : 15 @photos = scope.published.paginate :page => current_page, :limit => limit diff --git a/engines/adva_photos/test/unit/models/nested_sets_test.rb b/engines/adva_photos/test/unit/models/nested_sets_test.rb new file mode 100644 index 000000000..2d082473f --- /dev/null +++ b/engines/adva_photos/test/unit/models/nested_sets_test.rb @@ -0,0 +1,23 @@ +require File.expand_path(File.dirname(__FILE__) + '/../../test_helper') + +class NestedSetsTest < ActiveSupport::TestCase + def setup + super + @set = Category.find_by_title('Summer') + @sub_set = Category.find_by_title('A Subset') + @sub_set_photo = Photo.create!(:title => 'a subset photo', + :data_content_type => 'image/jpeg', + :data_file_name => 'test.png', + :author => User.first, + :section => @sub_set.section, + :published_at => Time.parse('2008-01-01 12:00:00') ) + + @sub_set_photo.sets << @sub_set + @photos = @set.photos << @sub_set_photo + end + + test 'all_contents returns a scope of all the photos of set and its descendants' do + assert_equal @photos, @set.all_contents + assert_equal [@sub_set_photo], @sub_set.all_contents + end +end \ No newline at end of file