yaqb 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 +11 -0
- data/.rspec +3 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +4 -0
- data/Gemfile.lock +93 -0
- data/LICENSE.txt +21 -0
- data/README.md +43 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/yaqb/configuration.rb +30 -0
- data/lib/yaqb/errors/query_builder_error.rb +12 -0
- data/lib/yaqb/hooks.rb +24 -0
- data/lib/yaqb/query_builder.rb +22 -0
- data/lib/yaqb/query_builders/filter.rb +87 -0
- data/lib/yaqb/query_builders/paginate.rb +64 -0
- data/lib/yaqb/query_builders/query_orchestrator.rb +44 -0
- data/lib/yaqb/query_builders/sort.rb +21 -0
- data/lib/yaqb/railtie.rb +11 -0
- data/lib/yaqb/version.rb +3 -0
- data/lib/yaqb.rb +19 -0
- data/yaqb.gemspec +34 -0
- metadata +139 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA256:
|
3
|
+
metadata.gz: 4f43be6e7b9eade59ab0beed42e93b7a8328a97493b6d9ffd82dd9937761a97d
|
4
|
+
data.tar.gz: f22b1488986b51be238f6b6fcd907f7f36a217c0a7845aa999e00e3b4e1ea932
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 9b9d0dc7d1b4301bbea1141312eb8cc1d0208f3d0ebb13b2485e478211d06a2dc2f8f8c334ed5ad67644288f9cde3c372e1b6513121ea8266eb6435306bc11dd
|
7
|
+
data.tar.gz: 546f2fe9f9f781e5c1c17ade93ca543611ce5d6c48839e2541a6f45717d4e959908013bff2f3c0605f5f571d8a0b024fb8a3b0dc6a8dc5b2b31ec9161aa48460
|
data/.gitignore
ADDED
data/.rspec
ADDED
data/.travis.yml
ADDED
data/CODE_OF_CONDUCT.md
ADDED
@@ -0,0 +1,74 @@
|
|
1
|
+
# Contributor Covenant Code of Conduct
|
2
|
+
|
3
|
+
## Our Pledge
|
4
|
+
|
5
|
+
In the interest of fostering an open and welcoming environment, we as
|
6
|
+
contributors and maintainers pledge to making participation in our project and
|
7
|
+
our community a harassment-free experience for everyone, regardless of age, body
|
8
|
+
size, disability, ethnicity, gender identity and expression, level of experience,
|
9
|
+
nationality, personal appearance, race, religion, or sexual identity and
|
10
|
+
orientation.
|
11
|
+
|
12
|
+
## Our Standards
|
13
|
+
|
14
|
+
Examples of behavior that contributes to creating a positive environment
|
15
|
+
include:
|
16
|
+
|
17
|
+
* Using welcoming and inclusive language
|
18
|
+
* Being respectful of differing viewpoints and experiences
|
19
|
+
* Gracefully accepting constructive criticism
|
20
|
+
* Focusing on what is best for the community
|
21
|
+
* Showing empathy towards other community members
|
22
|
+
|
23
|
+
Examples of unacceptable behavior by participants include:
|
24
|
+
|
25
|
+
* The use of sexualized language or imagery and unwelcome sexual attention or
|
26
|
+
advances
|
27
|
+
* Trolling, insulting/derogatory comments, and personal or political attacks
|
28
|
+
* Public or private harassment
|
29
|
+
* Publishing others' private information, such as a physical or electronic
|
30
|
+
address, without explicit permission
|
31
|
+
* Other conduct which could reasonably be considered inappropriate in a
|
32
|
+
professional setting
|
33
|
+
|
34
|
+
## Our Responsibilities
|
35
|
+
|
36
|
+
Project maintainers are responsible for clarifying the standards of acceptable
|
37
|
+
behavior and are expected to take appropriate and fair corrective action in
|
38
|
+
response to any instances of unacceptable behavior.
|
39
|
+
|
40
|
+
Project maintainers have the right and responsibility to remove, edit, or
|
41
|
+
reject comments, commits, code, wiki edits, issues, and other contributions
|
42
|
+
that are not aligned to this Code of Conduct, or to ban temporarily or
|
43
|
+
permanently any contributor for other behaviors that they deem inappropriate,
|
44
|
+
threatening, offensive, or harmful.
|
45
|
+
|
46
|
+
## Scope
|
47
|
+
|
48
|
+
This Code of Conduct applies both within project spaces and in public spaces
|
49
|
+
when an individual is representing the project or its community. Examples of
|
50
|
+
representing a project or community include using an official project e-mail
|
51
|
+
address, posting via an official social media account, or acting as an appointed
|
52
|
+
representative at an online or offline event. Representation of a project may be
|
53
|
+
further defined and clarified by project maintainers.
|
54
|
+
|
55
|
+
## Enforcement
|
56
|
+
|
57
|
+
Instances of abusive, harassing, or otherwise unacceptable behavior may be
|
58
|
+
reported by contacting the project team at ronald.chacon87@gmail.com. All
|
59
|
+
complaints will be reviewed and investigated and will result in a response that
|
60
|
+
is deemed necessary and appropriate to the circumstances. The project team is
|
61
|
+
obligated to maintain confidentiality with regard to the reporter of an incident.
|
62
|
+
Further details of specific enforcement policies may be posted separately.
|
63
|
+
|
64
|
+
Project maintainers who do not follow or enforce the Code of Conduct in good
|
65
|
+
faith may face temporary or permanent repercussions as determined by other
|
66
|
+
members of the project's leadership.
|
67
|
+
|
68
|
+
## Attribution
|
69
|
+
|
70
|
+
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4,
|
71
|
+
available at [http://contributor-covenant.org/version/1/4][version]
|
72
|
+
|
73
|
+
[homepage]: http://contributor-covenant.org
|
74
|
+
[version]: http://contributor-covenant.org/version/1/4/
|
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,93 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
yaqb (0.1.0)
|
5
|
+
|
6
|
+
GEM
|
7
|
+
remote: https://rubygems.org/
|
8
|
+
specs:
|
9
|
+
actionpack (6.0.0)
|
10
|
+
actionview (= 6.0.0)
|
11
|
+
activesupport (= 6.0.0)
|
12
|
+
rack (~> 2.0)
|
13
|
+
rack-test (>= 0.6.3)
|
14
|
+
rails-dom-testing (~> 2.0)
|
15
|
+
rails-html-sanitizer (~> 1.0, >= 1.2.0)
|
16
|
+
actionview (6.0.0)
|
17
|
+
activesupport (= 6.0.0)
|
18
|
+
builder (~> 3.1)
|
19
|
+
erubi (~> 1.4)
|
20
|
+
rails-dom-testing (~> 2.0)
|
21
|
+
rails-html-sanitizer (~> 1.1, >= 1.2.0)
|
22
|
+
activesupport (6.0.0)
|
23
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
24
|
+
i18n (>= 0.7, < 2)
|
25
|
+
minitest (~> 5.1)
|
26
|
+
tzinfo (~> 1.1)
|
27
|
+
zeitwerk (~> 2.1, >= 2.1.8)
|
28
|
+
builder (3.2.3)
|
29
|
+
coderay (1.1.2)
|
30
|
+
concurrent-ruby (1.1.5)
|
31
|
+
crass (1.0.4)
|
32
|
+
diff-lcs (1.3)
|
33
|
+
erubi (1.8.0)
|
34
|
+
i18n (1.7.0)
|
35
|
+
concurrent-ruby (~> 1.0)
|
36
|
+
loofah (2.2.3)
|
37
|
+
crass (~> 1.0.2)
|
38
|
+
nokogiri (>= 1.5.9)
|
39
|
+
method_source (0.9.2)
|
40
|
+
mini_portile2 (2.4.0)
|
41
|
+
minitest (5.12.2)
|
42
|
+
nokogiri (1.10.4)
|
43
|
+
mini_portile2 (~> 2.4.0)
|
44
|
+
pry (0.12.2)
|
45
|
+
coderay (~> 1.1.0)
|
46
|
+
method_source (~> 0.9.0)
|
47
|
+
rack (2.0.7)
|
48
|
+
rack-test (1.1.0)
|
49
|
+
rack (>= 1.0, < 3)
|
50
|
+
rails-dom-testing (2.0.3)
|
51
|
+
activesupport (>= 4.2.0)
|
52
|
+
nokogiri (>= 1.6)
|
53
|
+
rails-html-sanitizer (1.2.0)
|
54
|
+
loofah (~> 2.2, >= 2.2.2)
|
55
|
+
railties (6.0.0)
|
56
|
+
actionpack (= 6.0.0)
|
57
|
+
activesupport (= 6.0.0)
|
58
|
+
method_source
|
59
|
+
rake (>= 0.8.7)
|
60
|
+
thor (>= 0.20.3, < 2.0)
|
61
|
+
rake (12.3.3)
|
62
|
+
rspec (3.8.0)
|
63
|
+
rspec-core (~> 3.8.0)
|
64
|
+
rspec-expectations (~> 3.8.0)
|
65
|
+
rspec-mocks (~> 3.8.0)
|
66
|
+
rspec-core (3.8.2)
|
67
|
+
rspec-support (~> 3.8.0)
|
68
|
+
rspec-expectations (3.8.4)
|
69
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
70
|
+
rspec-support (~> 3.8.0)
|
71
|
+
rspec-mocks (3.8.1)
|
72
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
73
|
+
rspec-support (~> 3.8.0)
|
74
|
+
rspec-support (3.8.2)
|
75
|
+
thor (0.20.3)
|
76
|
+
thread_safe (0.3.6)
|
77
|
+
tzinfo (1.2.5)
|
78
|
+
thread_safe (~> 0.1)
|
79
|
+
zeitwerk (2.1.10)
|
80
|
+
|
81
|
+
PLATFORMS
|
82
|
+
ruby
|
83
|
+
|
84
|
+
DEPENDENCIES
|
85
|
+
bundler (~> 2.0)
|
86
|
+
pry
|
87
|
+
railties (>= 4.2.0)
|
88
|
+
rake (~> 12.3.0)
|
89
|
+
rspec (~> 3.8.0)
|
90
|
+
yaqb!
|
91
|
+
|
92
|
+
BUNDLED WITH
|
93
|
+
2.0.2
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Ronald Chacon
|
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,43 @@
|
|
1
|
+
# Yaqb
|
2
|
+
|
3
|
+
Welcome to your new gem! In this directory, you'll find the files you need to be able to package up your Ruby library into a gem. Put your Ruby code in the file `lib/yaqb`. To experiment with that code, run `bin/console` for an interactive prompt.
|
4
|
+
|
5
|
+
TODO: Delete this and the text above, and describe your gem
|
6
|
+
|
7
|
+
## Installation
|
8
|
+
|
9
|
+
Add this line to your application's Gemfile:
|
10
|
+
|
11
|
+
```ruby
|
12
|
+
gem 'yaqb'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install yaqb
|
22
|
+
|
23
|
+
## Usage
|
24
|
+
|
25
|
+
TODO: Write usage instructions here
|
26
|
+
|
27
|
+
## Development
|
28
|
+
|
29
|
+
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.
|
30
|
+
|
31
|
+
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).
|
32
|
+
|
33
|
+
## Contributing
|
34
|
+
|
35
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/[USERNAME]/yaqb. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [Contributor Covenant](http://contributor-covenant.org) code of conduct.
|
36
|
+
|
37
|
+
## License
|
38
|
+
|
39
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
40
|
+
|
41
|
+
## Code of Conduct
|
42
|
+
|
43
|
+
Everyone interacting in the Yaqb project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/yaqb/blob/master/CODE_OF_CONDUCT.md).
|
data/Rakefile
ADDED
data/bin/console
ADDED
@@ -0,0 +1,14 @@
|
|
1
|
+
#!/usr/bin/env ruby
|
2
|
+
|
3
|
+
require "bundler/setup"
|
4
|
+
require "yaqb"
|
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,30 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Yaqb
|
4
|
+
class Configuration
|
5
|
+
def paginator
|
6
|
+
@paginator || set_paginator
|
7
|
+
end
|
8
|
+
|
9
|
+
def paginator=(paginator)
|
10
|
+
case paginator.to_sym
|
11
|
+
when :kaminari
|
12
|
+
@paginator = :kaminari
|
13
|
+
else
|
14
|
+
raise StandardError, paginator_error_message(paginator)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
18
|
+
private
|
19
|
+
|
20
|
+
def set_paginator
|
21
|
+
@paginator = :kaminari if defined?(Kaminari)
|
22
|
+
end
|
23
|
+
|
24
|
+
def paginator_error_message(paginator)
|
25
|
+
<<~HEREDOC.chomp
|
26
|
+
Invalid Paginator: (#{paginator}). Currently supported paginators are: [Kaminari].
|
27
|
+
HEREDOC
|
28
|
+
end
|
29
|
+
end
|
30
|
+
end
|
data/lib/yaqb/hooks.rb
ADDED
@@ -0,0 +1,24 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
begin; require 'kaminari'; rescue LoadError; end
|
4
|
+
|
5
|
+
unless defined?(Kaminari)
|
6
|
+
Kernel.warn <<~HEREDOC.chomp
|
7
|
+
Warning: Yaqb relies on Kaminari. Please
|
8
|
+
install dependency by adding the following to your Gemfile:
|
9
|
+
gem 'kaminari'
|
10
|
+
HEREDOC
|
11
|
+
end
|
12
|
+
|
13
|
+
|
14
|
+
if defined?(ActionController::Base)
|
15
|
+
require 'yaqb/query_builder'
|
16
|
+
ActionController::Base.send(:include, Yaqb::QueryBuilder)
|
17
|
+
ActionController::Base.rescue_from Yaqb::Errors::QueryBuilderError, with: :builder_error
|
18
|
+
end
|
19
|
+
|
20
|
+
if defined?(ActionController::API)
|
21
|
+
require 'yaqb/query_builder'
|
22
|
+
ActionController::API.send(:include, Yaqb::QueryBuilder)
|
23
|
+
ActionController::API.rescue_from Yaqb::Errors::QueryBuilderError, with: :builder_error
|
24
|
+
end
|
@@ -0,0 +1,22 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'yaqb/query_builders/query_orchestrator'
|
4
|
+
|
5
|
+
module Yaqb
|
6
|
+
module QueryBuilder
|
7
|
+
private
|
8
|
+
|
9
|
+
def orchestrate(scope, options = {})
|
10
|
+
QueryBuilders::QueryOrchestrator.new(scope, params, request, response, options).call
|
11
|
+
end
|
12
|
+
|
13
|
+
def builder_error(error)
|
14
|
+
render status: 400, json: {
|
15
|
+
error: {
|
16
|
+
message: error.message,
|
17
|
+
invalid_params: error.invalid_params
|
18
|
+
}
|
19
|
+
}
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
@@ -0,0 +1,87 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Yaqb
|
4
|
+
module QueryBuilders
|
5
|
+
class Filter
|
6
|
+
PREDICATES = %w[eq cont notcont start end gt lt].freeze
|
7
|
+
|
8
|
+
def initialize(scope, params, options = {})
|
9
|
+
@scope = scope
|
10
|
+
@filters = params['q'] || {}
|
11
|
+
@options = options
|
12
|
+
end
|
13
|
+
|
14
|
+
def filter
|
15
|
+
return @scope unless @filters.any?
|
16
|
+
|
17
|
+
@filters = format_filters
|
18
|
+
validate_filters
|
19
|
+
build_filter_scope
|
20
|
+
|
21
|
+
@scope
|
22
|
+
end
|
23
|
+
|
24
|
+
private
|
25
|
+
|
26
|
+
def format_filters
|
27
|
+
@filters.each_with_object({}) do |(key, value), hash|
|
28
|
+
hash[key] = {
|
29
|
+
value: value,
|
30
|
+
column: key.split('_')[0...-1].join('_'),
|
31
|
+
predicate: key.split('_').last
|
32
|
+
}
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
def build_filter_scope
|
37
|
+
@filters.each do |_key, data|
|
38
|
+
@scope = send(data[:predicate], data[:column], data[:value])
|
39
|
+
end
|
40
|
+
end
|
41
|
+
|
42
|
+
def eq(column, value)
|
43
|
+
@scope.where(column => value)
|
44
|
+
end
|
45
|
+
|
46
|
+
def cont(column, value)
|
47
|
+
@scope.where("#{column} LIKE ?", "%#{value}%")
|
48
|
+
end
|
49
|
+
|
50
|
+
def notcont(column, value)
|
51
|
+
@scope.where("#{column} NOT LIKE ?", "%#{value}%")
|
52
|
+
end
|
53
|
+
|
54
|
+
def start(column, value)
|
55
|
+
@scope.where("#{column} LIKE ?", "#{value}%")
|
56
|
+
end
|
57
|
+
|
58
|
+
def end(column, value)
|
59
|
+
@scope.where("#{column} LIKE ?", "%#{value}")
|
60
|
+
end
|
61
|
+
|
62
|
+
def gt(column, value)
|
63
|
+
@scope.where("#{column} > ?", value)
|
64
|
+
end
|
65
|
+
|
66
|
+
def lt(column, value)
|
67
|
+
@scope.where("#{column} < ?", value)
|
68
|
+
end
|
69
|
+
|
70
|
+
def validate_filters
|
71
|
+
attributes = @options[:columns]
|
72
|
+
@filters.each do |key, data|
|
73
|
+
error!(key, data) unless attributes.include?(data[:column])
|
74
|
+
error!(key, data) unless PREDICATES.include?(data[:predicate])
|
75
|
+
end
|
76
|
+
end
|
77
|
+
|
78
|
+
def error!(key, data)
|
79
|
+
columns = @options[:columns].join(', ')
|
80
|
+
pred = PREDICATES.join(', ')
|
81
|
+
|
82
|
+
raise Errors::QueryBuilderError.new("q[#{key}]=#{data[:value]}"),
|
83
|
+
"Invalid Filter params. Allowed columns: (#{columns}), 'predicates': #{pred}"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
end
|
87
|
+
end
|
@@ -0,0 +1,64 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Yaqb
|
4
|
+
module QueryBuilders
|
5
|
+
class Paginate
|
6
|
+
def initialize(scope, query_params, url)
|
7
|
+
@query_params = query_params
|
8
|
+
@page = validate_param!(:page, 1)
|
9
|
+
@per = validate_param!(:per, Kaminari.config.default_per_page)
|
10
|
+
@scope = scope.page(@page).per(@per)
|
11
|
+
@url = url
|
12
|
+
end
|
13
|
+
|
14
|
+
def paginate
|
15
|
+
@scope
|
16
|
+
end
|
17
|
+
|
18
|
+
def links
|
19
|
+
@links ||= pages.each_with_object([]) do |(k, v), links|
|
20
|
+
query_params = @query_params.merge("page" => v, "per" => @per).to_param
|
21
|
+
links << "<#{@url}?#{query_params}>; rel=\"#{k}\""
|
22
|
+
end.join(', ')
|
23
|
+
end
|
24
|
+
|
25
|
+
private
|
26
|
+
|
27
|
+
def show_first_link?
|
28
|
+
@scope.total_pages > 1 && !@scope.first_page?
|
29
|
+
end
|
30
|
+
|
31
|
+
def show_previous_link?
|
32
|
+
!@scope.first_page?
|
33
|
+
end
|
34
|
+
|
35
|
+
def show_next_link?
|
36
|
+
!@scope.last_page?
|
37
|
+
end
|
38
|
+
|
39
|
+
def show_last_link?
|
40
|
+
@scope.total_pages > 1 && !@scope.last_page?
|
41
|
+
end
|
42
|
+
|
43
|
+
def pages
|
44
|
+
@pages ||= {}.tap do |h|
|
45
|
+
h[:first] = 1 if show_first_link?
|
46
|
+
h[:prev] = @scope.current_page - 1 if show_previous_link?
|
47
|
+
h[:next] = @scope.current_page + 1 if show_next_link?
|
48
|
+
h[:last] = @scope.total_pages if show_last_link?
|
49
|
+
end
|
50
|
+
end
|
51
|
+
|
52
|
+
def validate_param!(name, default)
|
53
|
+
return default unless @query_params[name]
|
54
|
+
|
55
|
+
unless @query_params[name] =~ /\A\d+\z/
|
56
|
+
raise Errors::QueryBuilderError.new("#{name}=#{@query_params[name]}"),
|
57
|
+
'Invalid Pagination params. Only numbers are supported for "page" and "per"'
|
58
|
+
end
|
59
|
+
|
60
|
+
@query_params[name]
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
end
|
@@ -0,0 +1,44 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'yaqb/query_builders/paginate'
|
4
|
+
require 'yaqb/query_builders/sort'
|
5
|
+
require 'yaqb/query_builders/filter'
|
6
|
+
|
7
|
+
module Yaqb
|
8
|
+
module QueryBuilders
|
9
|
+
class QueryOrchestrator
|
10
|
+
def initialize(scope, params, request, response, options = {})
|
11
|
+
@scope = scope
|
12
|
+
@params = params
|
13
|
+
@request = request
|
14
|
+
@response = response
|
15
|
+
@options = options
|
16
|
+
end
|
17
|
+
|
18
|
+
def call
|
19
|
+
@scope = sort
|
20
|
+
@scope = paginate
|
21
|
+
@scope = filter
|
22
|
+
|
23
|
+
@scope
|
24
|
+
end
|
25
|
+
|
26
|
+
private
|
27
|
+
|
28
|
+
def paginate
|
29
|
+
current_url = @request.base_url + @request.path
|
30
|
+
paginator = Paginate.new(@scope, @request.query_parameters, current_url)
|
31
|
+
@response.headers['Link'] = paginator.links
|
32
|
+
paginator.paginate
|
33
|
+
end
|
34
|
+
|
35
|
+
def sort
|
36
|
+
Sort.new(@scope, @params).sort
|
37
|
+
end
|
38
|
+
|
39
|
+
def filter
|
40
|
+
Filter.new(@scope, @params.to_unsafe_hash, @options).filter
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
@@ -0,0 +1,21 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module Yaqb
|
4
|
+
module QueryBuilders
|
5
|
+
class Sort
|
6
|
+
DIRECTIONS = %w[asc desc].freeze
|
7
|
+
|
8
|
+
def initialize(scope, params)
|
9
|
+
@scope = scope
|
10
|
+
@column = params[:sort]
|
11
|
+
@direction = params[:dir]
|
12
|
+
end
|
13
|
+
|
14
|
+
def sort
|
15
|
+
return @scope unless @column && @direction
|
16
|
+
|
17
|
+
@scope.order(Arel.sql("#{@column} #{@direction}"))
|
18
|
+
end
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
data/lib/yaqb/railtie.rb
ADDED
data/lib/yaqb/version.rb
ADDED
data/lib/yaqb.rb
ADDED
@@ -0,0 +1,19 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'yaqb/version'
|
4
|
+
require 'yaqb/configuration'
|
5
|
+
require 'yaqb/errors/query_builder_error'
|
6
|
+
require 'yaqb/railtie'
|
7
|
+
|
8
|
+
module Yaqb
|
9
|
+
class << self
|
10
|
+
def configure
|
11
|
+
yield(configuration)
|
12
|
+
end
|
13
|
+
|
14
|
+
def configuration
|
15
|
+
@configuration ||= Configuration.new
|
16
|
+
end
|
17
|
+
alias config configuration
|
18
|
+
end
|
19
|
+
end
|
data/yaqb.gemspec
ADDED
@@ -0,0 +1,34 @@
|
|
1
|
+
lib = File.expand_path("lib", __dir__)
|
2
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
3
|
+
require "yaqb/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |spec|
|
6
|
+
spec.name = "yaqb"
|
7
|
+
spec.version = Yaqb::VERSION
|
8
|
+
spec.authors = ['Ronald Chacon']
|
9
|
+
spec.email = ['ronald.chacon87@gmail.com']
|
10
|
+
|
11
|
+
spec.summary = 'YAQB'
|
12
|
+
spec.description = 'Yet Another Query Builder'
|
13
|
+
spec.homepage = 'https://github.com/ronaldchacon/yaqb'
|
14
|
+
spec.license = 'MIT'
|
15
|
+
|
16
|
+
spec.metadata['homepage_uri'] = spec.homepage
|
17
|
+
spec.metadata['source_code_uri'] = spec.homepage
|
18
|
+
spec.metadata['changelog_uri'] = spec.homepage
|
19
|
+
|
20
|
+
# Specify which files should be added to the gem when it is released.
|
21
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
22
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
23
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
24
|
+
end
|
25
|
+
spec.bindir = 'exe'
|
26
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
27
|
+
spec.require_paths = ['lib']
|
28
|
+
|
29
|
+
spec.add_development_dependency 'bundler', '~> 2.0'
|
30
|
+
spec.add_development_dependency 'pry'
|
31
|
+
spec.add_development_dependency 'railties', '>= 4.2.0'
|
32
|
+
spec.add_development_dependency 'rake', '~> 12.3.0'
|
33
|
+
spec.add_development_dependency 'rspec', '~> 3.8.0'
|
34
|
+
end
|
metadata
ADDED
@@ -0,0 +1,139 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: yaqb
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ronald Chacon
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-10-10 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: bundler
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '2.0'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '2.0'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: pry
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - ">="
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - ">="
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: railties
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - ">="
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: 4.2.0
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - ">="
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: 4.2.0
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: rake
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - "~>"
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: 12.3.0
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - "~>"
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: 12.3.0
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rspec
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - "~>"
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: 3.8.0
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - "~>"
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 3.8.0
|
83
|
+
description: Yet Another Query Builder
|
84
|
+
email:
|
85
|
+
- ronald.chacon87@gmail.com
|
86
|
+
executables: []
|
87
|
+
extensions: []
|
88
|
+
extra_rdoc_files: []
|
89
|
+
files:
|
90
|
+
- ".gitignore"
|
91
|
+
- ".rspec"
|
92
|
+
- ".travis.yml"
|
93
|
+
- CODE_OF_CONDUCT.md
|
94
|
+
- Gemfile
|
95
|
+
- Gemfile.lock
|
96
|
+
- LICENSE.txt
|
97
|
+
- README.md
|
98
|
+
- Rakefile
|
99
|
+
- bin/console
|
100
|
+
- bin/setup
|
101
|
+
- lib/yaqb.rb
|
102
|
+
- lib/yaqb/configuration.rb
|
103
|
+
- lib/yaqb/errors/query_builder_error.rb
|
104
|
+
- lib/yaqb/hooks.rb
|
105
|
+
- lib/yaqb/query_builder.rb
|
106
|
+
- lib/yaqb/query_builders/filter.rb
|
107
|
+
- lib/yaqb/query_builders/paginate.rb
|
108
|
+
- lib/yaqb/query_builders/query_orchestrator.rb
|
109
|
+
- lib/yaqb/query_builders/sort.rb
|
110
|
+
- lib/yaqb/railtie.rb
|
111
|
+
- lib/yaqb/version.rb
|
112
|
+
- yaqb.gemspec
|
113
|
+
homepage: https://github.com/ronaldchacon/yaqb
|
114
|
+
licenses:
|
115
|
+
- MIT
|
116
|
+
metadata:
|
117
|
+
homepage_uri: https://github.com/ronaldchacon/yaqb
|
118
|
+
source_code_uri: https://github.com/ronaldchacon/yaqb
|
119
|
+
changelog_uri: https://github.com/ronaldchacon/yaqb
|
120
|
+
post_install_message:
|
121
|
+
rdoc_options: []
|
122
|
+
require_paths:
|
123
|
+
- lib
|
124
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
125
|
+
requirements:
|
126
|
+
- - ">="
|
127
|
+
- !ruby/object:Gem::Version
|
128
|
+
version: '0'
|
129
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
130
|
+
requirements:
|
131
|
+
- - ">="
|
132
|
+
- !ruby/object:Gem::Version
|
133
|
+
version: '0'
|
134
|
+
requirements: []
|
135
|
+
rubygems_version: 3.0.3
|
136
|
+
signing_key:
|
137
|
+
specification_version: 4
|
138
|
+
summary: YAQB
|
139
|
+
test_files: []
|