Commit 1924d8b622d5c7925e2736e8b7b3c832abb90f49

Authored by Marcos Pereira
1 parent 27f11bf7

API - Comments within articles

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,18 +196,6 @@ module Noosfero @@ -183,18 +196,6 @@ module Noosfero
183 expose :children, :using => ArticleBase 196 expose :children, :using => ArticleBase
184 end 197 end
185 198
186 - class CommentBase < Entity  
187 - expose :body, :title, :id  
188 - expose :created_at, :format_with => :timestamp  
189 - expose :author, :using => Profile  
190 - expose :reply_of, :using => CommentBase  
191 - end  
192 -  
193 - class Comment < CommentBase  
194 - root 'comments', 'comment'  
195 - expose :children, as: :replies, :using => Comment  
196 - end  
197 -  
198 class User < Entity 199 class User < Entity
199 root 'users', 'user' 200 root 'users', 'user'
200 201
lib/noosfero/api/helpers.rb
@@ -119,7 +119,7 @@ require_relative &#39;../../find_by_contents&#39; @@ -119,7 +119,7 @@ require_relative &#39;../../find_by_contents&#39;
119 119
120 def present_article(asset) 120 def present_article(asset)
121 article = find_article(asset.articles, params[:id]) 121 article = find_article(asset.articles, params[:id])
122 - present_partial article, :with => Entities::Article 122 + present_partial article, :with => Entities::Article, :params => params
123 end 123 end
124 124
125 def present_articles_for_asset(asset, method = 'articles') 125 def present_articles_for_asset(asset, method = 'articles')
@@ -128,7 +128,7 @@ require_relative &#39;../../find_by_contents&#39; @@ -128,7 +128,7 @@ require_relative &#39;../../find_by_contents&#39;
128 end 128 end
129 129
130 def present_articles(articles) 130 def present_articles(articles)
131 - present_partial paginate(articles), :with => Entities::Article 131 + present_partial paginate(articles), :with => Entities::Article, :params => params
132 end 132 end
133 133
134 def find_articles(asset, method = 'articles') 134 def find_articles(asset, method = 'articles')
test/api/articles_test.rb
@@ -664,4 +664,20 @@ class ArticlesTest &lt; ActiveSupport::TestCase @@ -664,4 +664,20 @@ class ArticlesTest &lt; 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