From 1924d8b622d5c7925e2736e8b7b3c832abb90f49 Mon Sep 17 00:00:00 2001 From: Marcos Ronaldo Date: Mon, 18 Apr 2016 11:57:03 -0300 Subject: [PATCH] API - Comments within articles --- lib/noosfero/api/entities.rb | 25 +++++++++++++------------ lib/noosfero/api/helpers.rb | 4 ++-- test/api/articles_test.rb | 16 ++++++++++++++++ 3 files changed, 31 insertions(+), 14 deletions(-) diff --git a/lib/noosfero/api/entities.rb b/lib/noosfero/api/entities.rb index a71d805..f9fb7b3 100644 --- a/lib/noosfero/api/entities.rb +++ b/lib/noosfero/api/entities.rb @@ -148,6 +148,18 @@ module Noosfero expose :members, :using => Person end + class CommentBase < Entity + expose :body, :title, :id + expose :created_at, :format_with => :timestamp + expose :author, :using => Profile + expose :reply_of, :using => CommentBase + end + + class Comment < CommentBase + root 'comments', 'comment' + expose :children, as: :replies, :using => Comment + end + class ArticleBase < Entity root 'articles', 'article' expose :id @@ -175,6 +187,7 @@ module Noosfero expose :votes_count expose :comments_count expose :type + expose :comments, using: CommentBase, :if => lambda{|obj,opt| opt[:params] && ['1','true',true].include?(opt[:params][:show_comments])} end class Article < ArticleBase @@ -183,18 +196,6 @@ module Noosfero expose :children, :using => ArticleBase end - class CommentBase < Entity - expose :body, :title, :id - expose :created_at, :format_with => :timestamp - expose :author, :using => Profile - expose :reply_of, :using => CommentBase - end - - class Comment < CommentBase - root 'comments', 'comment' - expose :children, as: :replies, :using => Comment - end - class User < Entity root 'users', 'user' diff --git a/lib/noosfero/api/helpers.rb b/lib/noosfero/api/helpers.rb index dcdae61..5b9d9a3 100644 --- a/lib/noosfero/api/helpers.rb +++ b/lib/noosfero/api/helpers.rb @@ -119,7 +119,7 @@ require_relative '../../find_by_contents' def present_article(asset) article = find_article(asset.articles, params[:id]) - present_partial article, :with => Entities::Article + present_partial article, :with => Entities::Article, :params => params end def present_articles_for_asset(asset, method = 'articles') @@ -128,7 +128,7 @@ require_relative '../../find_by_contents' end def present_articles(articles) - present_partial paginate(articles), :with => Entities::Article + present_partial paginate(articles), :with => Entities::Article, :params => params end def find_articles(asset, method = 'articles') diff --git a/test/api/articles_test.rb b/test/api/articles_test.rb index dbe6c3d..2efad45 100644 --- a/test/api/articles_test.rb +++ b/test/api/articles_test.rb @@ -664,4 +664,20 @@ class ArticlesTest < ActiveSupport::TestCase assert_not_nil json['article'][attribute] end end + + should 'only show article comments when show_comments is present' do + person = fast_create(Person) + article = fast_create(Article, :profile_id => person.id, :name => "Some thing") + article.comments.create!(:body => "another comment", :author => person) + + get "/api/v1/articles/#{article.id}/?#{params.merge(:show_comments => '1').to_query}" + json = JSON.parse(last_response.body) + assert_includes json["article"].keys, "comments" + assert_equal json["article"]["comments"].first["body"], "another comment" + + get "/api/v1/articles/#{article.id}/?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_not_includes json["article"].keys, "comments" + end + end -- libgit2 0.21.2