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 | 103 | params[:order] || "created_at DESC" |
104 | 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 | 115 | def select_filtered_collection_of(object, method, params) |
107 | 116 | conditions = make_conditions_with_parameter(params) |
108 | 117 | order = make_order_with_parameters(params) |
109 | 118 | |
110 | 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 | 121 | objects = objects.where(conditions).limit(limit).order(order) |
116 | 122 | |
117 | 123 | objects | ... | ... |
lib/noosfero/api/v1/comments.rb
... | ... | @@ -15,18 +15,10 @@ module Noosfero |
15 | 15 | # Example Request: |
16 | 16 | # GET /articles/12/comments?oldest&limit=10&reference_id=23 |
17 | 17 | get ":id/comments" do |
18 | - | |
19 | - conditions = make_conditions_with_parameter(params) | |
20 | 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 | 21 | present comments, :with => Entities::Comment |
29 | - | |
30 | 22 | end |
31 | 23 | |
32 | 24 | get ":id/comments/:comment_id" do | ... | ... |
test/unit/api/comments_test.rb
... | ... | @@ -25,7 +25,7 @@ class CommentsTest < ActiveSupport::TestCase |
25 | 25 | assert_equal 403, last_response.status |
26 | 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 | 29 | person = fast_create(Person) |
30 | 30 | article = fast_create(Article, :profile_id => person.id, :name => "Some thing", :published => false) |
31 | 31 | assert !article.published? |
... | ... | @@ -41,7 +41,28 @@ class CommentsTest < ActiveSupport::TestCase |
41 | 41 | |
42 | 42 | get "/api/v1/articles/#{article.id}/comments?#{params.to_query}" |
43 | 43 | json = JSON.parse(last_response.body) |
44 | + assert_equal 200, last_response.status | |
44 | 45 | assert_equal 2, json["comments"].length |
45 | 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 | 68 | end | ... | ... |