Permalink
Browse files

Touch Tag when destroy a Article

  • Loading branch information...
1 parent 6615a21 commit 8abce4b076da694ab7cc7075201a732fd713f399 @reyesyang committed Jan 4, 2014
Showing with 16 additions and 4 deletions.
  1. +1 −1 app/models/article.rb
  2. +1 −1 app/models/tagging.rb
  3. +1 −1 spec/factories/articles.rb
  4. +13 −1 spec/models/article_spec.rb
View
@@ -1,6 +1,6 @@
# -*- encoding : utf-8 -*-
class Article < ActiveRecord::Base
- has_many :taggings
+ has_many :taggings, dependent: :destroy
has_many :tags, through: :taggings
validates :title, :content, :presence => true
View
@@ -1,5 +1,5 @@
# encoding: UTF-8
class Tagging < ActiveRecord::Base
belongs_to :article
- belongs_to :tag, counter_cache: :articles_count
+ belongs_to :tag, counter_cache: :articles_count, touch: true
end
@@ -4,7 +4,7 @@
factory :article do
sequence(:title) { |n| "artile title #{n}" }
content { 'a' * 20 }
- tag_list 'tag1,tag2'
+ tag_list 'tag1'
factory :draft_article do
tag_list "draft"
@@ -11,10 +11,22 @@
describe "assosiations" do
it { should have_many :taggings }
it { should have_many :tags }
+
+ it "touch related tag when destroy a article", f: true do
+ article = create :article
+ tag = article.tags.first.reload
+ tag_articles_count = tag.articles_count
+ tag_updated_at = tag.updated_at
+
+ sleep 1
+ article.destroy
+ expect(tag.reload.articles_count).to eq (tag_articles_count - 1)
+ expect(tag.updated_at).to be > tag_updated_at
+ end
end
describe '#tag_list' do
- subject!(:article) { create :article }
+ subject!(:article) { create :article, tag_list: 'tag1,tag2' }
context "assign to default value 'tag1,tag2'" do
it "Tag's count is 2" do

0 comments on commit 8abce4b

Please sign in to comment.