yaqb 0.5.0 → 0.5.1
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/Gemfile.lock +1 -1
- data/README.md +78 -4
- data/lib/yaqb/version.rb +1 -1
- data/yaqb.gemspec +2 -0
- metadata +3 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 21e2ed937f7b14a5ab0dfcb64a183e1d4cc127ae194fc38482e1dec50b217a08
|
4
|
+
data.tar.gz: 924a57354b4b4e6934aaaf3455aa4fab0aec368c5f2fae1085c59a49344d3648
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7bdfcdf4eede2b9df3e2520a64d825346c58b121e0dbd0495af39d4f9601232594090a770928bd956614e952fe2790e707c45675f9ce35daf3ba67d8961f9834
|
7
|
+
data.tar.gz: abca46cd2bfbd8e02bfbbfe5e8f31313e0511e29f70448cffb95c55b0720e79cc886a758e245ac643b5fde4d79539ac936b84a94eb1f86d61674c601c7592ed7
|
data/Gemfile.lock
CHANGED
data/README.md
CHANGED
@@ -1,14 +1,21 @@
|
|
1
1
|
# Yaqb
|
2
2
|
|
3
|
-
|
3
|
+
#### Yet Another Query Builder
|
4
|
+
Yaqb is query builder that will filter, sort and paginate your ActiveRecord collections.
|
4
5
|
|
5
|
-
|
6
|
+
In addition to query building, presenters are used to manage what a consumer of your API can query.
|
6
7
|
|
7
8
|
## Installation
|
8
9
|
|
9
|
-
|
10
|
+
In your Gemfile
|
10
11
|
|
11
12
|
```ruby
|
13
|
+
# Choose your preferred pagination gem
|
14
|
+
gem 'kaminari' # or
|
15
|
+
gem 'will_paginate' # or
|
16
|
+
gem 'pagy'
|
17
|
+
|
18
|
+
# Then add
|
12
19
|
gem 'yaqb'
|
13
20
|
```
|
14
21
|
|
@@ -20,9 +27,76 @@ Or install it yourself as:
|
|
20
27
|
|
21
28
|
$ gem install yaqb
|
22
29
|
|
30
|
+
## Configuration
|
31
|
+
|
32
|
+
By default, Yaqb will detect if you are using Kaminari, WillPaginate, or Pagy. If you want to change any of the configurable settings, you may do so:
|
33
|
+
|
34
|
+
```ruby
|
35
|
+
Yaqb.configure do |config|
|
36
|
+
# If for whatever reason you are using multiple pagination gems, you can manually set which gem to use.
|
37
|
+
config.paginator = :kaminari # :will_paginate, :pagy
|
38
|
+
end
|
39
|
+
```
|
40
|
+
|
23
41
|
## Usage
|
24
42
|
|
25
|
-
|
43
|
+
### Controllers
|
44
|
+
|
45
|
+
In your controller, you will need to include the following the module: `Yaqb::Base`
|
46
|
+
|
47
|
+
```ruby
|
48
|
+
class ApplicationController < ActionController::Base # or ActionController::API
|
49
|
+
include Yaqb::Base
|
50
|
+
end
|
51
|
+
|
52
|
+
# And then to use Yaqb
|
53
|
+
|
54
|
+
class BooksController < ApplicationController
|
55
|
+
def index
|
56
|
+
# #orchestrate expects a collection and a presenter.
|
57
|
+
# #orchestrate return a results object meaning you will have access to the following methods:
|
58
|
+
# #scope - This is your collection after being filtered, sorted and paginated
|
59
|
+
# #links - This returns a hash of links based on the paginated results.
|
60
|
+
result = orchestrate(Books.all, BookPresenter)
|
61
|
+
|
62
|
+
# Depending on your serializer you can render your data as so:
|
63
|
+
# BluePrinter gem by procore is being here:
|
64
|
+
render json: BookBlueprint.render(request.scope, root: :data, meta: { links: request.links })
|
65
|
+
end
|
66
|
+
end
|
67
|
+
```
|
68
|
+
|
69
|
+
### Presenters
|
70
|
+
|
71
|
+
Presenters will allow you to control what a consumer of your API can sort and filter by.
|
72
|
+
|
73
|
+
Your Presenter class will need to inherit from `Yaqb::Presenter`
|
74
|
+
|
75
|
+
```ruby
|
76
|
+
class BookPresenter < Yaqb::Presenter
|
77
|
+
sort_by :id, :title, :created_at, :updated_at
|
78
|
+
filter_by :id, :title
|
79
|
+
end
|
80
|
+
```
|
81
|
+
|
82
|
+
### Handling Errors
|
83
|
+
|
84
|
+
By default Yaqb will rescue from `QueryBuilderError` and return that error to the consumer
|
85
|
+
|
86
|
+
Given the following API call:
|
87
|
+
|
88
|
+
`GET https://api.example.com/v1/books?per=a`
|
89
|
+
|
90
|
+
The following response will be returned
|
91
|
+
|
92
|
+
```json
|
93
|
+
{
|
94
|
+
"error": {
|
95
|
+
"message": "Invalid pagination params. Only numbers are supported for \"page\" and \"per\"",
|
96
|
+
"invalid_params": "per=a"
|
97
|
+
}
|
98
|
+
}
|
99
|
+
```
|
26
100
|
|
27
101
|
## Development
|
28
102
|
|
data/lib/yaqb/version.rb
CHANGED
data/yaqb.gemspec
CHANGED
@@ -19,6 +19,8 @@ Gem::Specification.new do |spec|
|
|
19
19
|
|
20
20
|
spec.required_ruby_version = '>= 2.3.0'
|
21
21
|
spec.requirements << 'Kaminari'
|
22
|
+
spec.requirements << 'WillPaginate'
|
23
|
+
spec.requirements << 'Pagy'
|
22
24
|
|
23
25
|
# Specify which files should be added to the gem when it is released.
|
24
26
|
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: yaqb
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ronald Chacon
|
@@ -122,6 +122,8 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
122
122
|
version: '0'
|
123
123
|
requirements:
|
124
124
|
- Kaminari
|
125
|
+
- WillPaginate
|
126
|
+
- Pagy
|
125
127
|
rubygems_version: 3.0.3
|
126
128
|
signing_key:
|
127
129
|
specification_version: 4
|