[Git][noosfero/noosfero][api] 7 commits: Add pagination in api
Rodrigo Souto
gitlab at gitlab.com
Fri Aug 7 16:33:28 BRT 2015
Rodrigo Souto pushed to branch api at Noosfero / noosfero
Commits:
06a73d67 by Luciano Prestes Cavalcanti at 2015-08-03T16:55:48Z
Add pagination in api
Signed-off-by: Luciano Prestes Cavalcanti <lucianopcbr at gmail.com>
- - - - -
f3d1ede5 by Luciano Prestes Cavalcanti at 2015-08-06T09:57:44Z
Add tests for json api pagination
Signed-off-by: Luciano Prestes Cavalcanti <lucianopcbr at gmail.com>
- - - - -
fa43ac7e by Luciano Prestes Cavalcanti at 2015-08-06T14:22:13Z
Add updated_at in entities of API
Signed-off-by: Luciano Prestes Cavalcanti <lucianopcbr at gmail.com>
- - - - -
69d3de30 by Luciano Prestes Cavalcanti at 2015-08-06T14:22:13Z
Add timestamp in API
Signed-off-by: Luciano Prestes Cavalcanti <lucianopcbr at gmail.com>
- - - - -
9b4cb0ca by Luciano Prestes Cavalcanti at 2015-08-06T14:22:13Z
Add tests for API timestamp
Signed-off-by: Luciano Prestes Cavalcanti <lucianopcbr at gmail.com>
- - - - -
8136b8d2 by Rodrigo Souto at 2015-08-07T15:57:51Z
Merge branch 'api_pagination' into api
- - - - -
c7066b57 by Rodrigo Souto at 2015-08-07T16:28:45Z
api: ensure apropriate order on tests
- - - - -
4 changed files:
- lib/noosfero/api/entities.rb
- lib/noosfero/api/helpers.rb
- test/unit/api/articles_test.rb
- test/unit/api/communities_test.rb
Changes:
=====================================
lib/noosfero/api/entities.rb
=====================================
--- a/lib/noosfero/api/entities.rb
+++ b/lib/noosfero/api/entities.rb
@@ -33,6 +33,7 @@ module Noosfero
class Profile < Entity
expose :identifier, :name, :id
expose :created_at, :format_with => :timestamp
+ expose :updated_at, :format_with => :timestamp
expose :image, :using => Image
end
@@ -77,6 +78,7 @@ module Noosfero
expose :body
expose :abstract
expose :created_at, :format_with => :timestamp
+ expose :updated_at, :format_with => :timestamp
expose :title, :documentation => {:type => "String", :desc => "Title of the article"}
expose :created_by, :as => :author, :using => Profile
expose :profile, :using => Profile
=====================================
lib/noosfero/api/helpers.rb
=====================================
--- a/lib/noosfero/api/helpers.rb
+++ b/lib/noosfero/api/helpers.rb
@@ -131,6 +131,26 @@ module Noosfero
params[:order] || "created_at DESC"
end
+ def make_page_number_with_parameters(params)
+ params[:page] || 1
+ end
+
+ def make_per_page_with_parameters(params)
+ params[:per_page] ||= limit
+ params[:per_page].to_i
+ end
+
+ def make_timestamp_with_parameters_and_method(params, method)
+ timestamp = nil
+ if params[:timestamp]
+ datetime = DateTime.parse(params[:timestamp])
+ table_name = method.to_s.singularize.camelize.constantize.table_name
+ timestamp = "#{table_name}.updated_at >= '#{datetime}'"
+ end
+
+ timestamp
+ end
+
def by_reference(scope, params)
if params[:reference_id]
created_at = scope.find(params[:reference_id]).created_at
@@ -143,10 +163,14 @@ module Noosfero
def select_filtered_collection_of(object, method, params)
conditions = make_conditions_with_parameter(params)
order = make_order_with_parameters(params)
+ page_number = make_page_number_with_parameters(params)
+ per_page = make_per_page_with_parameters(params)
+ timestamp = make_timestamp_with_parameters_and_method(params, method)
objects = object.send(method)
objects = by_reference(objects, params)
- objects = objects.where(conditions).limit(limit).order(order)
+
+ objects = objects.where(conditions).where(timestamp).page(page_number).per_page(per_page).order(order)
objects
end
=====================================
test/unit/api/articles_test.rb
=====================================
--- a/test/unit/api/articles_test.rb
+++ b/test/unit/api/articles_test.rb
@@ -82,6 +82,43 @@ class ArticlesTest < ActiveSupport::TestCase
assert_not_includes json['articles'].map {|a| a['id']}, child.id
end
+ should 'list articles with pagination' do
+ Article.destroy_all
+ article_one = fast_create(Article, :profile_id => user.person.id, :name => "Another thing", :created_at => 2.days.ago)
+ article_two = fast_create(Article, :profile_id => user.person.id, :name => "Some thing", :created_at => 1.day.ago)
+
+ params[:page] = 1
+ params[:per_page] = 1
+ get "/api/v1/articles/?#{params.to_query}"
+ json_page_one = JSON.parse(last_response.body)
+
+ params[:page] = 2
+ params[:per_page] = 1
+ get "/api/v1/articles/?#{params.to_query}"
+ json_page_two = JSON.parse(last_response.body)
+
+ assert_includes json_page_one["articles"].map { |a| a["id"] }, article_two.id
+ assert_not_includes json_page_one["articles"].map { |a| a["id"] }, article_one.id
+
+ assert_includes json_page_two["articles"].map { |a| a["id"] }, article_one.id
+ assert_not_includes json_page_two["articles"].map { |a| a["id"] }, article_two.id
+ end
+
+ should 'list articles with timestamp' do
+ article_one = fast_create(Article, :profile_id => user.person.id, :name => "Another thing")
+ article_two = fast_create(Article, :profile_id => user.person.id, :name => "Some thing")
+
+ article_one.updated_at = Time.now + 3.hours
+ article_one.save!
+
+ params[:timestamp] = Time.now + 1.hours
+ get "/api/v1/articles/?#{params.to_query}"
+ json = JSON.parse(last_response.body)
+
+ assert_includes json["articles"].map { |a| a["id"] }, article_one.id
+ assert_not_includes json["articles"].map { |a| a["id"] }, article_two.id
+ end
+
#############################
# Profile Articles #
#############################
=====================================
test/unit/api/communities_test.rb
=====================================
--- a/test/unit/api/communities_test.rb
+++ b/test/unit/api/communities_test.rb
@@ -120,4 +120,40 @@ class CommunitiesTest < ActiveSupport::TestCase
assert_equivalent [c1.id], json['communities'].map {|c| c['id']}
end
+ should 'list communities with pagination' do
+ community1 = fast_create(Community, :public_profile => true, :created_at => 1.day.ago)
+ community2 = fast_create(Community, :created_at => 2.days.ago)
+
+ params[:page] = 2
+ params[:per_page] = 1
+ get "/api/v1/communities?#{params.to_query}"
+ json_page_two = JSON.parse(last_response.body)
+
+ params[:page] = 1
+ params[:per_page] = 1
+ get "/api/v1/communities?#{params.to_query}"
+ json_page_one = JSON.parse(last_response.body)
+
+
+ assert_includes json_page_one["communities"].map { |a| a["id"] }, community1.id
+ assert_not_includes json_page_one["communities"].map { |a| a["id"] }, community2.id
+
+ assert_includes json_page_two["communities"].map { |a| a["id"] }, community2.id
+ assert_not_includes json_page_two["communities"].map { |a| a["id"] }, community1.id
+ end
+
+ should 'list communities with timestamp' do
+ community1 = fast_create(Community, :public_profile => true)
+ community2 = fast_create(Community)
+
+ community1.updated_at = Time.now + 3.hours
+ community1.save!
+
+ params[:timestamp] = Time.now + 1.hours
+ get "/api/v1/communities/?#{params.to_query}"
+ json = JSON.parse(last_response.body)
+
+ assert_includes json["communities"].map { |a| a["id"] }, community1.id
+ assert_not_includes json["communities"].map { |a| a["id"] }, community2.id
+ end
end
View it on GitLab: https://gitlab.com/noosfero/noosfero/compare/34b572e59e11d4ca287b0db896e6071b631d0cbe...c7066b57c741f0407f6fe61e2bc6689eee0c4a53
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://listas.softwarelivre.org/pipermail/noosfero-dev/attachments/20150807/1a6efbd4/attachment-0001.html>
More information about the Noosfero-dev
mailing list