Commit fa90a41d1e3ada3e18cf2622a37690f1143ea0af

Authored by Joenio Costa
2 parents 62abbce2 1924d8b6

Merge branch 'api_article_comments' into 'master'

API - Comments within articles

Adds comments field to articles API entity when the http request includes show_comments parameter.

See merge request !858
lib/noosfero/api/entities.rb
... ... @@ -150,6 +150,18 @@ module Noosfero
150 150 expose :members, :using => Person
151 151 end
152 152  
  153 + class CommentBase < Entity
  154 + expose :body, :title, :id
  155 + expose :created_at, :format_with => :timestamp
  156 + expose :author, :using => Profile
  157 + expose :reply_of, :using => CommentBase
  158 + end
  159 +
  160 + class Comment < CommentBase
  161 + root 'comments', 'comment'
  162 + expose :children, as: :replies, :using => Comment
  163 + end
  164 +
153 165 class ArticleBase < Entity
154 166 root 'articles', 'article'
155 167 expose :id
... ... @@ -177,6 +189,7 @@ module Noosfero
177 189 expose :votes_count
178 190 expose :comments_count
179 191 expose :type
  192 + expose :comments, using: CommentBase, :if => lambda{|obj,opt| opt[:params] && ['1','true',true].include?(opt[:params][:show_comments])}
180 193 end
181 194  
182 195 class Article < ArticleBase
... ... @@ -185,18 +198,6 @@ module Noosfero
185 198 expose :children, :using => ArticleBase
186 199 end
187 200  
188   - class CommentBase < Entity
189   - expose :body, :title, :id
190   - expose :created_at, :format_with => :timestamp
191   - expose :author, :using => Profile
192   - expose :reply_of, :using => CommentBase
193   - end
194   -
195   - class Comment < CommentBase
196   - root 'comments', 'comment'
197   - expose :children, as: :replies, :using => Comment
198   - end
199   -
200 201 class User < Entity
201 202 root 'users', 'user'
202 203  
... ...
lib/noosfero/api/helpers.rb
... ... @@ -124,7 +124,7 @@ require_relative &#39;../../find_by_contents&#39;
124 124  
125 125 def present_article(asset)
126 126 article = find_article(asset.articles, params[:id])
127   - present_partial article, :with => Entities::Article
  127 + present_partial article, :with => Entities::Article, :params => params
128 128 end
129 129  
130 130 def present_articles_for_asset(asset, method = 'articles')
... ... @@ -133,7 +133,7 @@ require_relative &#39;../../find_by_contents&#39;
133 133 end
134 134  
135 135 def present_articles(articles)
136   - present_partial paginate(articles), :with => Entities::Article
  136 + present_partial paginate(articles), :with => Entities::Article, :params => params
137 137 end
138 138  
139 139 def find_articles(asset, method = 'articles')
... ...
test/api/articles_test.rb
... ... @@ -664,4 +664,20 @@ class ArticlesTest &lt; ActiveSupport::TestCase
664 664 assert_not_nil json['article'][attribute]
665 665 end
666 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 683 end
... ...