From e7aecdac406b942733780faff9cc681d450837c4 Mon Sep 17 00:00:00 2001 From: Victor Costa Date: Mon, 4 Apr 2016 17:50:12 -0300 Subject: [PATCH] api: add filter option to return only root comments --- lib/noosfero/api/v1/comments.rb | 2 +- test/api/comments_test.rb | 12 ++++++++++++ 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/lib/noosfero/api/v1/comments.rb b/lib/noosfero/api/v1/comments.rb index aef92b6..c631190 100644 --- a/lib/noosfero/api/v1/comments.rb +++ b/lib/noosfero/api/v1/comments.rb @@ -20,7 +20,7 @@ module Noosfero get ":id/comments" do article = find_article(environment.articles, params[:id]) comments = select_filtered_collection_of(article, :comments, params) - + comments = comments.without_reply if(params[:without_reply].present?) present comments, :with => Entities::Comment, :current_person => current_person end diff --git a/test/api/comments_test.rb b/test/api/comments_test.rb index ef22a42..b2aa960 100644 --- a/test/api/comments_test.rb +++ b/test/api/comments_test.rb @@ -88,4 +88,16 @@ class CommentsTest < ActiveSupport::TestCase assert_equal 200, last_response.status assert_equal 3, json["comments"].length end + + should 'return only root comments' do + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") + comment1 = article.comments.create!(:body => "some comment", :author => user.person) + comment2 = article.comments.create!(:body => "another comment", :author => user.person, :reply_of_id => comment1.id) + params[:without_reply] = true + + get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" + json = JSON.parse(last_response.body) + assert_equal 200, last_response.status + assert_equal [comment1.id], json["comments"].map { |c| c['id'] } + end end -- libgit2 0.21.2