From 6745812c7236e8d4f463e226feb2e61389c85563 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Mon, 3 Sep 2012 03:43:19 -0700 Subject: [PATCH 1/3] bump kaminari --- Gemfile.lock | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/Gemfile.lock b/Gemfile.lock index f226c931..7ec37f59 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -198,7 +198,7 @@ GEM httparty (0.8.3) multi_json (~> 1.0) multi_xml - i18n (0.6.0) + i18n (0.6.1) journey (1.0.4) jquery-rails (2.0.2) railties (>= 3.2.0, < 5.0) @@ -206,11 +206,10 @@ GEM jquery-ui-rails (0.5.0) jquery-rails railties (>= 3.1.0) - json (1.7.4) - kaminari (0.13.0) + json (1.7.5) + kaminari (0.14.0) actionpack (>= 3.0.0) activesupport (>= 3.0.0) - railties (>= 3.0.0) kgio (2.7.4) launchy (2.1.0) addressable (~> 2.2.6) @@ -348,7 +347,7 @@ GEM daemons (>= 1.0.9) eventmachine (>= 0.12.6) rack (>= 1.0.0) - thor (0.15.4) + thor (0.16.0) tilt (1.3.3) treetop (1.4.10) polyglot From 6817a6a2952bc6af74eb239982ed337f90897f98 Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Mon, 3 Sep 2012 04:22:49 -0700 Subject: [PATCH 2/3] add kaminari config with default and max per_page values --- config/initializers/kaminari_config.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) create mode 100644 config/initializers/kaminari_config.rb diff --git a/config/initializers/kaminari_config.rb b/config/initializers/kaminari_config.rb new file mode 100644 index 00000000..3cbe9a05 --- /dev/null +++ b/config/initializers/kaminari_config.rb @@ -0,0 +1,10 @@ +Kaminari.configure do |config| + config.default_per_page = 20 + config.max_per_page = 100 + # config.window = 4 + # config.outer_window = 0 + # config.left = 0 + # config.right = 0 + # config.page_method_name = :page + # config.param_name = :page +end From 76e4d94d432b19d7dd58503c5eac46811630c04a Mon Sep 17 00:00:00 2001 From: Nihad Abbasov Date: Mon, 3 Sep 2012 04:46:29 -0700 Subject: [PATCH 3/3] add pagination to API --- doc/api/README.md | 7 +++++++ lib/api/helpers.rb | 4 ++++ lib/api/issues.rb | 4 ++-- lib/api/milestones.rb | 2 +- lib/api/projects.rb | 2 +- lib/api/users.rb | 2 +- 6 files changed, 16 insertions(+), 5 deletions(-) diff --git a/doc/api/README.md b/doc/api/README.md index 53b4983e..3a6c7b76 100644 --- a/doc/api/README.md +++ b/doc/api/README.md @@ -23,6 +23,13 @@ GET http://example.com/api/v2/projects?private_token=QVy1PB7sTxfy4pqfZM1U The API uses JSON to serialize data. You don't need to specify `.json` at the end of API URL. +#### Pagination + +When listing resources you can pass the following parameters: + ++ `page` (default: `1`) - page number ++ `per_page` (default: `20`, max: `100`) - how many items to list per page + ## Contents + [Users](https://github.com/gitlabhq/gitlabhq/blob/master/doc/api/users.md) diff --git a/lib/api/helpers.rb b/lib/api/helpers.rb index c1ea0566..ce7b7b49 100644 --- a/lib/api/helpers.rb +++ b/lib/api/helpers.rb @@ -14,6 +14,10 @@ module Gitlab @project end + def paginate(object) + object.page(params[:page]).per(params[:per_page].to_i) + end + def authenticate! error!({'message' => '401 Unauthorized'}, 401) unless current_user end diff --git a/lib/api/issues.rb b/lib/api/issues.rb index 836c2818..68cb7e05 100644 --- a/lib/api/issues.rb +++ b/lib/api/issues.rb @@ -9,7 +9,7 @@ module Gitlab # Example Request: # GET /issues get do - present current_user.issues, with: Entities::Issue + present paginate(current_user.issues), with: Entities::Issue end end @@ -21,7 +21,7 @@ module Gitlab # Example Request: # GET /projects/:id/issues get ":id/issues" do - present user_project.issues, with: Entities::Issue + present paginate(user_project.issues), with: Entities::Issue end # Get a single project issue diff --git a/lib/api/milestones.rb b/lib/api/milestones.rb index f537b8e5..29f5efa4 100644 --- a/lib/api/milestones.rb +++ b/lib/api/milestones.rb @@ -11,7 +11,7 @@ module Gitlab # Example Request: # GET /projects/:id/milestones get ":id/milestones" do - present user_project.milestones, with: Entities::Milestone + present paginate(user_project.milestones), with: Entities::Milestone end # Get a single project milestone diff --git a/lib/api/projects.rb b/lib/api/projects.rb index f42849cd..3d4fde92 100644 --- a/lib/api/projects.rb +++ b/lib/api/projects.rb @@ -9,7 +9,7 @@ module Gitlab # Example Request: # GET /projects get do - @projects = current_user.projects + @projects = paginate current_user.projects present @projects, with: Entities::Project end diff --git a/lib/api/users.rb b/lib/api/users.rb index 81cb2a0e..98ced6f8 100644 --- a/lib/api/users.rb +++ b/lib/api/users.rb @@ -9,7 +9,7 @@ module Gitlab # Example Request: # GET /users get do - @users = User.all + @users = paginate User present @users, with: Entities::User end