querifier 0.1.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +33 -0
- data/lib/querifier/queries/base.rb +3 -8
- data/lib/querifier/queries/order.rb +14 -4
- data/lib/querifier/queries/where.rb +8 -3
- data/lib/querifier/version.rb +1 -1
- data/querifier.gemspec +1 -1
- metadata +4 -5
- data/.byebug_history +0 -53
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: '0384ae9c72a559364bfcc1ff5a8727e23e333083'
|
4
|
+
data.tar.gz: 92eb1c2528a31373da3bbce219299fe9b5bc6542
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fa625d8291d0fafb3c89f8b8926cdfdf4367c392efb76bc5cb9778407b082928ad9d7bb0672dda1b5a27b19cb4d491e38498185706154937652b39fee2572ff2
|
7
|
+
data.tar.gz: 9b882bc922be3d74f548b425e2b66db08354f7a398cbde06e11d5fd9fb3118de458d25ad87ee21dfddfa551b323d859dc3833645fa2833b7799dca5f07c0c286
|
data/README.md
CHANGED
@@ -69,6 +69,39 @@ class BookController < ApiController
|
|
69
69
|
end
|
70
70
|
```
|
71
71
|
|
72
|
+
## Custom model class
|
73
|
+
|
74
|
+
Querifier will try to assume the class for your model removing the `Query` from your query classname, some examples are:
|
75
|
+
- BookQuery => Book
|
76
|
+
- BooksQuery => Books
|
77
|
+
- SomeModule::BookQuery => Book
|
78
|
+
|
79
|
+
If your model isn't called like your query, you can configure it with the entity_class method, something like:
|
80
|
+
|
81
|
+
```ruby
|
82
|
+
class BookQuery
|
83
|
+
include Querifier::Queries::Default
|
84
|
+
|
85
|
+
# If no order param is provided, then this order will be used
|
86
|
+
# default_sort { id: :asc }
|
87
|
+
# Configure these constants to add attributes to the ordering a filtering
|
88
|
+
where_attributes :id, :author_name # Configure your attributes here
|
89
|
+
order_attributes :id # Configure your attributes here
|
90
|
+
|
91
|
+
|
92
|
+
# This will replace the assumption with the class you send via parameter
|
93
|
+
# The :: are optional, but I recommend you to be explicit about the modules of your class
|
94
|
+
def self.default_collection
|
95
|
+
::SomeOtherClassName.all
|
96
|
+
end
|
97
|
+
|
98
|
+
def filter_by_author_name(value)
|
99
|
+
@collection = @collection.joins(:author).where(authors: { name: value })
|
100
|
+
end
|
101
|
+
end
|
102
|
+
|
103
|
+
```
|
104
|
+
|
72
105
|
|
73
106
|
## Custom methods
|
74
107
|
|
@@ -7,7 +7,7 @@ module Querifier
|
|
7
7
|
end
|
8
8
|
|
9
9
|
def collection
|
10
|
-
@collection ||= self.class.
|
10
|
+
@collection ||= self.class.default_collection
|
11
11
|
end
|
12
12
|
|
13
13
|
def params
|
@@ -19,13 +19,8 @@ module Querifier
|
|
19
19
|
end
|
20
20
|
|
21
21
|
module ClassMethods
|
22
|
-
def
|
23
|
-
|
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
|
22
|
+
def default_collection
|
23
|
+
@@default_collection ||= Object.const_get(self.name.split('::').last.split('Query').first).all
|
29
24
|
end
|
30
25
|
end
|
31
26
|
|
@@ -61,13 +61,23 @@ module Querifier
|
|
61
61
|
@@default_sort = { id: :asc }
|
62
62
|
|
63
63
|
def order_attributes(*value)
|
64
|
-
return
|
65
|
-
|
64
|
+
return class_variable_set :@@order_attributes, [*value] if value.any?
|
65
|
+
begin
|
66
|
+
class_variable_get :@@order_attributes
|
67
|
+
rescue NameError
|
68
|
+
class_variable_set :@@order_attributes, []
|
69
|
+
class_variable_get :@@order_attributes
|
70
|
+
end
|
66
71
|
end
|
67
72
|
|
68
73
|
def default_sort(value = nil)
|
69
|
-
return
|
70
|
-
|
74
|
+
return class_variable_set :@@default_sort, value if value
|
75
|
+
begin
|
76
|
+
class_variable_get :@@default_sort
|
77
|
+
rescue NameError
|
78
|
+
class_variable_set :@@default_sort, { id: :asc }
|
79
|
+
class_variable_get :@@default_sort
|
80
|
+
end
|
71
81
|
end
|
72
82
|
end
|
73
83
|
end
|
@@ -9,7 +9,7 @@ module Querifier
|
|
9
9
|
|
10
10
|
def filter
|
11
11
|
return self if @filtered
|
12
|
-
filter_params.each do |attribute|
|
12
|
+
filter_params.each do |(attribute, _)|
|
13
13
|
value = filter_value(attribute)
|
14
14
|
send("filter_by_#{attribute}", value) if value
|
15
15
|
end
|
@@ -50,8 +50,13 @@ module Querifier
|
|
50
50
|
@@where_attributes = []
|
51
51
|
|
52
52
|
def where_attributes(*value)
|
53
|
-
return
|
54
|
-
|
53
|
+
return class_variable_set :@@where_attributes, [*value] if value.any?
|
54
|
+
begin
|
55
|
+
class_variable_get :@@where_attributes
|
56
|
+
rescue NameError
|
57
|
+
class_variable_set :@@where_attributes, []
|
58
|
+
class_variable_get :@@where_attributes
|
59
|
+
end
|
55
60
|
end
|
56
61
|
end
|
57
62
|
end
|
data/lib/querifier/version.rb
CHANGED
data/querifier.gemspec
CHANGED
@@ -32,6 +32,6 @@ Gem::Specification.new do |spec|
|
|
32
32
|
spec.add_dependency 'railties', '>= 4.1.0', '< 6'
|
33
33
|
# TODO
|
34
34
|
spec.add_development_dependency "bundler", "~> 1.16"
|
35
|
-
spec.add_development_dependency "rake", "~>
|
35
|
+
spec.add_development_dependency "rake", "~> 12.3"
|
36
36
|
spec.add_development_dependency "rspec", "~> 3.0"
|
37
37
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: querifier
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Hugo Farji
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2018-
|
11
|
+
date: 2018-12-04 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: railties
|
@@ -50,14 +50,14 @@ dependencies:
|
|
50
50
|
requirements:
|
51
51
|
- - "~>"
|
52
52
|
- !ruby/object:Gem::Version
|
53
|
-
version: '
|
53
|
+
version: '12.3'
|
54
54
|
type: :development
|
55
55
|
prerelease: false
|
56
56
|
version_requirements: !ruby/object:Gem::Requirement
|
57
57
|
requirements:
|
58
58
|
- - "~>"
|
59
59
|
- !ruby/object:Gem::Version
|
60
|
-
version: '
|
60
|
+
version: '12.3'
|
61
61
|
- !ruby/object:Gem::Dependency
|
62
62
|
name: rspec
|
63
63
|
requirement: !ruby/object:Gem::Requirement
|
@@ -79,7 +79,6 @@ executables: []
|
|
79
79
|
extensions: []
|
80
80
|
extra_rdoc_files: []
|
81
81
|
files:
|
82
|
-
- ".byebug_history"
|
83
82
|
- ".gitignore"
|
84
83
|
- ".rspec"
|
85
84
|
- ".travis.yml"
|
data/.byebug_history
DELETED
@@ -1,53 +0,0 @@
|
|
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
|