pattern_query_helper 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 +6 -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/pattern_query_helper.rb +118 -0
- data/lib/pattern_query_helper/associations.rb +17 -0
- data/lib/pattern_query_helper/filtering.rb +74 -0
- data/lib/pattern_query_helper/pagination.rb +63 -0
- data/lib/pattern_query_helper/sorting.rb +28 -0
- data/lib/pattern_query_helper/sql.rb +62 -0
- data/lib/pattern_query_helper/version.rb +3 -0
- data/pattern_query_helper.gemspec +47 -0
- metadata +162 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0e9ad6edf70f5123dc021db87bf78b717b9f8829
|
4
|
+
data.tar.gz: '08ede63bf55e731a9a8395f0235ba20fbf2ff30b'
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: 4aa38ee2f18d28de1c1c17f4aaef33e3a29ac9598e82a26084a42c11de96744344c7973c41d84fa0feff548e1de9d66afbded75816d68bf4ac5da090b5e56ed5
|
7
|
+
data.tar.gz: d69b4bd4af2f04721790514c5c1b0bdc254e24951a4b3a18553bb0d70be3e8268dfce254ba3ccb4cde742bbe35d0aa0b22c174bb94df54c0860b97445b176a54
|
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 eamigo13@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
|
+
pattern_query_helper (0.1.0)
|
5
|
+
activerecord (~> 5.0)
|
6
|
+
kaminari
|
7
|
+
|
8
|
+
GEM
|
9
|
+
remote: https://rubygems.org/
|
10
|
+
specs:
|
11
|
+
actionview (5.0.7)
|
12
|
+
activesupport (= 5.0.7)
|
13
|
+
builder (~> 3.1)
|
14
|
+
erubis (~> 2.7.0)
|
15
|
+
rails-dom-testing (~> 2.0)
|
16
|
+
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
17
|
+
activemodel (5.0.7)
|
18
|
+
activesupport (= 5.0.7)
|
19
|
+
activerecord (5.0.7)
|
20
|
+
activemodel (= 5.0.7)
|
21
|
+
activesupport (= 5.0.7)
|
22
|
+
arel (~> 7.0)
|
23
|
+
activesupport (5.0.7)
|
24
|
+
concurrent-ruby (~> 1.0, >= 1.0.2)
|
25
|
+
i18n (>= 0.7, < 2)
|
26
|
+
minitest (~> 5.1)
|
27
|
+
tzinfo (~> 1.1)
|
28
|
+
arel (7.1.4)
|
29
|
+
builder (3.2.3)
|
30
|
+
concurrent-ruby (1.1.3)
|
31
|
+
crass (1.0.4)
|
32
|
+
diff-lcs (1.3)
|
33
|
+
erubis (2.7.0)
|
34
|
+
faker (1.8.4)
|
35
|
+
i18n (~> 0.5)
|
36
|
+
i18n (0.9.3)
|
37
|
+
concurrent-ruby (~> 1.0)
|
38
|
+
kaminari (1.1.0)
|
39
|
+
activesupport (>= 4.1.0)
|
40
|
+
kaminari-actionview (= 1.1.0)
|
41
|
+
kaminari-activerecord (= 1.1.0)
|
42
|
+
kaminari-core (= 1.1.0)
|
43
|
+
kaminari-actionview (1.1.0)
|
44
|
+
actionview
|
45
|
+
kaminari-core (= 1.1.0)
|
46
|
+
kaminari-activerecord (1.1.0)
|
47
|
+
activerecord
|
48
|
+
kaminari-core (= 1.1.0)
|
49
|
+
kaminari-core (1.1.0)
|
50
|
+
loofah (2.2.3)
|
51
|
+
crass (~> 1.0.2)
|
52
|
+
nokogiri (>= 1.5.9)
|
53
|
+
mini_portile2 (2.3.0)
|
54
|
+
minitest (5.11.3)
|
55
|
+
nokogiri (1.8.5)
|
56
|
+
mini_portile2 (~> 2.3.0)
|
57
|
+
rails-dom-testing (2.0.3)
|
58
|
+
activesupport (>= 4.2.0)
|
59
|
+
nokogiri (>= 1.6)
|
60
|
+
rails-html-sanitizer (1.0.4)
|
61
|
+
loofah (~> 2.2, >= 2.2.2)
|
62
|
+
rake (10.5.0)
|
63
|
+
rspec (3.8.0)
|
64
|
+
rspec-core (~> 3.8.0)
|
65
|
+
rspec-expectations (~> 3.8.0)
|
66
|
+
rspec-mocks (~> 3.8.0)
|
67
|
+
rspec-core (3.8.0)
|
68
|
+
rspec-support (~> 3.8.0)
|
69
|
+
rspec-expectations (3.8.2)
|
70
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
71
|
+
rspec-support (~> 3.8.0)
|
72
|
+
rspec-mocks (3.8.0)
|
73
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
74
|
+
rspec-support (~> 3.8.0)
|
75
|
+
rspec-support (3.8.0)
|
76
|
+
sqlite3 (1.3.13)
|
77
|
+
thread_safe (0.3.6)
|
78
|
+
tzinfo (1.2.5)
|
79
|
+
thread_safe (~> 0.1)
|
80
|
+
|
81
|
+
PLATFORMS
|
82
|
+
ruby
|
83
|
+
|
84
|
+
DEPENDENCIES
|
85
|
+
bundler (~> 1.16)
|
86
|
+
faker
|
87
|
+
pattern_query_helper!
|
88
|
+
rake (~> 10.0)
|
89
|
+
rspec (~> 3.0)
|
90
|
+
sqlite3
|
91
|
+
|
92
|
+
BUNDLED WITH
|
93
|
+
1.16.6
|
data/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2019 Evan McDaniel
|
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
|
+
# PatternQueryHelper
|
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/pattern_query_helper`. 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 'pattern_query_helper'
|
13
|
+
```
|
14
|
+
|
15
|
+
And then execute:
|
16
|
+
|
17
|
+
$ bundle
|
18
|
+
|
19
|
+
Or install it yourself as:
|
20
|
+
|
21
|
+
$ gem install pattern_query_helper
|
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]/pattern_query_helper. 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 PatternQueryHelper project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/pattern_query_helper/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 "pattern_query_helper"
|
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,118 @@
|
|
1
|
+
require "pattern_query_helper/version"
|
2
|
+
require "pattern_query_helper/pagination"
|
3
|
+
require "pattern_query_helper/filtering"
|
4
|
+
require "pattern_query_helper/associations"
|
5
|
+
require "pattern_query_helper/sorting"
|
6
|
+
require "pattern_query_helper/sql"
|
7
|
+
|
8
|
+
module PatternQueryHelper
|
9
|
+
def self.paginated_sql_query(model, query, query_params, url_params)
|
10
|
+
parse_params(url_params)
|
11
|
+
|
12
|
+
query_config = {
|
13
|
+
model: model,
|
14
|
+
query: query,
|
15
|
+
query_params: query_params,
|
16
|
+
page: @pagination[:page],
|
17
|
+
per_page: @pagination[:per_page],
|
18
|
+
filter_string: @filtering[:filter_string],
|
19
|
+
filter_params: @filtering[:filter_params],
|
20
|
+
sort_string: @sorting,
|
21
|
+
}
|
22
|
+
|
23
|
+
data = PatternQueryHelper::Sql.sql_query(query_config)
|
24
|
+
data = PatternQueryHelper::Associations.load_associations(data, @associations)
|
25
|
+
count = PatternQueryHelper::Sql.sql_query_count(query_config)
|
26
|
+
pagination = PatternQueryHelper::Pagination.create_pagination_payload(count, @pagination)
|
27
|
+
|
28
|
+
{
|
29
|
+
pagination: pagination,
|
30
|
+
data: data
|
31
|
+
}
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.sql_query(model, query, query_params, url_params)
|
35
|
+
parse_params(url_params)
|
36
|
+
|
37
|
+
query_config = {
|
38
|
+
model: model,
|
39
|
+
query: query,
|
40
|
+
query_params: query_params,
|
41
|
+
filter_string: @filtering[:filter_string],
|
42
|
+
filter_params: @filtering[:filter_params],
|
43
|
+
sort_string: @sorting,
|
44
|
+
}
|
45
|
+
|
46
|
+
data = PatternQueryHelper::Sql.sql_query(query_config)
|
47
|
+
data = PatternQueryHelper::Associations.load_associations(data, @associations)
|
48
|
+
|
49
|
+
{
|
50
|
+
data: data
|
51
|
+
}
|
52
|
+
end
|
53
|
+
|
54
|
+
def self.single_record_sql_query(model, query, query_params, url_params)
|
55
|
+
parse_params(url_params)
|
56
|
+
|
57
|
+
query_config = {
|
58
|
+
model: model,
|
59
|
+
query: query,
|
60
|
+
query_params: query_params,
|
61
|
+
filter_string: @filtering[:filter_string],
|
62
|
+
filter_params: @filtering[:filter_params],
|
63
|
+
sort_string: @sorting,
|
64
|
+
}
|
65
|
+
|
66
|
+
data = PatternQueryHelper::Sql.single_record_query(query_config)
|
67
|
+
data = PatternQueryHelper::Associations.load_associations(data, @associations)
|
68
|
+
|
69
|
+
{
|
70
|
+
data: data
|
71
|
+
}
|
72
|
+
end
|
73
|
+
|
74
|
+
def self.active_record_query(active_record_call, url_params)
|
75
|
+
parse_params(url_params)
|
76
|
+
filtered_query = PatternQueryHelper::Filtering.filter_active_record_query(active_record_call, @filtering)
|
77
|
+
sorted_query = PatternQueryHelper::Sorting.sort_active_record_query(active_record_call, @sorting)
|
78
|
+
with_associations = PatternQueryHelper::Associations.load_associations(sorted_query, @associations)
|
79
|
+
{
|
80
|
+
data: with_associations
|
81
|
+
}
|
82
|
+
end
|
83
|
+
|
84
|
+
def self.paginated_active_record_query(active_record_call, url_params)
|
85
|
+
parse_params(url_params)
|
86
|
+
filtered_query = PatternQueryHelper::Filtering.filter_active_record_query(active_record_call, @filtering)
|
87
|
+
sorted_query = PatternQueryHelper::Sorting.sort_active_record_query(active_record_call, @sorting)
|
88
|
+
paginated_query = PatternQueryHelper::Pagination.paginate_active_record_query(sorted_query, @pagination)
|
89
|
+
with_associations = PatternQueryHelper::Associations.load_associations(paginated_query, @associations)
|
90
|
+
pagination = PatternQueryHelper::Pagination.create_pagination_payload(sorted_query.count, @pagination)
|
91
|
+
{
|
92
|
+
pagination: pagination,
|
93
|
+
data: with_associations
|
94
|
+
}
|
95
|
+
end
|
96
|
+
|
97
|
+
def self.single_record_active_record_query
|
98
|
+
# TODO: Add Logic to this method
|
99
|
+
end
|
100
|
+
|
101
|
+
def self.parse_params(params)
|
102
|
+
@filtering = PatternQueryHelper::Filtering.create_filters(params[:filter])
|
103
|
+
@sorting = PatternQueryHelper::Sorting.parse_sorting_params(params)
|
104
|
+
@associations = PatternQueryHelper::Associations.process_association_params(params)
|
105
|
+
@pagination = PatternQueryHelper::Pagination.parse_pagination_params(params)
|
106
|
+
|
107
|
+
{
|
108
|
+
filters: @filtering,
|
109
|
+
sorting: @sorting,
|
110
|
+
associations: @associations,
|
111
|
+
pagination: @pagination
|
112
|
+
}
|
113
|
+
end
|
114
|
+
|
115
|
+
class << self
|
116
|
+
attr_accessor :active_record_adapter
|
117
|
+
end
|
118
|
+
end
|
@@ -0,0 +1,17 @@
|
|
1
|
+
module PatternQueryHelper
|
2
|
+
class Associations
|
3
|
+
def self.process_association_params(params)
|
4
|
+
associations = params[:include] || []
|
5
|
+
if associations.class == String
|
6
|
+
return [associations.to_sym]
|
7
|
+
else
|
8
|
+
return associations.map { |x| x.to_sym }
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.load_associations(payload, associations)
|
13
|
+
ActiveRecord::Associations::Preloader.new.preload(payload, associations)
|
14
|
+
payload.as_json(include: associations)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -0,0 +1,74 @@
|
|
1
|
+
module PatternQueryHelper
|
2
|
+
class Filtering
|
3
|
+
def self.create_filters(filters, column_map=nil, symbol_prefix="")
|
4
|
+
filters ||= {}
|
5
|
+
filter_string = "true = true"
|
6
|
+
filter_params = {}
|
7
|
+
filter_array = []
|
8
|
+
filters.each do |filter_attribute, criteria|
|
9
|
+
if column_map
|
10
|
+
raise ArgumentError.new("Invalid filter '#{filter_attribute}'") unless column_map[filter_attribute]
|
11
|
+
filter_column = column_map[filter_attribute]
|
12
|
+
else
|
13
|
+
filter_column = filter_attribute
|
14
|
+
end
|
15
|
+
criteria.each do |operator_code, criterion|
|
16
|
+
filter_symbol = "#{symbol_prefix}#{filter_attribute}_#{operator_code}"
|
17
|
+
case operator_code
|
18
|
+
when "gte"
|
19
|
+
operator = ">="
|
20
|
+
when "lte"
|
21
|
+
operator = "<="
|
22
|
+
when "gt"
|
23
|
+
operator = ">"
|
24
|
+
when "lt"
|
25
|
+
operator = "<"
|
26
|
+
when "eql"
|
27
|
+
operator = "="
|
28
|
+
when "noteql"
|
29
|
+
operator = "!="
|
30
|
+
when "in"
|
31
|
+
values = criterion.split(",").map { |s| s.to_i }
|
32
|
+
values = values.to_s.gsub("[","(").gsub("]",")")
|
33
|
+
operator = "in #{values}"
|
34
|
+
filter_symbol = ""
|
35
|
+
when "notin"
|
36
|
+
values = criterion.split(",").map { |s| s.to_i }
|
37
|
+
values = values.to_s.gsub("[","(").gsub("]",")")
|
38
|
+
operator = "not in #{values}"
|
39
|
+
filter_symbol = ""
|
40
|
+
when "null"
|
41
|
+
if criterion = true || "true"
|
42
|
+
operator = "is null"
|
43
|
+
elsif criterion = false || "false"
|
44
|
+
operator = "is not null"
|
45
|
+
end
|
46
|
+
filter_symbol = ""
|
47
|
+
else
|
48
|
+
raise ArgumentError.new("Invalid operator code '#{operator_code}' on '#{filter_attribute}' filter")
|
49
|
+
end
|
50
|
+
filter_symbol_embed = ":#{filter_symbol}" unless filter_symbol.blank?
|
51
|
+
filter_string = "#{filter_string} and #{filter_column} #{operator} #{filter_symbol_embed}"
|
52
|
+
filter_params["#{filter_symbol}"] = criterion
|
53
|
+
filter_array << {
|
54
|
+
column: filter_attribute,
|
55
|
+
operator: operator,
|
56
|
+
value: criterion,
|
57
|
+
symbol: filter_symbol
|
58
|
+
}
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
{
|
63
|
+
filter_string: filter_string,
|
64
|
+
filter_params: filter_params,
|
65
|
+
filter_array: filter_array
|
66
|
+
}
|
67
|
+
|
68
|
+
end
|
69
|
+
|
70
|
+
def self.filter_active_record_query(active_record_call, filtering)
|
71
|
+
active_record_call.where(filtering[:filter_string], filtering[:filter_params].symbolize_keys)
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
require 'active_record'
|
2
|
+
require 'kaminari'
|
3
|
+
|
4
|
+
module PatternQueryHelper
|
5
|
+
class Pagination
|
6
|
+
def self.parse_pagination_params(params)
|
7
|
+
if params[:per_page] == 'all'
|
8
|
+
pagination_params = {
|
9
|
+
include_all: true
|
10
|
+
}
|
11
|
+
else
|
12
|
+
if params[:page]
|
13
|
+
page = params[:page].to_i
|
14
|
+
else
|
15
|
+
page = 1
|
16
|
+
end
|
17
|
+
if params[:per_page]
|
18
|
+
per_page = params[:per_page].to_i
|
19
|
+
else
|
20
|
+
per_page = 20
|
21
|
+
end
|
22
|
+
raise RangeError.new("page must be greater than 0") unless page > 0
|
23
|
+
raise RangeError.new("per_page must be greater than 0") unless per_page > 0
|
24
|
+
pagination_params = {
|
25
|
+
page: page.to_i,
|
26
|
+
per_page: per_page.to_i
|
27
|
+
}
|
28
|
+
end
|
29
|
+
|
30
|
+
pagination_params
|
31
|
+
|
32
|
+
end
|
33
|
+
|
34
|
+
def self.create_pagination_payload(count, pagination_params)
|
35
|
+
page = pagination_params[:page]
|
36
|
+
per_page = pagination_params[:per_page]
|
37
|
+
total_pages = (count/(per_page.nonzero? || 1).to_f).ceil
|
38
|
+
next_page = page + 1 if page.between?(1, total_pages - 1)
|
39
|
+
previous_page = page - 1 if page.between?(2, total_pages)
|
40
|
+
first_page = page == 1
|
41
|
+
last_page = page == total_pages
|
42
|
+
out_of_range = !page.between?(1,total_pages)
|
43
|
+
|
44
|
+
{
|
45
|
+
count: count,
|
46
|
+
current_page: page,
|
47
|
+
next_page: next_page,
|
48
|
+
previous_page: previous_page,
|
49
|
+
total_pages: total_pages,
|
50
|
+
per_page: per_page,
|
51
|
+
first_page: first_page,
|
52
|
+
last_page: last_page,
|
53
|
+
out_of_range: out_of_range
|
54
|
+
}
|
55
|
+
end
|
56
|
+
|
57
|
+
def self.paginate_active_record_query(active_record_call, pagination_params)
|
58
|
+
page = pagination_params[:page]
|
59
|
+
per_page = pagination_params[:per_page]
|
60
|
+
active_record_call.page(page).per(per_page)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -0,0 +1,28 @@
|
|
1
|
+
module PatternQueryHelper
|
2
|
+
class Sorting
|
3
|
+
def self.parse_sorting_params(params)
|
4
|
+
sort_sql= []
|
5
|
+
if params[:sort]
|
6
|
+
sorts = params[:sort].split(",")
|
7
|
+
sorts.each_with_index do |sort, index|
|
8
|
+
if sort.split(":")[1] == "desc"
|
9
|
+
case PatternQueryHelper.active_record_adapter
|
10
|
+
when "sqlite3"
|
11
|
+
direction = "desc"
|
12
|
+
else
|
13
|
+
direction = "desc null last"
|
14
|
+
end
|
15
|
+
else
|
16
|
+
direction = "asc"
|
17
|
+
end
|
18
|
+
sort_sql << "#{sort.split(":")[0]} #{direction}"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
sort_sql.join(", ")
|
22
|
+
end
|
23
|
+
|
24
|
+
def self.sort_active_record_query(active_record_call, sort_string)
|
25
|
+
active_record_call.order(sort_string)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
end
|
@@ -0,0 +1,62 @@
|
|
1
|
+
module PatternQueryHelper
|
2
|
+
class Sql
|
3
|
+
def self.sql_query(config)
|
4
|
+
model = config[:model]
|
5
|
+
query = config[:query]
|
6
|
+
query_params = config[:query_params] || {}
|
7
|
+
page = config[:page]
|
8
|
+
per_page = config[:per_page]
|
9
|
+
filter_string = config[:filter_string]
|
10
|
+
filter_params = config[:filter_params] || {}
|
11
|
+
sort_string = config[:sort_string]
|
12
|
+
|
13
|
+
if page && per_page
|
14
|
+
query_params[:limit] = per_page
|
15
|
+
query_params[:offset] = (page - 1) * per_page
|
16
|
+
limit = "limit :limit offset :offset"
|
17
|
+
end
|
18
|
+
|
19
|
+
query_params = query_params.merge(filter_params).symbolize_keys
|
20
|
+
sort_string = "order by #{sort_string}" if !sort_string.blank?
|
21
|
+
filter_string = "where #{filter_string}" if !filter_string.blank?
|
22
|
+
|
23
|
+
sql = %(
|
24
|
+
with query as (#{query})
|
25
|
+
select *
|
26
|
+
from query
|
27
|
+
#{filter_string}
|
28
|
+
#{sort_string}
|
29
|
+
#{limit}
|
30
|
+
)
|
31
|
+
|
32
|
+
model.find_by_sql([sql, query_params])
|
33
|
+
end
|
34
|
+
|
35
|
+
def self.sql_query_count(config)
|
36
|
+
model = config[:model]
|
37
|
+
query = config[:query]
|
38
|
+
query_params = config[:query_params] || {}
|
39
|
+
filter_string = config[:filter_string]
|
40
|
+
filter_params = config[:filter_params] || {}
|
41
|
+
|
42
|
+
query_params = query_params.merge(filter_params).symbolize_keys
|
43
|
+
filter_string = "where #{filter_string}" if !filter_string.blank?
|
44
|
+
|
45
|
+
count_sql = %(
|
46
|
+
with query as (#{query})
|
47
|
+
select count(*) as count
|
48
|
+
from query
|
49
|
+
#{filter_string}
|
50
|
+
)
|
51
|
+
|
52
|
+
model.find_by_sql([count_sql, query_params]).first["count"]
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.single_record_query(config)
|
56
|
+
results = sql_query(config)
|
57
|
+
raise StandardError.new("single record query returned multiple results") unless results.length == 1
|
58
|
+
results.first
|
59
|
+
end
|
60
|
+
|
61
|
+
end
|
62
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "pattern_query_helper/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "pattern_query_helper"
|
8
|
+
spec.version = PatternQueryHelper::VERSION
|
9
|
+
spec.authors = ["Evan McDaniel"]
|
10
|
+
spec.email = ["eamigo13@gmail.com"]
|
11
|
+
|
12
|
+
spec.summary = %q{Ruby Gem to help with pagination and data formatting at Pattern, Inc.}
|
13
|
+
spec.description = %q{Ruby gem developed to help with pagination, filtering, sorting, and including associations on both active record queries and custom sql queries}
|
14
|
+
spec.homepage = "https://github.com/iserve-products/pattern_query_helper"
|
15
|
+
spec.license = "MIT"
|
16
|
+
|
17
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
18
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
19
|
+
if spec.respond_to?(:metadata)
|
20
|
+
# spec.metadata["allowed_push_host"] = "TODO: Set to 'http://mygemserver.com'"
|
21
|
+
#
|
22
|
+
# spec.metadata["homepage_uri"] = spec.homepage
|
23
|
+
# spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
|
24
|
+
# spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
|
25
|
+
else
|
26
|
+
raise "RubyGems 2.0 or newer is required to protect against " \
|
27
|
+
"public gem pushes."
|
28
|
+
end
|
29
|
+
|
30
|
+
# Specify which files should be added to the gem when it is released.
|
31
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
32
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
33
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
34
|
+
end
|
35
|
+
spec.bindir = "exe"
|
36
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
37
|
+
spec.require_paths = ["lib"]
|
38
|
+
|
39
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
40
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
41
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
42
|
+
spec.add_development_dependency "sqlite3"
|
43
|
+
spec.add_development_dependency "faker"
|
44
|
+
|
45
|
+
spec.add_dependency "activerecord", "~> 5.0"
|
46
|
+
spec.add_dependency "kaminari"
|
47
|
+
end
|
metadata
ADDED
@@ -0,0 +1,162 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: pattern_query_helper
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Evan McDaniel
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2019-02-21 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: '1.16'
|
20
|
+
type: :development
|
21
|
+
prerelease: false
|
22
|
+
version_requirements: !ruby/object:Gem::Requirement
|
23
|
+
requirements:
|
24
|
+
- - "~>"
|
25
|
+
- !ruby/object:Gem::Version
|
26
|
+
version: '1.16'
|
27
|
+
- !ruby/object:Gem::Dependency
|
28
|
+
name: rake
|
29
|
+
requirement: !ruby/object:Gem::Requirement
|
30
|
+
requirements:
|
31
|
+
- - "~>"
|
32
|
+
- !ruby/object:Gem::Version
|
33
|
+
version: '10.0'
|
34
|
+
type: :development
|
35
|
+
prerelease: false
|
36
|
+
version_requirements: !ruby/object:Gem::Requirement
|
37
|
+
requirements:
|
38
|
+
- - "~>"
|
39
|
+
- !ruby/object:Gem::Version
|
40
|
+
version: '10.0'
|
41
|
+
- !ruby/object:Gem::Dependency
|
42
|
+
name: rspec
|
43
|
+
requirement: !ruby/object:Gem::Requirement
|
44
|
+
requirements:
|
45
|
+
- - "~>"
|
46
|
+
- !ruby/object:Gem::Version
|
47
|
+
version: '3.0'
|
48
|
+
type: :development
|
49
|
+
prerelease: false
|
50
|
+
version_requirements: !ruby/object:Gem::Requirement
|
51
|
+
requirements:
|
52
|
+
- - "~>"
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '3.0'
|
55
|
+
- !ruby/object:Gem::Dependency
|
56
|
+
name: sqlite3
|
57
|
+
requirement: !ruby/object:Gem::Requirement
|
58
|
+
requirements:
|
59
|
+
- - ">="
|
60
|
+
- !ruby/object:Gem::Version
|
61
|
+
version: '0'
|
62
|
+
type: :development
|
63
|
+
prerelease: false
|
64
|
+
version_requirements: !ruby/object:Gem::Requirement
|
65
|
+
requirements:
|
66
|
+
- - ">="
|
67
|
+
- !ruby/object:Gem::Version
|
68
|
+
version: '0'
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: faker
|
71
|
+
requirement: !ruby/object:Gem::Requirement
|
72
|
+
requirements:
|
73
|
+
- - ">="
|
74
|
+
- !ruby/object:Gem::Version
|
75
|
+
version: '0'
|
76
|
+
type: :development
|
77
|
+
prerelease: false
|
78
|
+
version_requirements: !ruby/object:Gem::Requirement
|
79
|
+
requirements:
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: '0'
|
83
|
+
- !ruby/object:Gem::Dependency
|
84
|
+
name: activerecord
|
85
|
+
requirement: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '5.0'
|
90
|
+
type: :runtime
|
91
|
+
prerelease: false
|
92
|
+
version_requirements: !ruby/object:Gem::Requirement
|
93
|
+
requirements:
|
94
|
+
- - "~>"
|
95
|
+
- !ruby/object:Gem::Version
|
96
|
+
version: '5.0'
|
97
|
+
- !ruby/object:Gem::Dependency
|
98
|
+
name: kaminari
|
99
|
+
requirement: !ruby/object:Gem::Requirement
|
100
|
+
requirements:
|
101
|
+
- - ">="
|
102
|
+
- !ruby/object:Gem::Version
|
103
|
+
version: '0'
|
104
|
+
type: :runtime
|
105
|
+
prerelease: false
|
106
|
+
version_requirements: !ruby/object:Gem::Requirement
|
107
|
+
requirements:
|
108
|
+
- - ">="
|
109
|
+
- !ruby/object:Gem::Version
|
110
|
+
version: '0'
|
111
|
+
description: Ruby gem developed to help with pagination, filtering, sorting, and including
|
112
|
+
associations on both active record queries and custom sql queries
|
113
|
+
email:
|
114
|
+
- eamigo13@gmail.com
|
115
|
+
executables: []
|
116
|
+
extensions: []
|
117
|
+
extra_rdoc_files: []
|
118
|
+
files:
|
119
|
+
- ".gitignore"
|
120
|
+
- ".rspec"
|
121
|
+
- ".travis.yml"
|
122
|
+
- CODE_OF_CONDUCT.md
|
123
|
+
- Gemfile
|
124
|
+
- Gemfile.lock
|
125
|
+
- LICENSE.txt
|
126
|
+
- README.md
|
127
|
+
- Rakefile
|
128
|
+
- bin/console
|
129
|
+
- bin/setup
|
130
|
+
- lib/pattern_query_helper.rb
|
131
|
+
- lib/pattern_query_helper/associations.rb
|
132
|
+
- lib/pattern_query_helper/filtering.rb
|
133
|
+
- lib/pattern_query_helper/pagination.rb
|
134
|
+
- lib/pattern_query_helper/sorting.rb
|
135
|
+
- lib/pattern_query_helper/sql.rb
|
136
|
+
- lib/pattern_query_helper/version.rb
|
137
|
+
- pattern_query_helper.gemspec
|
138
|
+
homepage: https://github.com/iserve-products/pattern_query_helper
|
139
|
+
licenses:
|
140
|
+
- MIT
|
141
|
+
metadata: {}
|
142
|
+
post_install_message:
|
143
|
+
rdoc_options: []
|
144
|
+
require_paths:
|
145
|
+
- lib
|
146
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
147
|
+
requirements:
|
148
|
+
- - ">="
|
149
|
+
- !ruby/object:Gem::Version
|
150
|
+
version: '0'
|
151
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
152
|
+
requirements:
|
153
|
+
- - ">="
|
154
|
+
- !ruby/object:Gem::Version
|
155
|
+
version: '0'
|
156
|
+
requirements: []
|
157
|
+
rubyforge_project:
|
158
|
+
rubygems_version: 2.6.14
|
159
|
+
signing_key:
|
160
|
+
specification_version: 4
|
161
|
+
summary: Ruby Gem to help with pagination and data formatting at Pattern, Inc.
|
162
|
+
test_files: []
|