Commit e43a8ebde5fbdc85cd249717f961c858e5500c19
Committed by
Tallys Martins
1 parent
3624f9df
Exists in
stable-spb-1.5
API - Comments within articles
(cherry picked from commit 1924d8b622d5c7925e2736e8b7b3c832abb90f49) Conflicts: lib/noosfero/api/entities.rb
Showing
3 changed files
with
31 additions
and
9 deletions
Show diff stats
lib/noosfero/api/entities.rb
| @@ -148,6 +148,18 @@ module Noosfero | @@ -148,6 +148,18 @@ module Noosfero | ||
| 148 | expose :members, :using => Person | 148 | expose :members, :using => Person |
| 149 | end | 149 | end |
| 150 | 150 | ||
| 151 | + class CommentBase < Entity | ||
| 152 | + expose :body, :title, :id | ||
| 153 | + expose :created_at, :format_with => :timestamp | ||
| 154 | + expose :author, :using => Profile | ||
| 155 | + expose :reply_of, :using => CommentBase | ||
| 156 | + end | ||
| 157 | + | ||
| 158 | + class Comment < CommentBase | ||
| 159 | + root 'comments', 'comment' | ||
| 160 | + expose :children, as: :replies, :using => Comment | ||
| 161 | + end | ||
| 162 | + | ||
| 151 | class ArticleBase < Entity | 163 | class ArticleBase < Entity |
| 152 | root 'articles', 'article' | 164 | root 'articles', 'article' |
| 153 | expose :id | 165 | expose :id |
| @@ -175,6 +187,7 @@ module Noosfero | @@ -175,6 +187,7 @@ module Noosfero | ||
| 175 | expose :votes_count | 187 | expose :votes_count |
| 176 | expose :comments_count | 188 | expose :comments_count |
| 177 | expose :type | 189 | expose :type |
| 190 | + expose :comments, using: CommentBase, :if => lambda{|obj,opt| opt[:params] && ['1','true',true].include?(opt[:params][:show_comments])} | ||
| 178 | end | 191 | end |
| 179 | 192 | ||
| 180 | class Article < ArticleBase | 193 | class Article < ArticleBase |
| @@ -183,13 +196,6 @@ module Noosfero | @@ -183,13 +196,6 @@ module Noosfero | ||
| 183 | expose :children, :using => ArticleBase | 196 | expose :children, :using => ArticleBase |
| 184 | end | 197 | end |
| 185 | 198 | ||
| 186 | - class Comment < Entity | ||
| 187 | - root 'comments', 'comment' | ||
| 188 | - expose :body, :title, :id | ||
| 189 | - expose :created_at, :format_with => :timestamp | ||
| 190 | - expose :author, :using => Profile | ||
| 191 | - end | ||
| 192 | - | ||
| 193 | class User < Entity | 199 | class User < Entity |
| 194 | root 'users', 'user' | 200 | root 'users', 'user' |
| 195 | 201 |
lib/noosfero/api/helpers.rb
| @@ -120,7 +120,7 @@ require_relative '../../find_by_contents' | @@ -120,7 +120,7 @@ require_relative '../../find_by_contents' | ||
| 120 | 120 | ||
| 121 | def present_article(asset) | 121 | def present_article(asset) |
| 122 | article = find_article(asset.articles, params[:id]) | 122 | article = find_article(asset.articles, params[:id]) |
| 123 | - present_partial article, :with => Entities::Article | 123 | + present_partial article, :with => Entities::Article, :params => params |
| 124 | end | 124 | end |
| 125 | 125 | ||
| 126 | def present_articles_for_asset(asset, method = 'articles') | 126 | def present_articles_for_asset(asset, method = 'articles') |
| @@ -129,7 +129,7 @@ require_relative '../../find_by_contents' | @@ -129,7 +129,7 @@ require_relative '../../find_by_contents' | ||
| 129 | end | 129 | end |
| 130 | 130 | ||
| 131 | def present_articles(articles) | 131 | def present_articles(articles) |
| 132 | - present_partial paginate(articles), :with => Entities::Article | 132 | + present_partial paginate(articles), :with => Entities::Article, :params => params |
| 133 | end | 133 | end |
| 134 | 134 | ||
| 135 | def find_articles(asset, method = 'articles') | 135 | def find_articles(asset, method = 'articles') |
test/unit/api/articles_test.rb
| @@ -664,4 +664,20 @@ class ArticlesTest < ActiveSupport::TestCase | @@ -664,4 +664,20 @@ class ArticlesTest < ActiveSupport::TestCase | ||
| 664 | assert_not_nil json['article'][attribute] | 664 | assert_not_nil json['article'][attribute] |
| 665 | end | 665 | end |
| 666 | end | 666 | end |
| 667 | + | ||
| 668 | + should 'only show article comments when show_comments is present' do | ||
| 669 | + person = fast_create(Person) | ||
| 670 | + article = fast_create(Article, :profile_id => person.id, :name => "Some thing") | ||
| 671 | + article.comments.create!(:body => "another comment", :author => person) | ||
| 672 | + | ||
| 673 | + get "/api/v1/articles/#{article.id}/?#{params.merge(:show_comments => '1').to_query}" | ||
| 674 | + json = JSON.parse(last_response.body) | ||
| 675 | + assert_includes json["article"].keys, "comments" | ||
| 676 | + assert_equal json["article"]["comments"].first["body"], "another comment" | ||
| 677 | + | ||
| 678 | + get "/api/v1/articles/#{article.id}/?#{params.to_query}" | ||
| 679 | + json = JSON.parse(last_response.body) | ||
| 680 | + assert_not_includes json["article"].keys, "comments" | ||
| 681 | + end | ||
| 682 | + | ||
| 667 | end | 683 | end |