pager_api 0.2.4 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 26830a930c08e28898bc2b08427937a650edbacdfa3f30ba5e772a00f106f853
4
- data.tar.gz: 0d0ce3d051f008887e2a12056583446d3fd3eee62590928bca58635d874410ac
3
+ metadata.gz: 67a2baad4c6f702c2513b3660a6a6dea15254eb0a45f04115a5575e18ea79c2f
4
+ data.tar.gz: 3b29f1c7b4b9b523d06f5459e48fe526016166b0312d762aa21c5abf43fbcd55
5
5
  SHA512:
6
- metadata.gz: c14e9e1d77e18b61ba966320e413746107f8e2535cbd5c57b9cca149b6578d6f65050c4a08c607bd2e91b277321adaa5a807016d63cb5329ba4e2147bda43918
7
- data.tar.gz: 7353b9e6620269f4b87f9e01a89c3457542e89a0caf37327227e2fb4235dd9f60babc031841e638b583c2dd6c0e69068b1484e6b6a5af211e86cebaf0415188b
6
+ metadata.gz: dd773a056972e831875221c96785f0011dbad53df9843036f344d95aab019e4b6647a7480f0fa1e62560c2f313d7aafe51cd611a378daa5a27b469bc3cd156a2
7
+ data.tar.gz: 8ef3ff35c17a6524628c29a602f4673ce3ce4a66f631607dbc7b42b7cd91f7307a15d6e33136e4b7a980eab20c5fa1e2a8d37572b275ad05c1b98ad020e4dabb
data/README.md CHANGED
@@ -22,11 +22,12 @@ API Pagination done right. Pager API is a library to help you add `meta` informa
22
22
 
23
23
  ## Quick Start
24
24
 
