querifier 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/.byebug_history +53 -0
- data/.gitignore +12 -0
- data/.rspec +4 -0
- data/.travis.yml +7 -0
- data/CODE_OF_CONDUCT.md +74 -0
- data/Gemfile +11 -0
- data/LICENSE.txt +21 -0
- data/README.md +140 -0
- data/Rakefile +6 -0
- data/bin/console +14 -0
- data/bin/setup +8 -0
- data/lib/generators/querifier/install_generator.rb +12 -0
- data/lib/generators/querifier/query_generator.rb +12 -0
- data/lib/generators/querifier/templates/querifier.rb +7 -0
- data/lib/generators/querifier/templates/query.rb.erb +9 -0
- data/lib/querifier/config.rb +26 -0
- data/lib/querifier/queries/base.rb +40 -0
- data/lib/querifier/queries/default.rb +11 -0
- data/lib/querifier/queries/order.rb +75 -0
- data/lib/querifier/queries/where.rb +58 -0
- data/lib/querifier/version.rb +3 -0
- data/lib/querifier.rb +15 -0
- data/querifier.gemspec +37 -0
- metadata +129 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 701de1ffaba4c760abe580c62e16985eb68df74f
|
4
|
+
data.tar.gz: 8b53ec5cdd62b5c98dfb8e2fbb6854e45c9fd64f
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: e51da15b1102a7f5d2dfc896cd7d1b0dd6623613d2746722311c21f39b2154aadc83ddbc862184f7754296ab51c45dd52f570f37b192e1c45f47ccbf587f70da
|
7
|
+
data.tar.gz: af643144ff3e1ecd28835b548dee2af206327ebcb2a716667bc60ab3b80dd57aedcce2127b938606651565d31ce3fa19f5b44d1fedc60fefe96367bbc420c381
|
data/.byebug_history
ADDED
@@ -0,0 +1,53 @@
|
|
1
|
+
continue
|
2
|
+
klass
|
3
|
+
exit
|
4
|
+
continue
|
5
|
+
option
|
6
|
+
exit
|
7
|
+
!!!
|
8
|
+
continue
|
9
|
+
exit
|
10
|
+
params.fetch(:order, {}).find_all { |(k, _)| self.class::ORDER_ATTRIBUTES.include? k }.to_h
|
11
|
+
params.fetch(:order, {}).find_all { |(k, _)| self.class::ORDER_ATTRIBUTES.include? k }.flatten
|
12
|
+
params.fetch(:order, {}).find_all { |(k, _)| self.class::ORDER_ATTRIBUTES.include? k }
|
13
|
+
params.fetch(:order)
|
14
|
+
)
|
15
|
+
params.fetch(:order
|
16
|
+
exit
|
17
|
+
option && direction && self.class::ORDER_ATTRIBUTES.include?(option) && %w[asc desc].include?(direction.to_s.downcase)
|
18
|
+
option && direction && self.class::ORDER_ATTRIBUTES.include?(option)
|
19
|
+
option && direction
|
20
|
+
direction
|
21
|
+
option
|
22
|
+
exit
|
23
|
+
!!!
|
24
|
+
continue
|
25
|
+
%w[asc desc].include?(direction.to_s.downcase)
|
26
|
+
%w[asc desc].include?(direction.downcase)
|
27
|
+
self.class::ORDER_ATTRIBUTES.include?(option)
|
28
|
+
valid_sort?(option, direction)
|
29
|
+
order_params
|
30
|
+
continue
|
31
|
+
order_params
|
32
|
+
direction
|
33
|
+
option
|
34
|
+
continue
|
35
|
+
f
|
36
|
+
continue
|
37
|
+
self.class.entity_class.where
|
38
|
+
self.class.entity_class
|
39
|
+
self.class
|
40
|
+
self
|
41
|
+
continue
|
42
|
+
@collection
|
43
|
+
continue
|
44
|
+
@collection
|
45
|
+
collection
|
46
|
+
continue
|
47
|
+
@collection
|
48
|
+
continue
|
49
|
+
@collection
|
50
|
+
continue
|
51
|
+
value
|
52
|
+
continue
|
53
|
+
value
|
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 hdf1986.6700@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/LICENSE.txt
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
The MIT License (MIT)
|
2
|
+
|
3
|
+
Copyright (c) 2018 Hugo Farji
|
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,140 @@
|
|
1
|
+
[![Build Status](https://travis-ci.com/hdf1986/querifier.svg?branch=master)](https://travis-ci.com/hdf1986/querifier)
|
2
|
+
# Querifier
|
3
|
+
|
4
|
+
Querifier is a gem intended to create queries for api's easy & fast, it isn't an ORM, instead, it's a layer over it.
|
5
|
+
|
6
|
+
The basic use case is when you have an API and you want to filter & order the results without having to think too much
|
7
|
+
|
8
|
+
## Installation
|
9
|
+
|
10
|
+
Add this line to your application's Gemfile:
|
11
|
+
|
12
|
+
```ruby
|
13
|
+
gem 'querifier'
|
14
|
+
```
|
15
|
+
|
16
|
+
And then execute:
|
17
|
+
|
18
|
+
```shell
|
19
|
+
bundle
|
20
|
+
```
|
21
|
+
|
22
|
+
If you are using rails, you can do:
|
23
|
+
|
24
|
+
```
|
25
|
+
rails generate querifier:install
|
26
|
+
```
|
27
|
+
|
28
|
+
## Usage
|
29
|
+
If you are using rails, you can do:
|
30
|
+
|
31
|
+
```
|
32
|
+
rails generate querifier:query your_model
|
33
|
+
```
|
34
|
+
|
35
|
+
it will create a query for your model, in this case it will be
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
class BookQuery
|
39
|
+
include Querifier::Queries::Default
|
40
|
+
|
41
|
+
# If no order param is provided, then this order will be used
|
42
|
+
# default_sort { id: :asc }
|
43
|
+
# Configure these constants to add attributes to the ordering a filtering
|
44
|
+
where_attributes :id # Configure your attributes here
|
45
|
+
order_attributes :id # Configure your attributes here
|
46
|
+
end
|
47
|
+
|
48
|
+
```
|
49
|
+
|
50
|
+
After that, your query will be able to receive params like this
|
51
|
+
```ruby
|
52
|
+
# Will return an array or query with the filtered and ordered results
|
53
|
+
BookQuery.new(filter: { order: { id: desc }, where: { id: 1 } }).collection
|
54
|
+
```
|
55
|
+
|
56
|
+
This work great with Rails controllers, you can start filtering and ordering with something like this:
|
57
|
+
|
58
|
+
```ruby
|
59
|
+
class BookController < ApiController
|
60
|
+
def index
|
61
|
+
render json: BookQuery.new(params).collection
|
62
|
+
end
|
63
|
+
end
|
64
|
+
```
|
65
|
+
|
66
|
+
|
67
|
+
## Custom methods
|
68
|
+
|
69
|
+
In case you arrive to a case where you need a filter different than the default ones, you can do something like this:
|
70
|
+
|
71
|
+
```ruby
|
72
|
+
class BookQuery
|
73
|
+
include Querifier::Queries::Default
|
74
|
+
|
75
|
+
# If no order param is provided, then this order will be used
|
76
|
+
# default_sort { id: :asc }
|
77
|
+
# Configure these constants to add attributes to the ordering a filtering
|
78
|
+
where_attributes :id, :author_name # Configure your attributes here
|
79
|
+
order_attributes :id # Configure your attributes here
|
80
|
+
|
81
|
+
def filter_by_author_name(value)
|
82
|
+
@collection = @collection.joins(:author).where(authors: { name: value })
|
83
|
+
end
|
84
|
+
end
|
85
|
+
|
86
|
+
```
|
87
|
+
|
88
|
+
You can do the same with the ordering
|
89
|
+
|
90
|
+
```ruby
|
91
|
+
class BookQuery
|
92
|
+
include Querifier::Queries::Default
|
93
|
+
|
94
|
+
# If no order param is provided, then this order will be used
|
95
|
+
# default_sort { id: :asc }
|
96
|
+
# Configure these constants to add attributes to the ordering a filtering
|
97
|
+
where_attributes :id, :author_name # Configure your attributes here
|
98
|
+
order_attributes :id # Configure your attributes here
|
99
|
+
|
100
|
+
def order_by_author_name(direction)
|
101
|
+
@collection = @collection.joins(:author).order("authors.name #{direction}")
|
102
|
+
# Don't pannic! I know we are concatenating a raw value to the query, but in this case this is being validated in the invocation of this method
|
103
|
+
# In case you have any doubt about it, check https://github.com/hdf1986/querifier/blob/master/lib/querifier/queries/order.rb valid_sort? method
|
104
|
+
# Im totally open to better ways of doing this, since i didn't find a nice way to implement joined and dinamic ordering queries
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
```
|
109
|
+
|
110
|
+
## Good to know
|
111
|
+
- You can create a filter_by_* or order_by_* method for any name you want to, just take care that if it doesn't exist in the database, it will need a custom method as seen before
|
112
|
+
- The filters are executed in the order they are received from the `.new` method, this is a coincidence, so i can't ensure it will keep happening in the future
|
113
|
+
- Most of this structure is inspired by Loopback REST API for querying data (see https://loopback.io/doc/en/lb3/Querying-data.html). I don't like loopback at all, but i think this standard is a good place to start with
|
114
|
+
- Probably there's some minor performance differences between custom methods and default ones (the custom ones being the faster ones), because we use `method_missing` magic to implement the default ones
|
115
|
+
- If you don't want to use where, or order, you can include just `Querifier::Queries::Order` or `Querifier::Queries::Where` instead of `Querifier::Queries::Default`, according to your needs
|
116
|
+
|
117
|
+
## To-do's
|
118
|
+
|
119
|
+
- Support for greather than where filter
|
120
|
+
- Support for lower than where filter
|
121
|
+
- Support for equal than where filter (currently we are using sql LIKE by default)
|
122
|
+
- Performance metrics
|
123
|
+
- Permit multiple order attributes: We are supporting this in the structure, but in the practice we are ignoring the second or greather elements
|
124
|
+
- Add support for different adapters: Currently we are assuming that the ORM is something similar to ActiveRecord, i don't think it's a good idea to be tied to ActiveRecord, so will be great to provide some sort of customizable things there
|
125
|
+
|
126
|
+
## Development
|
127
|
+
|
128
|
+
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.
|
129
|
+
|
130
|
+
## Contributing
|
131
|
+
|
132
|
+
Bug reports and pull requests are welcome on GitHub at https://github.com/hdf1986/querifier. 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.
|
133
|
+
|
134
|
+
## License
|
135
|
+
|
136
|
+
The gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).
|
137
|
+
|
138
|
+
## Code of Conduct
|
139
|
+
|
140
|
+
Everyone interacting in the Querifier project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/Querifier/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 "Querifier"
|
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,12 @@
|
|
1
|
+
module Querifier
|
2
|
+
module Generators
|
3
|
+
class InstallGenerator < ::Rails::Generators::Base
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
desc 'Creates Querifier initializer for your application'
|
6
|
+
|
7
|
+
def copy_initializer
|
8
|
+
template 'querifier.rb', 'config/initializers/querifier.rb'
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,12 @@
|
|
1
|
+
module Querifier
|
2
|
+
module Generators
|
3
|
+
class QueryGenerator < ::Rails::Generators::NamedBase
|
4
|
+
source_root File.expand_path('../templates', __FILE__)
|
5
|
+
desc 'Creates Querifier query for your application'
|
6
|
+
|
7
|
+
def create_service
|
8
|
+
template 'query.rb.erb', "app/queries/#{plural_name.singularize}_query.rb"
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
end
|
@@ -0,0 +1,7 @@
|
|
1
|
+
Querifier.configure do |config|
|
2
|
+
# This is used to identify the filter params, with this configuration the query should be:
|
3
|
+
# ?filter[where][field_name]=something&filter[order][field_name]=desc
|
4
|
+
config.where_param = :where
|
5
|
+
config.order_param = :order
|
6
|
+
config.filter_param = :filter
|
7
|
+
end
|
@@ -0,0 +1,9 @@
|
|
1
|
+
class <%= class_name %>Query
|
2
|
+
include Querifier::Queries::Default
|
3
|
+
|
4
|
+
# If no order param is provided, then this order will be used
|
5
|
+
# default_sort { id: :asc }
|
6
|
+
# Configure these constants to add attributes to the ordering a filtering
|
7
|
+
where_attributes :id
|
8
|
+
order_attributes :id
|
9
|
+
end
|
@@ -0,0 +1,26 @@
|
|
1
|
+
module Querifier
|
2
|
+
module Config
|
3
|
+
DEFAULTS_CONFIGS = {
|
4
|
+
where_param: :where,
|
5
|
+
order_param: :order,
|
6
|
+
filter_param: :filter
|
7
|
+
}.freeze
|
8
|
+
|
9
|
+
module_function
|
10
|
+
|
11
|
+
DEFAULTS_CONFIGS.each do |key, value|
|
12
|
+
define_method key do
|
13
|
+
instance_variable_get("@#{key}") || instance_variable_set("@#{key}", value)
|
14
|
+
end
|
15
|
+
|
16
|
+
define_method "#{key}=" do |v|
|
17
|
+
instance_variable_set("@#{key}", v)
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
# This is mostly useful for the tests
|
22
|
+
def reset!
|
23
|
+
DEFAULTS_CONFIGS.each { |k, v| send("#{k}=", v) }
|
24
|
+
end
|
25
|
+
end
|
26
|
+
end
|
@@ -0,0 +1,40 @@
|
|
1
|
+
module Querifier
|
2
|
+
module Queries
|
3
|
+
module Base
|
4
|
+
def initialize(params)
|
5
|
+
@params = params
|
6
|
+
check_params!
|
7
|
+
end
|
8
|
+
|
9
|
+
def collection
|
10
|
+
@collection ||= self.class.entity_class.all
|
11
|
+
end
|
12
|
+
|
13
|
+
def params
|
14
|
+
@params.fetch(Config.filter_param, {})
|
15
|
+
end
|
16
|
+
|
17
|
+
def self.included(klass)
|
18
|
+
klass.extend(ClassMethods)
|
19
|
+
end
|
20
|
+
|
21
|
+
module ClassMethods
|
22
|
+
def entity_class(value = nil)
|
23
|
+
return entity_class = value unless value.nil?
|
24
|
+
@@entity_class ||= Object.const_get self.name.split('::').last.split('Query').first
|
25
|
+
end
|
26
|
+
|
27
|
+
def entity_class=(value)
|
28
|
+
@@entity_class = value
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def check_params!
|
35
|
+
return if @params.respond_to?(:keys) && @params.respond_to?(:has_key?)
|
36
|
+
raise ArgumentError, "The params should extend of a hash, instead, they are a #{@params.class}" # rubocop:disable Metrics/LineLength
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
40
|
+
end
|
@@ -0,0 +1,75 @@
|
|
1
|
+
module Querifier
|
2
|
+
module Queries
|
3
|
+
module Order
|
4
|
+
def collection
|
5
|
+
super
|
6
|
+
order
|
7
|
+
@collection
|
8
|
+
end
|
9
|
+
|
10
|
+
def order
|
11
|
+
return self if @ordered
|
12
|
+
if valid_sort?
|
13
|
+
# TODO: Permit multiple orders
|
14
|
+
option = order_params.keys.first
|
15
|
+
direction = order_params.fetch(option, nil)
|
16
|
+
send("order_by_#{option}", direction)
|
17
|
+
else
|
18
|
+
order_by_default
|
19
|
+
end
|
20
|
+
@ordered = true
|
21
|
+
self
|
22
|
+
end
|
23
|
+
|
24
|
+
def order_by_default
|
25
|
+
order_by(*self.class.default_sort.to_a.flatten)
|
26
|
+
end
|
27
|
+
|
28
|
+
def order_by(option, direction)
|
29
|
+
@collection = @collection.order(option => direction)
|
30
|
+
end
|
31
|
+
|
32
|
+
def valid_sort?
|
33
|
+
option = order_params.keys.first
|
34
|
+
direction = order_params.fetch(option, nil)
|
35
|
+
option &&
|
36
|
+
direction &&
|
37
|
+
valid_option?(option) &&
|
38
|
+
%w[asc desc].include?(direction.to_s.downcase)
|
39
|
+
end
|
40
|
+
|
41
|
+
def valid_option?(option)
|
42
|
+
self.class.order_attributes.include?(option.to_sym)
|
43
|
+
end
|
44
|
+
|
45
|
+
def method_missing(message, *args, &block)
|
46
|
+
return order_by(Regexp.last_match(1).to_sym, *args) if message.to_s =~ /order_by_(.*)/ # rubocop:disable Performance/RegexpMatch, Metrics/LineLength
|
47
|
+
super
|
48
|
+
end
|
49
|
+
|
50
|
+
def order_params
|
51
|
+
@order_params ||=
|
52
|
+
params.fetch(Config.order_param, {}).select { |k| valid_option? k.to_sym }
|
53
|
+
end
|
54
|
+
|
55
|
+
def self.included(klass)
|
56
|
+
klass.extend(ClassMethods)
|
57
|
+
end
|
58
|
+
|
59
|
+
module ClassMethods
|
60
|
+
@@order_attributes = []
|
61
|
+
@@default_sort = { id: :asc }
|
62
|
+
|
63
|
+
def order_attributes(*value)
|
64
|
+
return @@order_attributes = [*value] if value.any?
|
65
|
+
@@order_attributes
|
66
|
+
end
|
67
|
+
|
68
|
+
def default_sort(value = nil)
|
69
|
+
return @@default_sort = value if value
|
70
|
+
@@default_sort
|
71
|
+
end
|
72
|
+
end
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
@@ -0,0 +1,58 @@
|
|
1
|
+
module Querifier
|
2
|
+
module Queries
|
3
|
+
module Where
|
4
|
+
def collection
|
5
|
+
super
|
6
|
+
filter
|
7
|
+
@collection
|
8
|
+
end
|
9
|
+
|
10
|
+
def filter
|
11
|
+
return self if @filtered
|
12
|
+
filter_params.each do |(attribute, value)|
|
13
|
+
send("filter_by_#{attribute}", value) if value
|
14
|
+
end
|
15
|
+
@filtered = true
|
16
|
+
self
|
17
|
+
end
|
18
|
+
|
19
|
+
def filter_value(key)
|
20
|
+
filter_params.fetch(key.to_sym, nil)
|
21
|
+
end
|
22
|
+
|
23
|
+
def filter_by(name, value)
|
24
|
+
filter_content = filter_value(name)
|
25
|
+
@collection = @collection.where("#{name} LIKE ?", "%#{filter_content}%") if filter_content
|
26
|
+
self
|
27
|
+
end
|
28
|
+
|
29
|
+
def method_missing(message, *args, &block)
|
30
|
+
return filter_by(Regexp.last_match(1).to_sym, args.first) if message.to_s =~ /filter_by_(.*)/ # rubocop:disable Performance/RegexpMatch, Metrics/LineLength
|
31
|
+
super
|
32
|
+
end
|
33
|
+
|
34
|
+
def valid_option?(option)
|
35
|
+
self.class.where_attributes.include?(option.to_sym)
|
36
|
+
end
|
37
|
+
|
38
|
+
def filter_params
|
39
|
+
@filter_params ||=
|
40
|
+
params.fetch(Config.where_param, {}).select { |k| valid_option? k.to_sym }
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
def self.included(klass)
|
45
|
+
klass.extend(ClassMethods)
|
46
|
+
end
|
47
|
+
|
48
|
+
module ClassMethods
|
49
|
+
@@where_attributes = []
|
50
|
+
|
51
|
+
def where_attributes(*value)
|
52
|
+
return @@where_attributes = [*value] if value.any?
|
53
|
+
@@where_attributes
|
54
|
+
end
|
55
|
+
end
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
data/lib/querifier.rb
ADDED
@@ -0,0 +1,15 @@
|
|
1
|
+
require "querifier/version"
|
2
|
+
require "querifier/config"
|
3
|
+
require "querifier/queries/base"
|
4
|
+
require "querifier/queries/where"
|
5
|
+
require "querifier/queries/order"
|
6
|
+
require "querifier/queries/default"
|
7
|
+
|
8
|
+
# TODO: Add greather than
|
9
|
+
# TODO: Add lower than
|
10
|
+
# TODO: Add equal than
|
11
|
+
module Querifier
|
12
|
+
def self.configure
|
13
|
+
yield Config
|
14
|
+
end
|
15
|
+
end
|
data/querifier.gemspec
ADDED
@@ -0,0 +1,37 @@
|
|
1
|
+
|
2
|
+
lib = File.expand_path("../lib", __FILE__)
|
3
|
+
$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
|
4
|
+
require "querifier/version"
|
5
|
+
|
6
|
+
Gem::Specification.new do |spec|
|
7
|
+
spec.name = "querifier"
|
8
|
+
spec.version = Querifier::VERSION
|
9
|
+
spec.authors = ["Hugo Farji"]
|
10
|
+
spec.email = ["hugo.farji@gmail.com"]
|
11
|
+
|
12
|
+
# TODO
|
13
|
+
spec.summary = %q{ Write a short summary, because RubyGems requires one.}
|
14
|
+
# TODO
|
15
|
+
spec.description = %q{ Write a longer description or delete this line.}
|
16
|
+
# TODO
|
17
|
+
spec.homepage = "https://google.com.ar"
|
18
|
+
spec.license = "MIT"
|
19
|
+
|
20
|
+
# Prevent pushing this gem to RubyGems.org. To allow pushes either set the 'allowed_push_host'
|
21
|
+
# to allow pushing to a single host or delete this section to allow pushing to any host.
|
22
|
+
|
23
|
+
# Specify which files should be added to the gem when it is released.
|
24
|
+
# The `git ls-files -z` loads the files in the RubyGem that have been added into git.
|
25
|
+
spec.files = Dir.chdir(File.expand_path('..', __FILE__)) do
|
26
|
+
`git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
|
27
|
+
end
|
28
|
+
spec.bindir = "exe"
|
29
|
+
spec.executables = spec.files.grep(%r{^exe/}) { |f| File.basename(f) }
|
30
|
+
spec.require_paths = ["lib"]
|
31
|
+
|
32
|
+
spec.add_dependency 'railties', '>= 4.1.0', '< 6'
|
33
|
+
# TODO
|
34
|
+
spec.add_development_dependency "bundler", "~> 1.16"
|
35
|
+
spec.add_development_dependency "rake", "~> 10.0"
|
36
|
+
spec.add_development_dependency "rspec", "~> 3.0"
|
37
|
+
end
|
metadata
ADDED
@@ -0,0 +1,129 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: querifier
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Hugo Farji
|
8
|
+
autorequire:
|
9
|
+
bindir: exe
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-11-01 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: railties
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - ">="
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: 4.1.0
|
20
|
+
- - "<"
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: '6'
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - ">="
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: 4.1.0
|
30
|
+
- - "<"
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '6'
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: bundler
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '1.16'
|
40
|
+
type: :development
|
41
|
+
prerelease: false
|
42
|
+
version_requirements: !ruby/object:Gem::Requirement
|
43
|
+
requirements:
|
44
|
+
- - "~>"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '1.16'
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: rake
|
49
|
+
requirement: !ruby/object:Gem::Requirement
|
50
|
+
requirements:
|
51
|
+
- - "~>"
|
52
|
+
- !ruby/object:Gem::Version
|
53
|
+
version: '10.0'
|
54
|
+
type: :development
|
55
|
+
prerelease: false
|
56
|
+
version_requirements: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - "~>"
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: '10.0'
|
61
|
+
- !ruby/object:Gem::Dependency
|
62
|
+
name: rspec
|
63
|
+
requirement: !ruby/object:Gem::Requirement
|
64
|
+
requirements:
|
65
|
+
- - "~>"
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
version: '3.0'
|
68
|
+
type: :development
|
69
|
+
prerelease: false
|
70
|
+
version_requirements: !ruby/object:Gem::Requirement
|
71
|
+
requirements:
|
72
|
+
- - "~>"
|
73
|
+
- !ruby/object:Gem::Version
|
74
|
+
version: '3.0'
|
75
|
+
description: " Write a longer description or delete this line."
|
76
|
+
email:
|
77
|
+
- hugo.farji@gmail.com
|
78
|
+
executables: []
|
79
|
+
extensions: []
|
80
|
+
extra_rdoc_files: []
|
81
|
+
files:
|
82
|
+
- ".byebug_history"
|
83
|
+
- ".gitignore"
|
84
|
+
- ".rspec"
|
85
|
+
- ".travis.yml"
|
86
|
+
- CODE_OF_CONDUCT.md
|
87
|
+
- Gemfile
|
88
|
+
- LICENSE.txt
|
89
|
+
- README.md
|
90
|
+
- Rakefile
|
91
|
+
- bin/console
|
92
|
+
- bin/setup
|
93
|
+
- lib/generators/querifier/install_generator.rb
|
94
|
+
- lib/generators/querifier/query_generator.rb
|
95
|
+
- lib/generators/querifier/templates/querifier.rb
|
96
|
+
- lib/generators/querifier/templates/query.rb.erb
|
97
|
+
- lib/querifier.rb
|
98
|
+
- lib/querifier/config.rb
|
99
|
+
- lib/querifier/queries/base.rb
|
100
|
+
- lib/querifier/queries/default.rb
|
101
|
+
- lib/querifier/queries/order.rb
|
102
|
+
- lib/querifier/queries/where.rb
|
103
|
+
- lib/querifier/version.rb
|
104
|
+
- querifier.gemspec
|
105
|
+
homepage: https://google.com.ar
|
106
|
+
licenses:
|
107
|
+
- MIT
|
108
|
+
metadata: {}
|
109
|
+
post_install_message:
|
110
|
+
rdoc_options: []
|
111
|
+
require_paths:
|
112
|
+
- lib
|
113
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
114
|
+
requirements:
|
115
|
+
- - ">="
|
116
|
+
- !ruby/object:Gem::Version
|
117
|
+
version: '0'
|
118
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - ">="
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '0'
|
123
|
+
requirements: []
|
124
|
+
rubyforge_project:
|
125
|
+
rubygems_version: 2.6.11
|
126
|
+
signing_key:
|
127
|
+
specification_version: 4
|
128
|
+
summary: Write a short summary, because RubyGems requires one.
|
129
|
+
test_files: []
|