jbuilder_pagination_plus 0.0.1 → 0.0.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +2 -1
- data/README.md +1 -1
- data/lib/jbuilder/pagination.rb +22 -0
- data/lib/jbuilder/pagination/exceptions/unpageable_resource_error.rb +13 -0
- data/lib/jbuilder/pagination/version.rb +1 -1
- metadata +2 -3
- data/lib/concerns/pagination.rb +0 -25
- data/lib/exceptions/unpageable_resource_error.rb +0 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 97ca43221a3f22c2b74158298688d92bd46340c9
|
4
|
+
data.tar.gz: 20c8892e8c423755cfd7b3e5e27b812e50f6768b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: f977ecf09a870ff5fe4abe3a89cb766b4994f98e1d90bea0d9dd4d2654c8166e8ce47b627499895abdd6978e76dc31a368eb6ff89e77eeac5fd5d381161734f0
|
7
|
+
data.tar.gz: db68215e0b2c45e2324c66505cf7b38a184099d10cfd6301791b3a7da03bc865217cbb736020025778b0c357e5353bd50bad098e875c127684be1481fa931ee3
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -1,4 +1,4 @@
|
|
1
|
-
# Jbuilder Pagination Plus [](https://travis-ci.org/PinsterTeam/jbuilder_pagination_plus)
|
1
|
+
# Jbuilder Pagination Plus [](https://travis-ci.org/PinsterTeam/jbuilder_pagination_plus) [](https://badge.fury.io/rb/jbuilder_pagination_plus)
|
2
2
|
|
3
3
|
[Jbuilder](https://github.com/rails/jbuilder) extension that makes easier to use pagination according to the [JSON API](http://jsonapi.org/format/#fetching-pagination) conventions.
|
4
4
|
|
data/lib/jbuilder/pagination.rb
CHANGED
@@ -1,3 +1,25 @@
|
|
1
1
|
require "jbuilder"
|
2
2
|
require "jbuilder/pagination/pages"
|
3
|
+
require 'jbuilder/pagination/exceptions/unpageable_resource_error'
|
3
4
|
|
5
|
+
module Pagination
|
6
|
+
# The order of this hash matters, elements are pulled off the end by calling .pop recursively
|
7
|
+
DEFAULT_PAGINATION = [[:page, ->(params) { params.dig(:page, :number) }],
|
8
|
+
[:per, ->(params) { params.dig(:page, :size) }]].freeze
|
9
|
+
|
10
|
+
def paginate(pageable_resource, methods = DEFAULT_PAGINATION.dup, params = self.params)
|
11
|
+
return pageable_resource if methods.blank?
|
12
|
+
key_value_array = methods.pop
|
13
|
+
build_pagination(key_value_array, paginate(pageable_resource, methods, params), params)
|
14
|
+
end
|
15
|
+
|
16
|
+
private
|
17
|
+
|
18
|
+
def build_pagination(key_value_array, pageable_resource, params)
|
19
|
+
unless pageable_resource.respond_to?(key_value_array[0])
|
20
|
+
raise Errors::UnpageableResourceError, "Resource does not respond to '#{key_value_array[0]}' method!"
|
21
|
+
end
|
22
|
+
|
23
|
+
pageable_resource.public_send(key_value_array[0], key_value_array[1].call(params))
|
24
|
+
end
|
25
|
+
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: jbuilder_pagination_plus
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Bruno Bacarini
|
@@ -102,9 +102,8 @@ files:
|
|
102
102
|
- bin/console
|
103
103
|
- bin/setup
|
104
104
|
- jbuilder_pagination_plus.gemspec
|
105
|
-
- lib/concerns/pagination.rb
|
106
|
-
- lib/exceptions/unpageable_resource_error.rb
|
107
105
|
- lib/jbuilder/pagination.rb
|
106
|
+
- lib/jbuilder/pagination/exceptions/unpageable_resource_error.rb
|
108
107
|
- lib/jbuilder/pagination/pages.rb
|
109
108
|
- lib/jbuilder/pagination/version.rb
|
110
109
|
homepage: https://github.com/PinsterTeam/jbuilder_pagination_plus
|
data/lib/concerns/pagination.rb
DELETED
@@ -1,25 +0,0 @@
|
|
1
|
-
# frozen_string_literal: true
|
2
|
-
|
3
|
-
require 'exceptions/unpageable_resource_error'
|
4
|
-
|
5
|
-
module Pagination
|
6
|
-
# The order of this hash matters, elements are pulled off the end by calling .pop recursively
|
7
|
-
DEFAULT_PAGINATION = [[:page, ->(params) { params.dig(:page, :number) }],
|
8
|
-
[:per, ->(params) { params.dig(:page, :size) }]].freeze
|
9
|
-
|
10
|
-
def paginate(pageable_resource, methods = DEFAULT_PAGINATION.dup, params = self.params)
|
11
|
-
return pageable_resource if methods.blank?
|
12
|
-
key_value_array = methods.pop
|
13
|
-
build_pagination(key_value_array, paginate(pageable_resource, methods, params), params)
|
14
|
-
end
|
15
|
-
|
16
|
-
private
|
17
|
-
|
18
|
-
def build_pagination(key_value_array, pageable_resource, params)
|
19
|
-
unless pageable_resource.respond_to?(key_value_array[0])
|
20
|
-
raise UnpageableResourceError, "Resource does not respond to '#{key_value_array[0]}' method!"
|
21
|
-
end
|
22
|
-
|
23
|
-
pageable_resource.public_send(key_value_array[0], key_value_array[1].call(params))
|
24
|
-
end
|
25
|
-
end
|