Commit a07338d6ec14158b3ce4482c3b64213e600157db

Authored by AntonioTerceiro
1 parent 26a4d4aa

ActionItem132: adding HABTM relationship between articles and categories. Declar…

…ed only in articles by now, though.



git-svn-id: https://svn.colivre.coop.br/svn/noosfero/trunk@1159 3f533792-8f58-4932-b0fe-aaf55b0a4547
app/models/article.rb
... ... @@ -9,6 +9,8 @@ class Article < ActiveRecord::Base
9 9  
10 10 has_many :comments
11 11  
  12 + has_and_belongs_to_many :categories
  13 +
12 14 acts_as_taggable
13 15 N_('Tag list')
14 16  
... ...
db/migrate/021_add_articles_categories.rb 0 → 100644
... ... @@ -0,0 +1,14 @@
  1 +class AddArticlesCategories < ActiveRecord::Migration
  2 + def self.up
  3 + create_table :articles_categories do |t|
  4 + t.column :article_id, :integer
  5 + t.column :category_id, :integer
  6 + end
  7 + add_index(:articles_categories, :article_id)
  8 + add_index(:articles_categories, :category_id)
  9 + end
  10 +
  11 + def self.down
  12 + drop_table :articles_categories
  13 + end
  14 +end
... ...
test/unit/article_test.rb
... ... @@ -171,4 +171,22 @@ class ArticleTest &lt; Test::Unit::TestCase
171 171 assert_equal(profile.url + "/myarticle", article.url)
172 172 end
173 173  
  174 + should 'associate with categories' do
  175 + env = Environment.default
  176 + c1 = env.categories.build(:name => "test category 1"); c1.save!
  177 + c2 = env.categories.build(:name => "test category 2"); c2.save!
  178 +
  179 + article = profile.articles.build(:name => 'withcategories')
  180 + article.save!
  181 +
  182 + assert_raise ActiveRecord::AssociationTypeMismatch do
  183 + article.categories << 1
  184 + end
  185 +
  186 + assert_nothing_raised do
  187 + article.categories << c1
  188 + article.categories << c2
  189 + end
  190 + end
  191 +
174 192 end
... ...