Commit b71e5cb98bde02014663ede3a635393f591975b9

Authored by MoisesMachado
1 parent 20649273

ActionItem514: refactored out the categorization functionality


git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@2196 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/article_categorization.rb
... ... @@ -3,18 +3,13 @@ class ArticleCategorization < ActiveRecord::Base
3 3 belongs_to :article
4 4 belongs_to :category
5 5  
6   - def self.add_category_to_article(category, article)
7   - connection.execute("insert into articles_categories (category_id, article_id) values(#{category.id}, #{article.id})")
  6 + extend Categorization
8 7  
9   - c = category.parent
10   - while !c.nil? && !self.find(:first, :conditions => {:article_id => article, :category_id => c})
11   - connection.execute("insert into articles_categories (category_id, article_id, virtual) values(#{c.id}, #{article.id}, 1>0)")
12   - c = c.parent
  8 + class << self
  9 + alias :add_category_to_article :add_category_to_object
  10 + def object_id_column
  11 + :article_id
13 12 end
14 13 end
15 14  
16   - def self.remove_all_for(article)
17   - self.delete_all(:article_id => article.id)
18   - end
19   -
20 15 end
... ...
app/models/categorization.rb 0 → 100644
... ... @@ -0,0 +1,17 @@
  1 +module Categorization
  2 +
  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})")
  5 +
  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
  10 + end
  11 + end
  12 +
  13 + def remove_all_for(object)
  14 + self.delete_all(object_id_column => object.id)
  15 + end
  16 +
  17 +end
... ...
app/models/profile_categorization.rb
... ... @@ -3,21 +3,15 @@ class ProfileCategorization &lt; ActiveRecord::Base
3 3 belongs_to :profile
4 4 belongs_to :category
5 5  
6   - def self.add_category_to_profile(category, profile)
7   - return if self.find_by_category_id_and_profile_id(category.id, profile.id)
8   - connection.execute("insert into categories_profiles (category_id, profile_id) values(#{category.id}, #{profile.id})")
  6 + extend Categorization
9 7  
10   - c = category.parent
11   - while !c.nil? && !self.find(:first, :conditions => {:profile_id => profile, :category_id => c})
12   - connection.execute("insert into categories_profiles (category_id, profile_id, virtual) values(#{c.id}, #{profile.id}, 1>0)")
13   - c = c.parent
  8 + class << self
  9 + alias :add_category_to_profile :add_category_to_object
  10 + def object_id_column
  11 + :profile_id
14 12 end
15 13 end
16 14  
17   - def self.remove_all_for(profile)
18   - self.delete_all(:profile_id => profile.id)
19   - end
20   -
21 15 def self.remove_region(profile)
22 16 region = profile.categories.find(:first, :conditions => "type = 'Region'")
23 17 if region
... ...