jsonapi-resources-optional_paginators 0.1.0
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 +7 -0
- data/.gitignore +11 -0
- data/.rspec +3 -0
- data/.travis.yml +6 -0
- data/Gemfile +7 -0
- data/LICENSE.txt +21 -0
- data/README.md +59 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/jsonapi-resources-optional_paginators.gemspec +27 -0
- data/lib/jsonapi/resources/optional_paginators.rb +14 -0
- data/lib/jsonapi/resources/optional_paginators/null_paginator.rb +19 -0
- data/lib/jsonapi/resources/optional_paginators/optional_paginator.rb +40 -0
- data/lib/jsonapi/resources/optional_paginators/version.rb +7 -0
- data/lib/optional_paged_paginator.rb +4 -0
- metadata +74 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: d01c0519bdca1e9ae06c94b54f5a2fb24558f521363892e69eb9f1e8a7e3a6a1
|
4
|
+
data.tar.gz: 47aa97a7a99c7cdbaad20152ce743e1e857a78761d47e792998fa62854cf059c
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: bf673b3a49af1a409ace776beb1412b4bb8b333e262e7421fadfb2566e10439da6bb2fc311109e8e130eee2ff12601f7f1a18ae245caeb6fb7aff1d47ebf8766
|
7
|
+
data.tar.gz: a73732cb519796d6cd3f2e494e3ae3a72e1cffe05999e08d9b79d2c201b72fe3d8bdfae3a1575fbd5b1d55f2e5d046f604ac8d22ceed243f5fd0e0ec4e908dfb
|
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) 2020 Josh Justice
|
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,59 @@
|
|
1
|
+
# JSONAPI::Resources::OptionalPaginators
|
2
|
+
|
3
|
+
JSONAPI::Resources paginators that allow returning records unpaginated.
|
4
|
+
|
5
|
+
JSONAPI::Resources allows you to configure resources to either have no pagination, or to have a paginator of a certain type. This often works fine.
|
6
|
+
|
7
|
+
But sometimes you may want the option to either paginate or not paginate a resource, depending on the request. For example, in a todo list app, your completed todo list will get longer over time, so you might want to paginate it. But your uncompleted todo list should stay short, so you might want to retrieve all incomplete todos unpaginated.
|
8
|
+
|
9
|
+
This gem allows you to make JR paginators optional, so that if you don't pass any `page[]` parameters, all records are returned. This is accomplished with an `OptionalPaginator` class that wraps any other paginator. For convenience, a wrapped version of the built-in `PagedPaginator` is provided as `OptionalPagedPaginator`.
|
10
|
+
|
11
|
+
## Installation
|
12
|
+
|
13
|
+
Add this line to your application's Gemfile:
|
14
|
+
|
15
|
+
```ruby
|
16
|
+
gem 'jsonapi-resources-optional_paginators'
|
17
|
+
```
|
18
|
+
|
19
|
+
And then execute:
|
20
|
+
|
21
|
+
$ bundle install
|
22
|
+
|
23
|
+
Or install it yourself as:
|
24
|
+
|
25
|
+
$ gem install jsonapi-resources-optional_paginators
|
26
|
+
|
27
|
+
## Usage
|
28
|
+
|
29
|
+
To use the `OptionalPagedPaginator`, configure it as the paginator for a resource or the default paginator for the app:
|
30
|
+
|
31
|
+
JSONAPI.configure do |config|
|
32
|
+
config.default_paginator = :optional_paged
|
33
|
+
end
|
34
|
+
|
35
|
+
To make another paginator class optional, call the `OptionalPaginator.for` method, passing the paginator class to wrap. It returns a new class, so assign it to a constant:
|
36
|
+
|
37
|
+
OptionalOffsetPaginator =
|
38
|
+
JSONAPI::Resources::OptionalPaginators::OptionalPaginator.for(OffsetPaginator)
|
39
|
+
|
40
|
+
Note that the class should not be nested inside a module, so that JR can find the class name based on the symbol passed to the config:
|
41
|
+
|
42
|
+
JSONAPI.configure do |config|
|
43
|
+
config.default_paginator = :optional_offset
|
44
|
+
end
|
45
|
+
|
46
|
+
## Development
|
47
|
+
|
48
|
+
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
49
|
+
|
50
|
+
To install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and tags, and push the `.gem` file to [rubygems.org](https://rubygems.org).
|
51
|
+
|
52
|
+
## Contributing
|
53
|
+
|
54
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/codingitwrong/jsonapi-resources-optional_paginators.
|
55
|
+
|
56
|
+
|
57
|
+
## License
|
58
|
+
|
59
|
+
The gem is available as open source under the terms of the [MIT License](https://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 "jsonapi/resources/optional_paginators"
|
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(__FILE__)
|
data/bin/setup
ADDED
@@ -0,0 +1,27 @@
|
|
1
|
+
require_relative 'lib/jsonapi/resources/optional_paginators/version'
|
2
|
+
|
3
|
+
Gem::Specification.new do |spec|
|
4
|
+
spec.name = 'jsonapi-resources-optional_paginators'
|
5
|
+
spec.version = JSONAPI::Resources::OptionalPaginators::VERSION
|
6
|
+
spec.authors = ['Josh Justice']
|
7
|
+
spec.email = ['me@codingitwrong.com']
|
8
|
+
|
9
|
+
spec.summary = 'JR paginators that allow returning records unpaginated.'
|
10
|
+
spec.homepage = 'https://github.com/CodingItWrong/jsonapi-resources-optional_paginators'
|
11
|
+
spec.license = 'MIT'
|
12
|
+
spec.required_ruby_version = Gem::Requirement.new('>= 2.3.0')
|
13
|
+
|
14
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
15
|
+
spec.metadata['source_code_uri'] = 'https://github.com/CodingItWrong/jsonapi-resources-optional_paginators.git'
|
16
|
+
|
17
|
+
# Specify which files should be added to the gem when it is released.
|
18
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
19
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
20
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
21
|
+
end
|
22
|
+
spec.bindir = 'exe'
|
23
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
24
|
+
spec.require_paths = ['lib']
|
25
|
+
|
26
|
+
spec.add_runtime_dependency 'jsonapi-resources', ['>= 0']
|
27
|
+
end
|
@@ -0,0 +1,14 @@
|
|
1
|
+
require 'jsonapi/resources/optional_paginators/version'
|
2
|
+
|
3
|
+
require_relative 'optional_paginators/null_paginator'
|
4
|
+
require_relative 'optional_paginators/optional_paginator'
|
5
|
+
require_relative '../../optional_paged_paginator'
|
6
|
+
|
7
|
+
module JSONAPI
|
8
|
+
module Resources
|
9
|
+
module OptionalPaginators
|
10
|
+
class Error < StandardError; end
|
11
|
+
# Your code goes here...
|
12
|
+
end
|
13
|
+
end
|
14
|
+
end
|
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module JSONAPI
|
4
|
+
module Resources
|
5
|
+
module OptionalPaginators
|
6
|
+
class NullPaginator < JSONAPI::Paginator
|
7
|
+
def initialize(params); end
|
8
|
+
|
9
|
+
def apply(relation, _order_options)
|
10
|
+
relation
|
11
|
+
end
|
12
|
+
|
13
|
+
def calculate_page_count(_record_count)
|
14
|
+
1
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require_relative 'null_paginator'
|
4
|
+
|
5
|
+
module JSONAPI
|
6
|
+
module Resources
|
7
|
+
module OptionalPaginators
|
8
|
+
class OptionalPaginator < SimpleDelegator
|
9
|
+
class << self
|
10
|
+
attr_accessor :wrapped_class
|
11
|
+
|
12
|
+
def for(paginator_class_argument)
|
13
|
+
Class.new(self).tap do |wrapper_class|
|
14
|
+
wrapper_class.wrapped_class = paginator_class_argument
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
attr_reader :delegate
|
20
|
+
|
21
|
+
def self.requires_record_count
|
22
|
+
wrapped_class.requires_record_count
|
23
|
+
end
|
24
|
+
|
25
|
+
def initialize(params)
|
26
|
+
inner_paginator = paginator_for_params(params).new(params)
|
27
|
+
super(inner_paginator)
|
28
|
+
end
|
29
|
+
|
30
|
+
def paginator_for_params(params)
|
31
|
+
if params.nil?
|
32
|
+
NullPaginator
|
33
|
+
else
|
34
|
+
self.class.wrapped_class
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
metadata
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: jsonapi-resources-optional_paginators
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Josh Justice
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2020-07-20 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: jsonapi-resources
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '0'
|
20
|
+
type: :runtime
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - ">="
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '0'
|
27
|
+
description:
|
28
|
+
email:
|
29
|
+
- me@codingitwrong.com
|
30
|
+
executables: []
|
31
|
+
extensions: []
|
32
|
+
extra_rdoc_files: []
|
33
|
+
files:
|
34
|
+
- ".gitignore"
|
35
|
+
- ".rspec"
|
36
|
+
- ".travis.yml"
|
37
|
+
- Gemfile
|
38
|
+
- LICENSE.txt
|
39
|
+
- README.md
|
40
|
+
- Rakefile
|
41
|
+
- bin/console
|
42
|
+
- bin/setup
|
43
|
+
- jsonapi-resources-optional_paginators.gemspec
|
44
|
+
- lib/jsonapi/resources/optional_paginators.rb
|
45
|
+
- lib/jsonapi/resources/optional_paginators/null_paginator.rb
|
46
|
+
- lib/jsonapi/resources/optional_paginators/optional_paginator.rb
|
47
|
+
- lib/jsonapi/resources/optional_paginators/version.rb
|
48
|
+
- lib/optional_paged_paginator.rb
|
49
|
+
homepage: https://github.com/CodingItWrong/jsonapi-resources-optional_paginators
|
50
|
+
licenses:
|
51
|
+
- MIT
|
52
|
+
metadata:
|
53
|
+
homepage_uri: https://github.com/CodingItWrong/jsonapi-resources-optional_paginators
|
54
|
+
source_code_uri: https://github.com/CodingItWrong/jsonapi-resources-optional_paginators.git
|
55
|
+
post_install_message:
|
56
|
+
rdoc_options: []
|
57
|
+
require_paths:
|
58
|
+
- lib
|
59
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
60
|
+
requirements:
|
61
|
+
- - ">="
|
62
|
+
- !ruby/object:Gem::Version
|
63
|
+
version: 2.3.0
|
64
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
requirements: []
|
70
|
+
rubygems_version: 3.1.2
|
71
|
+
signing_key:
|
72
|
+
specification_version: 4
|
73
|
+
summary: JR paginators that allow returning records unpaginated.
|
74
|
+
test_files: []
|