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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: b598e1844ee0ffb21795036c763ded62f5aaebba
4
- data.tar.gz: 0f8f48ea56bb67c9edf48f818cc589714c73294a
3
+ metadata.gz: 97ca43221a3f22c2b74158298688d92bd46340c9
4
+ data.tar.gz: 20c8892e8c423755cfd7b3e5e27b812e50f6768b
5
5
  SHA512:
6
- metadata.gz: f79f1c3771d7b01761026e91073daf0e7d1a66c1758fcd0c1daec3e5386deabb5e497c88f6373fb8e527c319add201c9c96dc3462b806443de732848d7d98ba0
7
- data.tar.gz: 973a9854fd17438a380d4ef223ffc99b6e69bfd15a6579b0be54c960eb3b7605ad41c7892b0bc43a957c52dd98109372ca44300c6a5f60e1fd14aa6e3985e399
6
+ metadata.gz: f977ecf09a870ff5fe4abe3a89cb766b4994f98e1d90bea0d9dd4d2654c8166e8ce47b627499895abdd6978e76dc31a368eb6ff89e77eeac5fd5d381161734f0
7
+ data.tar.gz: db68215e0b2c45e2324c66505cf7b38a184099d10cfd6301791b3a7da03bc865217cbb736020025778b0c357e5353bd50bad098e875c127684be1481fa931ee3
data/.gitignore CHANGED
@@ -8,4 +8,5 @@
8
8
  /spec/reports/
9
9
  /tmp/
10
10
  *.sw?
11
- .idea/
11
+ .idea/
12
+ *.gem
data/README.md CHANGED
@@ -1,4 +1,4 @@
1
- # Jbuilder Pagination Plus [![Build Status](https://travis-ci.org/PinsterTeam/jbuilder_pagination_plus.svg?branch=master)](https://travis-ci.org/PinsterTeam/jbuilder_pagination_plus)
1
+ # Jbuilder Pagination Plus [![Build Status](https://travis-ci.org/PinsterTeam/jbuilder_pagination_plus.svg?branch=master)](https://travis-ci.org/PinsterTeam/jbuilder_pagination_plus) [![Gem Version](https://badge.fury.io/rb/jbuilder_pagination_plus.svg)](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
 
@@ -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
@@ -0,0 +1,13 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Pagination
4
+ module Errors
5
+ class UnpageableResourceError < StandardError
6
+ def initialize(message)
7
+ super(message)
8
+ end
9
+ end
10
+ end
11
+ end
12
+
13
+
@@ -1,5 +1,5 @@
1
1
  module Jbuilder
2
2
  class Pagination
3
- VERSION = "0.0.1"
3
+ VERSION = "0.0.2"
4
4
  end
5
5
  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.1
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
@@ -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
@@ -1,7 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- class UnpageableResourceError < StandardError
4
- def initialize(message)
5
- super(message)
6
- end
7
- end