Commit e43a8ebde5fbdc85cd249717f961c858e5500c19

Authored by Marcos Pereira
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
lib/noosfero/api/entities.rb
... ... @@ -148,6 +148,18 @@ module Noosfero
148 148 expose :members, :using => Person
149 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 163 class ArticleBase < Entity
152 164 root 'articles', 'article'
153 165 expose :id
... ... @@ -175,6 +187,7 @@ module Noosfero
175 187 expose :votes_count
176 188 expose :comments_count
177 189 expose :type
  190 + expose :comments, using: CommentBase, :if => lambda{|obj,opt| opt[:params] && ['1','true',true].include?(opt[:params][:show_comments])}
178 191 end
179 192  
180 193 class Article < ArticleBase
... ... @@ -183,13 +196,6 @@ module Noosfero
183 196 expose :children, :using => ArticleBase
184 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 199 class User < Entity
194 200 root 'users', 'user'
195 201  
... ...
lib/noosfero/api/helpers.rb
... ... @@ -120,7 +120,7 @@ require_relative &#39;../../find_by_contents&#39;
120 120  
121 121 def present_article(asset)
122 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 124 end
125 125  
126 126 def present_articles_for_asset(asset, method = 'articles')
... ... @@ -129,7 +129,7 @@ require_relative &#39;../../find_by_contents&#39;
129 129 end
130 130  
131 131 def present_articles(articles)
132   - present_partial paginate(articles), :with => Entities::Article
  132 + present_partial paginate(articles), :with => Entities::Article, :params => params
133 133 end
134 134  
135 135 def find_articles(asset, method = 'articles')
... ...
test/unit/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
... ...