Commit e7ce66b23b1bb0228b5940247f47ef1a306f509d

Authored by MoisesMachado
1 parent cf5a660f

ActionItem501: fixed the duplication of categorization in hierachies


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2201 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/categorization.rb
1 1 module Categorization
2 2  
3 3 def add_category_to_object(category, object)
4   - connection.execute("insert into #{table_name} (category_id, #{object_id_column}) values(#{category.id}, #{object.id})")
  4 + if !find(:first, :conditions => {object_id_column => object, :category_id => category} )
  5 + connection.execute("insert into #{table_name} (category_id, #{object_id_column}) values(#{category.id}, #{object.id})")
5 6  
6   - c = category.parent
7   - while !c.nil? && !self.find(:first, :conditions => {object_id_column => object, :category_id => c})
8   - connection.execute("insert into #{table_name} (category_id, #{object_id_column}, virtual) values(#{c.id}, #{object.id}, 1>0)")
9   - c = c.parent
  7 + c = category.parent
  8 + while !c.nil? && !self.find(:first, :conditions => {object_id_column => object, :category_id => c})
  9 + connection.execute("insert into #{table_name} (category_id, #{object_id_column}, virtual) values(#{c.id}, #{object.id}, 1>0)")
  10 + c = c.parent
  11 + end
10 12 end
11 13 end
12 14  
... ...
test/unit/article_categorization_test.rb
... ... @@ -61,4 +61,17 @@ class ArticleCategorizationTest < Test::Unit::TestCase
61 61 end
62 62 end
63 63  
  64 + should 'not duplicate when adding the parent of a category by witch the article is already categorized' do
  65 + c1 = Category.create!(:name => 'c1', :environment => Environment.default)
  66 + c2 = c1.children.create!(:name => 'c2', :environment => Environment.default)
  67 +
  68 + p = create_user('testuser').person
  69 + a = p.articles.create!(:name => 'test')
  70 +
  71 + assert_difference ArticleCategorization, :count, 2 do
  72 + ArticleCategorization.add_category_to_article(c2, a)
  73 + ArticleCategorization.add_category_to_article(c1, a)
  74 + end
  75 + end
  76 +
64 77 end
... ...