Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
parent category shows contents of its child categories - same with sets
  • Loading branch information
mseppae committed Jul 14, 2009
1 parent e14ef6c commit 2846d2d
Show file tree
Hide file tree
Showing 6 changed files with 80 additions and 4 deletions.
2 changes: 1 addition & 1 deletion engines/adva_cms/app/controllers/articles_controller.rb
Expand Up @@ -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)
Expand Down
20 changes: 19 additions & 1 deletion engines/adva_cms/app/models/category.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
3 changes: 2 additions & 1 deletion engines/adva_cms/app/models/content.rb
Expand Up @@ -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!
Expand Down
34 changes: 34 additions & 0 deletions 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
2 changes: 1 addition & 1 deletion engines/adva_photos/app/controllers/albums_controller.rb
Expand Up @@ -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
Expand Down
23 changes: 23 additions & 0 deletions 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

0 comments on commit 2846d2d

Please sign in to comment.