Commit c1b783c7bd7dfe040570a86d255cd5cda7e4a0b1
1 parent
d0472735
Exists in
theme-brasil-digital-from-staging
and in
9 other branches
Add counter cache for article followers
Showing
4 changed files
with
35 additions
and
12 deletions
Show diff stats
app/models/article_follower.rb
db/migrate/20150909091347_add_followers_count_to_article.rb
0 → 100644
| ... | ... | @@ -0,0 +1,15 @@ |
| 1 | +class AddFollowersCountToArticle < ActiveRecord::Migration | |
| 2 | + | |
| 3 | + def self.up | |
| 4 | + add_column :articles, :followers_count, :integer, :default => 0 | |
| 5 | + add_column :article_versions, :followers_count, :integer | |
| 6 | + | |
| 7 | + execute "update articles set followers_count = (select count(*) from article_followers where article_followers.article_id = articles.id)" | |
| 8 | + end | |
| 9 | + | |
| 10 | + def self.down | |
| 11 | + remove_column :article_versions, :followers_count | |
| 12 | + remove_column :articles, :followers_count | |
| 13 | + end | |
| 14 | + | |
| 15 | +end | ... | ... |
test/fixtures/article_followers.yml
test/unit/article_test.rb
| ... | ... | @@ -2167,4 +2167,21 @@ class ArticleTest < ActiveSupport::TestCase |
| 2167 | 2167 | article.destroy |
| 2168 | 2168 | end |
| 2169 | 2169 | |
| 2170 | + should "increment followers count when a person follow an article" do | |
| 2171 | + a = fast_create(Article) | |
| 2172 | + p = fast_create(Person) | |
| 2173 | + assert_difference "a.reload.followers_count" do | |
| 2174 | + a.person_followers << p | |
| 2175 | + end | |
| 2176 | + end | |
| 2177 | + | |
| 2178 | + should "decrement followers count when a person unfollow an article" do | |
| 2179 | + p = fast_create(Person) | |
| 2180 | + a = fast_create(Article, :profile_id => p) | |
| 2181 | + a.person_followers << p | |
| 2182 | + assert_difference "a.reload.followers_count", -1 do | |
| 2183 | + a.person_followers.destroy_all | |
| 2184 | + end | |
| 2185 | + end | |
| 2186 | + | |
| 2170 | 2187 | end | ... | ... |