Commit a07338d6ec14158b3ce4482c3b64213e600157db
1 parent
26a4d4aa
Exists in
master
and in
29 other branches
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
Showing
3 changed files
with
34 additions
and
0 deletions
Show diff stats
app/models/article.rb
... | ... | @@ -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 < 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 | ... | ... |