Commit 9edda6b526b123f4be7b9ca807f502fccc88de24
Exists in
staging
and in
4 other branches
Merge branch 'staging' into production
Showing
5 changed files
with
52 additions
and
2 deletions
Show diff stats
app/models/article_follower.rb
1 | 1 | class ArticleFollower < ActiveRecord::Base |
2 | + | |
3 | + extend CacheCounterHelper | |
4 | + | |
2 | 5 | attr_accessible :article_id, :person_id, :since |
3 | - belongs_to :article | |
6 | + belongs_to :article, :counter_cache => :followers_count | |
4 | 7 | belongs_to :person |
8 | + | |
9 | + after_create do |article_follower| | |
10 | + ArticleFollower.update_cache_counter(:followers_count, article_follower.article, 1) | |
11 | + end | |
12 | + | |
13 | + after_destroy do |article_follower| | |
14 | + ArticleFollower.update_cache_counter(:followers_count, article_follower.article, -1) | |
15 | + end | |
5 | 16 | end | ... | ... |
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 | ... | ... |
lib/noosfero/api/entities.rb
... | ... | @@ -99,6 +99,8 @@ module Noosfero |
99 | 99 | expose :end_date, :documentation => {type: 'DateTime', desc: 'The date of finish of the article'} |
100 | 100 | expose :tag_list |
101 | 101 | expose :children_count |
102 | + expose :followers_count | |
103 | + expose :slug, :documentation => {:type => "String", :desc => "Trimmed and parsed name of a article"} | |
102 | 104 | end |
103 | 105 | |
104 | 106 | class Article < ArticleBase |
... | ... | @@ -107,7 +109,6 @@ module Noosfero |
107 | 109 | expose :children, using: ArticleBase do |article, options| |
108 | 110 | article.children.limit(Noosfero::API::V1::Articles::MAX_PER_PAGE) |
109 | 111 | end |
110 | - expose :slug, :documentation => {:type => "String", :desc => "Trimmed and parsed name of a article"} | |
111 | 112 | end |
112 | 113 | |
113 | 114 | class Comment < Entity | ... | ... |
test/unit/api/articles_test.rb
... | ... | @@ -100,6 +100,18 @@ class ArticlesTest < ActiveSupport::TestCase |
100 | 100 | assert_equal true, json['success'] |
101 | 101 | end |
102 | 102 | |
103 | + should 'return the followers count of an article' do | |
104 | + article = fast_create(Article, :profile_id => @person.id, :name => "Some thing") | |
105 | + | |
106 | + article.person_followers << @person | |
107 | + | |
108 | + get "/api/v1/articles/#{article.id}" | |
109 | + json = JSON.parse(last_response.body) | |
110 | + | |
111 | + assert_equal 200, last_response.status | |
112 | + assert_equal 1, json['article']['followers_count'] | |
113 | + end | |
114 | + | |
103 | 115 | should 'return the followers of a article identified by id' do |
104 | 116 | article = fast_create(Article, :profile_id => @person.id, :name => "Some thing") |
105 | 117 | ... | ... |