jbuilder_pagination_plus 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: b598e1844ee0ffb21795036c763ded62f5aaebba
4
+ data.tar.gz: 0f8f48ea56bb67c9edf48f818cc589714c73294a
5
+ SHA512:
6
+ metadata.gz: f79f1c3771d7b01761026e91073daf0e7d1a66c1758fcd0c1daec3e5386deabb5e497c88f6373fb8e527c319add201c9c96dc3462b806443de732848d7d98ba0
7
+ data.tar.gz: 973a9854fd17438a380d4ef223ffc99b6e69bfd15a6579b0be54c960eb3b7605ad41c7892b0bc43a957c52dd98109372ca44300c6a5f60e1fd14aa6e3985e399
data/.gitignore ADDED
@@ -0,0 +1,11 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /Gemfile.lock
4
+ /_yardoc/
5
+ /coverage/
6
+ /doc/
7
+ /pkg/
8
+ /spec/reports/
9
+ /tmp/
10
+ *.sw?
11
+ .idea/
data/.rspec ADDED
@@ -0,0 +1 @@
1
+ --color
data/.ruby-gemset ADDED
@@ -0,0 +1 @@
1
+ jbuilder_pagination
data/.ruby-version ADDED
@@ -0,0 +1 @@
1
+ ruby-2.4.3
data/.travis.yml ADDED
@@ -0,0 +1,6 @@
1
+ language: ruby
2
+ rvm:
3
+ - 2.4.2
4
+ install: bin/setup
5
+ script:
6
+ - bundle exec rspec spec
@@ -0,0 +1,13 @@
1
+ # Contributor Code of Conduct
2
+
3
+ As contributors and maintainers of this project, we pledge to respect all people who contribute through reporting issues, posting feature requests, updating documentation, submitting pull requests or patches, and other activities.
4
+
5
+ We are committed to making participation in this project a harassment-free experience for everyone, regardless of level of experience, gender, gender identity and expression, sexual orientation, disability, personal appearance, body size, race, age, or religion.
6
+
7
+ Examples of unacceptable behavior by participants include the use of sexual language or imagery, derogatory comments or personal attacks, trolling, public or private harassment, insults, or other unprofessional conduct.
8
+
9
+ Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct. Project maintainers who do not follow the Code of Conduct may be removed from the project team.
10
+
11
+ Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by opening an issue or contacting one or more of the project maintainers.
12
+
13
+ This Code of Conduct is adapted from the [Contributor Covenant](http:contributor-covenant.org), version 1.0.0, available at [http://contributor-covenant.org/version/1/0/0/](http://contributor-covenant.org/version/1/0/0/)
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source 'https://rubygems.org'
2
+
3
+ # Specify your gem's dependencies in jbuilder_pagination_plus.gemspec
4
+ gemspec
data/LICENSE.txt ADDED
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2015 Bruno Bacarini
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
data/README.md ADDED
@@ -0,0 +1,85 @@
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)
2
+
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
+
5
+ Forked from: [https://github.com/bacarini/jbuilder_pagination](https://github.com/bacarini/jbuilder_pagination)
6
+
7
+ ## Requirement
8
+
9
+ `JbuilderPaginationPlus` relies on a paginated collection with the methods `current_page`, `total_pages`, and `size`, such as are supported by both [Kaminari](https://github.com/amatsuda/kaminari) or [WillPaginate](https://github.com/mislav/will_paginate).
10
+
11
+ ## Installation
12
+
13
+ Add this line to your application's Gemfile:
14
+
15
+ ```ruby
16
+ gem 'jbuilder_pagination_plus', require: 'jbuilder/pagination'
17
+ ```
18
+
19
+ And then execute:
20
+
21
+ $ bundle
22
+
23
+ Or install it yourself as:
24
+
25
+ $ gem install jbuilder_pagination_plus
26
+
27
+ ## Usage
28
+
29
+ ###### Kaminari examples
30
+ ```ruby
31
+ #array
32
+ @posts = paginate Kaminari.paginate_array([1, 2, 3])
33
+
34
+ #active_record
35
+ @posts = paginate Post
36
+ ```
37
+
38
+ ###### WillPaginate examples
39
+
40
+ ```ruby
41
+ #array
42
+ @posts = [1,2,3].paginate(page: 3, per_page: 1)
43
+
44
+ #active_record
45
+ @posts = Post.page(3).per_page(1)
46
+ ```
47
+
48
+ And then in your `*.json.jbuilder`
49
+
50
+ ```ruby
51
+ json.links do
52
+ json.pages! @posts, url: "http://example.com/posts", query_parameters: { additional: 'parameters' }
53
+ end
54
+ json.data do
55
+ # Whatever your data is
56
+ end
57
+
58
+ # {
59
+ # "links": {
60
+ # "self": "http://example.com/posts?page[number]=3&page[size]=1&additional=parameters",
61
+ # "first": "http://example.com/posts?page[number]=1&page[size]=1&additional=parameters",
62
+ # "prev": "http://example.com/posts?page[number]=2&page[size]=1&additional=parameters",
63
+ # "next": "http://example.com/posts?page[number]=4&page[size]=1&additional=parameters",
64
+ # "last": "http://example.com/posts?page[number]=13&page[size]=1&additional=parameters"
65
+ # },
66
+ # "data": {
67
+ # ...
68
+ # }
69
+ # }
70
+ ```
71
+ The options `url` and `query_parameters` are opcionals.
72
+
73
+ In case there is no pagination at all, `links` will be omitted.
74
+
75
+ ## Development
76
+
77
+ After checking out the repo, run `bin/setup` to install dependencies. Then, run `bin/console` for an interactive prompt that will allow you to experiment.
78
+
79
+ ## Contributing
80
+
81
+ 1. Fork it
82
+ 2. Create your feature branch (`git checkout -b my-new-feature`)
83
+ 3. Commit your changes (`git commit -am 'Add some feature'`)
84
+ 4. Push to the branch (`git push origin my-new-feature`)
85
+ 5. Create a new Pull Request
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require "bundler/gem_tasks"
data/bin/console ADDED
@@ -0,0 +1,14 @@
1
+ #!/usr/bin/env ruby
2
+
3
+ require "bundler/setup"
4
+ require "jbuilder/pagination"
5
+
6
+ # You can add fixtures and/or initialization code here to make experimenting
7
+ # with your gem easier. You can also use a different console, if you like.
8
+
9
+ # (If you use this, don't forget to add pry to your Gemfile!)
10
+ # require "pry"
11
+ # Pry.start
12
+
13
+ require "irb"
14
+ IRB.start
data/bin/setup ADDED
@@ -0,0 +1,7 @@
1
+ #!/bin/bash
2
+ set -euo pipefail
3
+ IFS=$'\n\t'
4
+
5
+ bundle install --jobs=3 --retry=3
6
+
7
+ # Do any other automated setup that you need to do here
@@ -0,0 +1,26 @@
1
+ # coding: utf-8
2
+ lib = File.expand_path('../lib', __FILE__)
3
+ $LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
4
+ require 'jbuilder/pagination/version'
5
+
6
+ Gem::Specification.new do |spec|
7
+ spec.name = "jbuilder_pagination_plus"
8
+ spec.version = Jbuilder::Pagination::VERSION
9
+ spec.authors = ["Bruno Bacarini", "Andrew Newell"]
10
+ spec.email = ["bacarini.bruno@gmail.com", "scytherswings@gmail.com"]
11
+
12
+ spec.summary = "Jbuilder extension to allows pagination according to JSON API format"
13
+ spec.description = "Jbuilder extension to allows pagination according to JSON API format. See http://jsonapi.org for details on the format. Also provides methods for simpler pagination in controllers"
14
+ spec.homepage = "https://github.com/PinsterTeam/jbuilder_pagination_plus"
15
+ spec.license = "MIT"
16
+
17
+ spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
18
+ spec.bindir = "exe"
19
+ spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
20
+ spec.require_paths = ["lib"]
21
+
22
+ spec.add_runtime_dependency 'jbuilder', '~> 2.2', '>= 2.2.0'
23
+ spec.add_development_dependency 'rspec', '~> 3.2', '>= 3.2.0'
24
+ spec.add_development_dependency 'rake', '~> 10.0'
25
+ spec.add_development_dependency 'pry', '~> 0'
26
+ end
@@ -0,0 +1,25 @@
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
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ class UnpageableResourceError < StandardError
4
+ def initialize(message)
5
+ super(message)
6
+ end
7
+ end
@@ -0,0 +1,41 @@
1
+ class Jbuilder
2
+ ONE_PAGE = 1
3
+
4
+ def pages!(collection, options={})
5
+ return unless collection && is_paginated?(collection)
6
+
7
+ pages_from(collection).map do |key, value|
8
+ params = query_parameters(options).merge(page: { number: value, size: collection.size }).to_query
9
+ _set_value key, "#{options.fetch(:url, nil)}?#{params}"
10
+ end
11
+ end
12
+
13
+ private
14
+
15
+ def pages_from(collection)
16
+ {}.tap do |pages|
17
+ pages[:self] = collection.current_page
18
+ return pages if collection.total_pages == ONE_PAGE
19
+
20
+ unless collection.current_page == ONE_PAGE
21
+ pages[:first] = ONE_PAGE
22
+ pages[:prev] = collection.current_page - ONE_PAGE
23
+ end
24
+
25
+ unless collection.current_page == collection.total_pages
26
+ pages[:next] = collection.current_page + ONE_PAGE
27
+ pages[:last] = collection.total_pages
28
+ end
29
+ end
30
+ end
31
+
32
+ def query_parameters(options)
33
+ @query_parameters ||= options.fetch(:query_parameters, {})
34
+ end
35
+
36
+ def is_paginated?(collection)
37
+ collection.respond_to?(:current_page) &&
38
+ collection.respond_to?(:total_pages) &&
39
+ collection.respond_to?(:size)
40
+ end
41
+ end
@@ -0,0 +1,5 @@
1
+ module Jbuilder
2
+ class Pagination
3
+ VERSION = "0.0.1"
4
+ end
5
+ end
@@ -0,0 +1,3 @@
1
+ require "jbuilder"
2
+ require "jbuilder/pagination/pages"
3
+
metadata ADDED
@@ -0,0 +1,134 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: jbuilder_pagination_plus
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Bruno Bacarini
8
+ - Andrew Newell
9
+ autorequire:
10
+ bindir: exe
11
+ cert_chain: []
12
+ date: 2018-05-17 00:00:00.000000000 Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: jbuilder
16
+ requirement: !ruby/object:Gem::Requirement
17
+ requirements:
18
+ - - "~>"
19
+ - !ruby/object:Gem::Version
20
+ version: '2.2'
21
+ - - ">="
22
+ - !ruby/object:Gem::Version
23
+ version: 2.2.0
24
+ type: :runtime
25
+ prerelease: false
26
+ version_requirements: !ruby/object:Gem::Requirement
27
+ requirements:
28
+ - - "~>"
29
+ - !ruby/object:Gem::Version
30
+ version: '2.2'
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: 2.2.0
34
+ - !ruby/object:Gem::Dependency
35
+ name: rspec
36
+ requirement: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '3.2'
41
+ - - ">="
42
+ - !ruby/object:Gem::Version
43
+ version: 3.2.0
44
+ type: :development
45
+ prerelease: false
46
+ version_requirements: !ruby/object:Gem::Requirement
47
+ requirements:
48
+ - - "~>"
49
+ - !ruby/object:Gem::Version
50
+ version: '3.2'
51
+ - - ">="
52
+ - !ruby/object:Gem::Version
53
+ version: 3.2.0
54
+ - !ruby/object:Gem::Dependency
55
+ name: rake
56
+ requirement: !ruby/object:Gem::Requirement
57
+ requirements:
58
+ - - "~>"
59
+ - !ruby/object:Gem::Version
60
+ version: '10.0'
61
+ type: :development
62
+ prerelease: false
63
+ version_requirements: !ruby/object:Gem::Requirement
64
+ requirements:
65
+ - - "~>"
66
+ - !ruby/object:Gem::Version
67
+ version: '10.0'
68
+ - !ruby/object:Gem::Dependency
69
+ name: pry
70
+ requirement: !ruby/object:Gem::Requirement
71
+ requirements:
72
+ - - "~>"
73
+ - !ruby/object:Gem::Version
74
+ version: '0'
75
+ type: :development
76
+ prerelease: false
77
+ version_requirements: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - "~>"
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ description: Jbuilder extension to allows pagination according to JSON API format.
83
+ See http://jsonapi.org for details on the format. Also provides methods for simpler
84
+ pagination in controllers
85
+ email:
86
+ - bacarini.bruno@gmail.com
87
+ - scytherswings@gmail.com
88
+ executables: []
89
+ extensions: []
90
+ extra_rdoc_files: []
91
+ files:
92
+ - ".gitignore"
93
+ - ".rspec"
94
+ - ".ruby-gemset"
95
+ - ".ruby-version"
96
+ - ".travis.yml"
97
+ - CODE_OF_CONDUCT.md
98
+ - Gemfile
99
+ - LICENSE.txt
100
+ - README.md
101
+ - Rakefile
102
+ - bin/console
103
+ - bin/setup
104
+ - jbuilder_pagination_plus.gemspec
105
+ - lib/concerns/pagination.rb
106
+ - lib/exceptions/unpageable_resource_error.rb
107
+ - lib/jbuilder/pagination.rb
108
+ - lib/jbuilder/pagination/pages.rb
109
+ - lib/jbuilder/pagination/version.rb
110
+ homepage: https://github.com/PinsterTeam/jbuilder_pagination_plus
111
+ licenses:
112
+ - MIT
113
+ metadata: {}
114
+ post_install_message:
115
+ rdoc_options: []
116
+ require_paths:
117
+ - lib
118
+ required_ruby_version: !ruby/object:Gem::Requirement
119
+ requirements:
120
+ - - ">="
121
+ - !ruby/object:Gem::Version
122
+ version: '0'
123
+ required_rubygems_version: !ruby/object:Gem::Requirement
124
+ requirements:
125
+ - - ">="
126
+ - !ruby/object:Gem::Version
127
+ version: '0'
128
+ requirements: []
129
+ rubyforge_project:
130
+ rubygems_version: 2.6.14
131
+ signing_key:
132
+ specification_version: 4
133
+ summary: Jbuilder extension to allows pagination according to JSON API format
134
+ test_files: []