rocket_pants-pagination 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +9 -0
- data/.rspec +2 -0
- data/.travis.yml +4 -0
- data/Gemfile +4 -0
- data/LICENSE.txt +21 -0
- data/README.md +51 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +7 -0
- data/lib/rocket_pants/pagination/version.rb +5 -0
- data/lib/rocket_pants/pagination.rb +115 -0
- data/rocket_pants-pagination.gemspec +29 -0
- metadata +154 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 5fc7bbf163f232c569ae8c590d8b61da5ca93f80
|
4
|
+
data.tar.gz: 46c661cc0911c65473c2b6eb1ce2155108bd39e6
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: d1f9906f54492e7392cb1d99a2d9896792924924446f61e16560c67b16e077c526ac6f659652b94cce9164bab31208a163bec43aa3192930cdd179433781dc1e
|
7
|
+
data.tar.gz: cace7216edea2bfb1a79311c7429d12fbdb3272dce32d64073c7a77c22d1d484b9e9ede3eab8d226fe56e79b5642868ed22d3cd220c837377b8554d22339526f
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/Gemfile
ADDED
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2015 Alessandro Desantis
|
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,51 @@
|
|
1
|
+
# RocketPants::Pagination
|
2
|
+
|
3
|
+
Pagination support for [RocketPants](https://github.com/Sutto/rocket_pants).
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
Add this line to your application's Gemfile:
|
8
|
+
|
9
|
+
```ruby gem 'rocket_pants-pagination' ```
|
10
|
+
|
11
|
+
And then execute:
|
12
|
+
|
13
|
+
$ bundle
|
14
|
+
|
15
|
+
Or install it yourself as:
|
16
|
+
|
17
|
+
$ gem install rocket_pants-pagination
|
18
|
+
|
19
|
+
## Usage
|
20
|
+
|
21
|
+
```ruby
|
22
|
+
class PostsController < RocketPants::Base
|
23
|
+
include RocketPants::Pagination
|
24
|
+
|
25
|
+
def index
|
26
|
+
@posts = Post.all
|
27
|
+
paginate_and_expose posts: @posts
|
28
|
+
end
|
29
|
+
end
|
30
|
+
```
|
31
|
+
|
32
|
+
## Development
|
33
|
+
|
34
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run
|
35
|
+
`rake spec` to run the tests. You can also run `bin/console` for an interactive
|
36
|
+
prompt that will allow you to experiment.
|
37
|
+
|
38
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To
|
39
|
+
release a new version, update the version number in `version.rb`, and then run
|
40
|
+
`bundle exec rake release`, which will create a git tag for the version, push
|
41
|
+
git commits and tags, and push the `.gem` file to
|
42
|
+
[rubygems.org](https://rubygems.org).
|
43
|
+
|
44
|
+
## Contributing
|
45
|
+
|
46
|
+
Bug reports and pull requests are welcome on GitHub at
|
47
|
+
[https://github.com/alessandro1997/rocket_pants-pagination](https://github.com/alessandro1997/rocket_pants-pagination).
|
48
|
+
|
49
|
+
## License
|
50
|
+
|
51
|
+
The gem is available as open source under the terms of the [MIT License](http://opensource.org/licenses/MIT).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "rocket_pants/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,115 @@
|
|
1
|
+
require 'rocket_pants/pagination/version'
|
2
|
+
|
3
|
+
module RocketPants
|
4
|
+
#
|
5
|
+
# Pagination support for RocketPants
|
6
|
+
#
|
7
|
+
# Include this module in your RocketPants controllers to add pagination
|
8
|
+
# support.
|
9
|
+
#
|
10
|
+
# @author Alessandro Desantis <desa.alessandro@gmail.com>
|
11
|
+
#
|
12
|
+
module Pagination
|
13
|
+
#
|
14
|
+
# Reserved root keys
|
15
|
+
#
|
16
|
+
RESERVED_PAGINATION_KEYS = [:count, :pagination]
|
17
|
+
|
18
|
+
#
|
19
|
+
# Paginates the given relation.
|
20
|
+
#
|
21
|
+
# The page param is 'page' by default.
|
22
|
+
#
|
23
|
+
# @param [ActiveRecord::Relation] the relation to paginate
|
24
|
+
# @param [Hash] an options hash for the #paginate method
|
25
|
+
#
|
26
|
+
# @return [ActiveRecord::Relation] a paginated relation
|
27
|
+
#
|
28
|
+
def paginate(relation, options = {})
|
29
|
+
relation.paginate({ page: params[:page] }.merge(options))
|
30
|
+
end
|
31
|
+
|
32
|
+
#
|
33
|
+
# Exposes the given collection with pagination metadata.
|
34
|
+
#
|
35
|
+
# @param [Hash{Symbol => ActiveRecord::Relation}] a hash with exactly one
|
36
|
+
# element, its key being the root key and its value a paginated relation
|
37
|
+
#
|
38
|
+
# @raise [ArgumentError] if the hash is malformed
|
39
|
+
#
|
40
|
+
def expose_with_pagination(hash)
|
41
|
+
root_key, collection = extract_pagination_elements_from hash
|
42
|
+
|
43
|
+
response = {
|
44
|
+
"#{root_key}" => ActiveModel::ArraySerializer.new(collection),
|
45
|
+
count: collection.count,
|
46
|
+
pagination: {
|
47
|
+
pages: collection.total_pages,
|
48
|
+
current: collection.current_page,
|
49
|
+
count: collection.count,
|
50
|
+
per_page: collection.per_page,
|
51
|
+
previous: collection.previous_page,
|
52
|
+
next: collection.next_page
|
53
|
+
}
|
54
|
+
}
|
55
|
+
|
56
|
+
expose response
|
57
|
+
end
|
58
|
+
|
59
|
+
#
|
60
|
+
# Paginates and exposes the given collection with pagination metadata.
|
61
|
+
#
|
62
|
+
# Basically, just calls {#paginate}, then {#expose_with_pagination}.
|
63
|
+
#
|
64
|
+
# @param [Hash(Symbol => ActiveRecord::Relation)] a hash with exactly one
|
65
|
+
# element, its key being the root key and its value a paginated relation
|
66
|
+
#
|
67
|
+
# @raise [ArgumentError] if the hash is malformed
|
68
|
+
#
|
69
|
+
def paginate_and_expose(hash)
|
70
|
+
root_key, collection = extract_pagination_elements_from hash
|
71
|
+
expose_with_pagination "#{root_key}" => paginate(collection)
|
72
|
+
end
|
73
|
+
|
74
|
+
private
|
75
|
+
|
76
|
+
#
|
77
|
+
# Returns the first pair from the given hash.
|
78
|
+
#
|
79
|
+
# This is used to extract the root key and collection from a hash.
|
80
|
+
#
|
81
|
+
# @param [Hash(Symbol => ActiveRecord::Relation)] a hash with exactly one
|
82
|
+
# element, its key being the root key and its value a relation
|
83
|
+
#
|
84
|
+
# @return [Array(Symbol, ActiveRecord::Relation)] an array containing a root
|
85
|
+
# key and a relation
|
86
|
+
#
|
87
|
+
# @raise [ArgumentError] if the hash is malformed
|
88
|
+
#
|
89
|
+
def extract_pagination_elements_from(hash)
|
90
|
+
validate_pagination_hash! hash
|
91
|
+
hash.first
|
92
|
+
end
|
93
|
+
|
94
|
+
#
|
95
|
+
# Validates the given hash.
|
96
|
+
#
|
97
|
+
# Ensures that the hash has exactly one element and that the first element's
|
98
|
+
# key is not a reserved one.
|
99
|
+
#
|
100
|
+
# @param [Hash] a hash to validate
|
101
|
+
#
|
102
|
+
# @raise [ArgumentError] if the hash doesn't have exactly 1 element
|
103
|
+
# @raise [ArgumentError] if the first element's key is a reserved one
|
104
|
+
#
|
105
|
+
def validate_pagination_hash!(hash)
|
106
|
+
if hash.size != 1
|
107
|
+
raise ArgumentError, "the given hash should contain exactly 1 element (#{hash.size} elements)"
|
108
|
+
end
|
109
|
+
|
110
|
+
if hash.keys.first.to_sym.in?(RESERVED_PAGINATION_KEYS)
|
111
|
+
raise ArgumentError, "the given root key is reserved: #{hash.keys.first}"
|
112
|
+
end
|
113
|
+
end
|
114
|
+
end
|
115
|
+
end
|
@@ -0,0 +1,29 @@
|
|
1
|
+
# coding: utf-8
|
2
|
+
lib = File.expand_path('../lib', __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require 'rocket_pants/pagination/version'
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = 'rocket_pants-pagination'
|
8
|
+
spec.version = RocketPants::Pagination::VERSION
|
9
|
+
spec.authors = ['Alessandro Desantis']
|
10
|
+
spec.email = ['desa.alessandro@gmail.com']
|
11
|
+
|
12
|
+
spec.summary = %q{Pagination support for RocketPants.}
|
13
|
+
spec.homepage = 'https://github.com/alessandro1997/rocket_pants-pagination'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.files = `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
17
|
+
spec.bindir = 'exe'
|
18
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
19
|
+
spec.require_paths = ['lib']
|
20
|
+
|
21
|
+
spec.add_dependency 'rocket_pants', '~> 1.13'
|
22
|
+
spec.add_dependency 'will_paginate', '~> 3.0'
|
23
|
+
spec.add_dependency 'active_model_serializers', '~> 0.9'
|
24
|
+
|
25
|
+
spec.add_development_dependency 'bundler', '~> 1.10'
|
26
|
+
spec.add_development_dependency 'rake', '~> 10.0'
|
27
|
+
spec.add_development_dependency 'rspec'
|
28
|
+
spec.add_development_dependency 'fuubar'
|
29
|
+
end
|
metadata
ADDED
@@ -0,0 +1,154 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: rocket_pants-pagination
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Alessandro Desantis
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2015-08-22 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: rocket_pants
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.13'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.13'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: will_paginate
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '3.0'
|
34
|
+
type: :runtime
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '3.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: active_model_serializers
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '0.9'
|
48
|
+
type: :runtime
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0.9'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: bundler
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '1.10'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '1.10'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rake
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '10.0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '10.0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: rspec
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - ">="
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0'
|
90
|
+
type: :development
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - ">="
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: fuubar
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :development
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description:
|
112
|
+
email:
|
113
|
+
- desa.alessandro@gmail.com
|
114
|
+
executables: []
|
115
|
+
extensions: []
|
116
|
+
extra_rdoc_files: []
|
117
|
+
files:
|
118
|
+
- ".gitignore"
|
119
|
+
- ".rspec"
|
120
|
+
- ".travis.yml"
|
121
|
+
- Gemfile
|
122
|
+
- LICENSE.txt
|
123
|
+
- README.md
|
124
|
+
- Rakefile
|
125
|
+
- bin/console
|
126
|
+
- bin/setup
|
127
|
+
- lib/rocket_pants/pagination.rb
|
128
|
+
- lib/rocket_pants/pagination/version.rb
|
129
|
+
- rocket_pants-pagination.gemspec
|
130
|
+
homepage: https://github.com/alessandro1997/rocket_pants-pagination
|
131
|
+
licenses:
|
132
|
+
- MIT
|
133
|
+
metadata: {}
|
134
|
+
post_install_message:
|
135
|
+
rdoc_options: []
|
136
|
+
require_paths:
|
137
|
+
- lib
|
138
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
139
|
+
requirements:
|
140
|
+
- - ">="
|
141
|
+
- !ruby/object:Gem::Version
|
142
|
+
version: '0'
|
143
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
144
|
+
requirements:
|
145
|
+
- - ">="
|
146
|
+
- !ruby/object:Gem::Version
|
147
|
+
version: '0'
|
148
|
+
requirements: []
|
149
|
+
rubyforge_project:
|
150
|
+
rubygems_version: 2.4.8
|
151
|
+
signing_key:
|
152
|
+
specification_version: 4
|
153
|
+
summary: Pagination support for RocketPants.
|
154
|
+
test_files: []
|