25
- `pager_api` depends on [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate) to handle pagination. You need to add one of these gems to your Gemfile **before** the `pager_api` gem:
25
+ `pager_api` depends on [Pagy](https://github.com/ddnexus/pagy), [Kaminari](https://github.com/amatsuda/kaminari), [WillPaginate](https://github.com/mislav/will_paginate) to handle pagination. You need to add one of these gems to your Gemfile **before** the `pager_api` gem:
26
26
 
27
27
  ```ruby
28
28
  # gem 'will_paginate'
29
29
  # gem 'kaminari'
30
+ # gem 'pagy'
30
31
  gem 'pager_api'
31
32
  ```
32
33
 
@@ -48,7 +49,7 @@ The gem comes with an installer for you to configure it, for example to switch b
48
49
 
49
50
  This will create a file under the `initializers` directory called `pager_api.rb`. You can easily configure it there to meet your needs.
50
51
 
51
- By default `pager_api` uses [Kaminari](https://github.com/amatsuda/kaminari). Configure the `pager_api.rb` initializer in order to use [WillPaginate](https://github.com/mislav/will_paginate).
52
+ By default `pager_api` uses [Pagy](https://github.com/ddnexus/pagy). Configure the `pager_api.rb` initializer in order to use [WillPaginate](https://github.com/mislav/will_paginate) or [Kaminari](https://github.com/amatsuda/kaminari).
52
53
 
53
54
  **We highly recommend you use [Active Model Serializers](https://github.com/rails-api/active_model_serializers) for rendering your JSON responses**
54
55
 
@@ -1,7 +1,7 @@
1
1
  module PagerApi
2
2
  module Generators
3
3
  class InstallGenerator < Rails::Generators::Base
4
- source_root File.expand_path("../templates", __FILE__)
4
+ source_root File.expand_path("../templates", __FILE__)
5
5
 
6
6
  desc "Creates a PagerApi initializer in your application"
7
7
 
@@ -1,20 +1,21 @@
1
+ begin; require "kaminari"; rescue LoadError; end
2
+ begin; require "will_paginate"; rescue LoadError; end
3
+ begin; require "pagy"; rescue LoadError; end
4
+
1
5
  # Dynamic pagination handler call
2
6
  require "pager_api/pagination/#{PagerApi.pagination_handler}"
3
7
 
4
- begin; require 'kaminari'; rescue LoadError; end
5
- begin; require 'will_paginate'; rescue LoadError; end
6
-
7
- if defined?(ActionController::API)
8
- ActionController::API.send(:include, "PagerApi::Pagination::#{PagerApi.pagination_handler.to_s.classify}".constantize)
9
- else
8
+ if defined?(ActionController::Base)
10
9
  ActionController::Base.send(:include, "PagerApi::Pagination::#{PagerApi.pagination_handler.to_s.classify}".constantize)
10
+ else
11
+ ActionController::API.send(:include, "PagerApi::Pagination::#{PagerApi.pagination_handler.to_s.classify}".constantize)
11
12
  end
12
13
 
13
- unless defined?(Kaminari) or defined?(WillPaginate)
14
- Kernel.warn <<-WARNING.gsub(/^\s{4}/, '')
15
- Warning: pager-api needs Kaminari or Will Paginate as a dependency.
14
+ unless defined?(Kaminari) or defined?(WillPaginate) or defined?(Pagy)
15
+ Kernel.warn <<-WARNING.gsub(/^\s{4}/, "")
16
+ Warning: pager-api needs Kaminari, Will Paginate or Pagy as a dependency.
16
17
  You need to add it to your Gemfile
17
18
 
18
- gem 'kaminari' or gem 'will_paginate'
19
+ gem 'kaminari', gem 'will_paginate' or gem 'pagy'
19
20
  WARNING
20
21
  end
@@ -0,0 +1,87 @@
1
+ module PagerApi
2
+ module Pagination
3
+ module Pagy
4
+ include ::Pagy::Backend
5
+
6
+ def paginate(*args)
7
+ options = args.extract_options!
8
+ collection = args.first
9
+
10
+ pagy, paginated_collection = paginate_collection(collection, options)
11
+
12
+ options[:json] = paginated_collection
13
+
14
+ options[:meta] = meta(pagy, options) if PagerApi.include_pagination_on_meta?
15
+
16
+ pagination_headers(pagy) if PagerApi.include_pagination_headers?
17
+
18
+ render options
19
+ end
20
+
21
+ private
22
+
23
+ # Link: <http://example.com/api/v1/users?page="2">; rel="next",
24
+ # <http://example.com/api/v1//users?page="5">; rel="last",
25
+ # <http://example.com/api/v1//users?page="1">; rel="first",
26
+ # <http://example.com/api/v1/users?page="1">; rel="prev",
27
+ def pagination_headers(pagy)
28
+ links = (headers["Link"] || "").split(",").map(&:strip)
29
+ clean_url = request.original_url.sub(/\?.*$/, "")
30
+
31
+ paging_info = pages(pagy)
32
+
33
+ paging_info.each do |key, value|
34
+ query_params = request.query_parameters.merge(page: value)
35
+ links << %Q{ <#{clean_url}?#{query_params.to_param}>; rel="#{key}" }
36
+ end
37
+
38
+ headers["Link"] = links.join(", ") unless links.empty?
39
+ headers[PagerApi.total_pages_header] = pagy.pages
40
+ headers[PagerApi.total_count_header] = pagy.count
41
+
42
+ return nil
43
+ end
44
+
45
+ def pagination_links(pagy)
46
+ current_uri = request.env["PATH_INFO"]
47
+ meta_links = {}
48
+
49
+ pages(pagy).each do |key, value|
50
+ query_params = request.query_parameters.merge(page: value)
51
+ meta_links[key] = "#{current_uri}?#{query_params.to_param}"
52
+ end
53
+
54
+ meta_links
55
+ end
56
+
57
+ def pages(pagy)
58
+ {}.tap do |paging|
59
+ paging[:first] = 1
60
+ paging[:last] = pagy.pages
61
+
62
+ paging[:prev] = pagy.prev unless pagy.prev.nil?
63
+ paging[:next] = pagy.next unless pagy.next.nil?
64
+ end
65
+ end
66
+
67
+ def paginate_collection(collection, options = {})
68
+ options[:page] = params[:page] || 1
69
+ options[:items] = options.delete(:per_page) || params[:per_page] || ::Pagy::VARS[:items]
70
+
71
+ meta, collection = pagy(collection, options)
72
+ [meta, collection]
73
+ end
74
+
75
+ def meta(pagy, options = {})
76
+ {
77
+ pagination: {
78
+ per_page: pagy.items,
79
+ total_pages: pagy.pages,
80
+ total_objects: pagy.count,
81
+ links: pagination_links(pagy),
82
+ },
83
+ }
84
+ end
85
+ end
86
+ end
87
+ end
@@ -1,6 +1,6 @@
1
1
  module PagerApi
2
2
  class Railtie < Rails::Railtie
3
- initializer "pager_api.configure_pagination_helpers" do
3
+ config.after_initialize do
4
4
  require 'pager_api/hooks'
5
5
  end
6
6
  end
@@ -1,3 +1,3 @@
1
1
  module PagerApi
2
- VERSION = "0.2.4"
2
+ VERSION = "0.3.0"
3
3
  end
data/lib/pager_api.rb CHANGED
@@ -2,7 +2,7 @@ module PagerApi
2
2
 
3
3
  # pagination handler
4
4
  mattr_accessor :pagination_handler
5
- @@pagination_handler = :kaminari
5
+ @@pagination_handler = :pagy
6
6
 
7
7
  # Meta tag information for pagination
8
8
  mattr_accessor :include_pagination_on_meta
@@ -32,7 +32,6 @@ module PagerApi
32
32
  def self.setup
33
33
  yield self
34
34
  end
35
-
36
35
  end
37
36
 
38
37
  require "pager_api/version"
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: pager_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.4
4
+ version: 0.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Abraham Kuri
8
8
  autorequire:
9
9
  bindir: exe
10
10
  cert_chain: []
11
- date: 2019-04-16 00:00:00.000000000 Z
11
+ date: 2019-04-30 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bundler
@@ -64,6 +64,7 @@ files:
64
64
  - lib/pager_api.rb
65
65
  - lib/pager_api/hooks.rb
66
66
  - lib/pager_api/pagination/kaminari.rb
67
+ - lib/pager_api/pagination/pagy.rb
67
68
  - lib/pager_api/pagination/will_paginate.rb
68
69
  - lib/pager_api/railtie.rb
69
70
  - lib/pager_api/version.rb