[Git][noosfero/noosfero][master] 2 commits: API - Comments within articles

Joenio Costa gitlab at mg.gitlab.com
Wed Apr 27 10:53:56 BRT 2016


Joenio Costa pushed to branch master at Noosfero / noosfero


Commits:
1924d8b6 by Marcos Ronaldo at 2016-04-18T11:57:03-03:00
API - Comments within articles

- - - - -
fa90a41d by Joenio Costa at 2016-04-27T13:53:38+00:00
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
- - - - -


3 changed files:

- lib/noosfero/api/entities.rb
- lib/noosfero/api/helpers.rb
- test/api/articles_test.rb


Changes:

=====================================
lib/noosfero/api/entities.rb
=====================================
--- a/lib/noosfero/api/entities.rb
+++ b/lib/noosfero/api/entities.rb
@@ -150,6 +150,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
@@ -177,6 +189,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
@@ -185,18 +198,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'
 


=====================================
lib/noosfero/api/helpers.rb
=====================================
--- a/lib/noosfero/api/helpers.rb
+++ b/lib/noosfero/api/helpers.rb
@@ -124,7 +124,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')
@@ -133,7 +133,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')


=====================================
test/api/articles_test.rb
=====================================
--- 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



View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/62abbce29210aaf457bb900ff59c931b324c8261...fa90a41d1e3ada3e18cf2622a37690f1143ea0af
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20160427/94395327/attachment-0001.html>


More information about the Noosfero-dev mailing list