Commit c71d67d786737154c578562b3ee037ea48c27a67
1 parent
d3b8064f
Exists in
master
and in
29 other branches
api: review comments mountpoint
Showing
3 changed files
with
33 additions
and
14 deletions
Show diff stats
lib/noosfero/api/helpers.rb
@@ -103,15 +103,21 @@ module Noosfero | @@ -103,15 +103,21 @@ module Noosfero | ||
103 | params[:order] || "created_at DESC" | 103 | params[:order] || "created_at DESC" |
104 | end | 104 | end |
105 | 105 | ||
106 | + def by_reference(scope, params) | ||
107 | + if params[:reference_id] | ||
108 | + created_at = scope.find(params[:reference_id]).created_at | ||
109 | + scope.send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at) | ||
110 | + else | ||
111 | + scope | ||
112 | + end | ||
113 | + end | ||
114 | + | ||
106 | def select_filtered_collection_of(object, method, params) | 115 | def select_filtered_collection_of(object, method, params) |
107 | conditions = make_conditions_with_parameter(params) | 116 | conditions = make_conditions_with_parameter(params) |
108 | order = make_order_with_parameters(params) | 117 | order = make_order_with_parameters(params) |
109 | 118 | ||
110 | objects = object.send(method) | 119 | objects = object.send(method) |
111 | - if params[:reference_id] | ||
112 | - created_at = objects.find(params[:reference_id]).created_at | ||
113 | - objects = objects.send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at) | ||
114 | - end | 120 | + objects = by_reference(objects, params) |
115 | objects = objects.where(conditions).limit(limit).order(order) | 121 | objects = objects.where(conditions).limit(limit).order(order) |
116 | 122 | ||
117 | objects | 123 | objects |
lib/noosfero/api/v1/comments.rb
@@ -15,18 +15,10 @@ module Noosfero | @@ -15,18 +15,10 @@ module Noosfero | ||
15 | # Example Request: | 15 | # Example Request: |
16 | # GET /articles/12/comments?oldest&limit=10&reference_id=23 | 16 | # GET /articles/12/comments?oldest&limit=10&reference_id=23 |
17 | get ":id/comments" do | 17 | get ":id/comments" do |
18 | - | ||
19 | - conditions = make_conditions_with_parameter(params) | ||
20 | article = find_article(environment.articles, params[:id]) | 18 | article = find_article(environment.articles, params[:id]) |
19 | + comments = select_filtered_collection_of(article, :comments, params) | ||
21 | 20 | ||
22 | - if params[:reference_id] | ||
23 | - created_at = article.comments.find(params[:reference_id]).created_at | ||
24 | - comments = article.comments.send("#{params.key?(:oldest) ? 'older_than' : 'younger_than'}", created_at).reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit) | ||
25 | - else | ||
26 | - comments = article.comments.reorder("created_at DESC").find(:all, :conditions => conditions, :limit => limit) | ||
27 | - end | ||
28 | present comments, :with => Entities::Comment | 21 | present comments, :with => Entities::Comment |
29 | - | ||
30 | end | 22 | end |
31 | 23 | ||
32 | get ":id/comments/:comment_id" do | 24 | get ":id/comments/:comment_id" do |
test/unit/api/comments_test.rb
@@ -25,7 +25,7 @@ class CommentsTest < ActiveSupport::TestCase | @@ -25,7 +25,7 @@ class CommentsTest < ActiveSupport::TestCase | ||
25 | assert_equal 403, last_response.status | 25 | assert_equal 403, last_response.status |
26 | end | 26 | end |
27 | 27 | ||
28 | - should 'not comment a article if user has no permission to view it' do | 28 | + should 'not comment an article if user has no permission to view it' do |
29 | person = fast_create(Person) | 29 | person = fast_create(Person) |
30 | article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) | 30 | article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) |
31 | assert !article.published? | 31 | assert !article.published? |
@@ -41,7 +41,28 @@ class CommentsTest < ActiveSupport::TestCase | @@ -41,7 +41,28 @@ class CommentsTest < ActiveSupport::TestCase | ||
41 | 41 | ||
42 | get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" | 42 | get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" |
43 | json = JSON.parse(last_response.body) | 43 | json = JSON.parse(last_response.body) |
44 | + assert_equal 200, last_response.status | ||
44 | assert_equal 2, json["comments"].length | 45 | assert_equal 2, json["comments"].length |
45 | end | 46 | end |
46 | 47 | ||
48 | + should 'return comment of an article' do | ||
49 | + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | ||
50 | + comment = article.comments.create!(:body => "another comment", :author => user.person) | ||
51 | + | ||
52 | + get "/api/v1/articles/#{article.id}/comments/#{comment.id}?#{params.to_query}" | ||
53 | + json = JSON.parse(last_response.body) | ||
54 | + assert_equal 200, last_response.status | ||
55 | + assert_equal comment.id, json['comment']['id'] | ||
56 | + end | ||
57 | + | ||
58 | + should 'comment an article' do | ||
59 | + article = fast_create(Article, :profile_id => user.person.id, :name => "Some thing") | ||
60 | + body = 'My comment' | ||
61 | + params.merge!({:body => body}) | ||
62 | + | ||
63 | + post "/api/v1/articles/#{article.id}/comments?#{params.to_query}" | ||
64 | + json = JSON.parse(last_response.body) | ||
65 | + assert_equal 201, last_response.status | ||
66 | + assert_equal body, json['comment']['body'] | ||
67 | + end | ||
47 | end | 68 | end |