filtering 0.1.2 → 0.1.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/lib/filtering/base.rb +34 -14
- data/lib/filtering/version.rb +1 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 4b170e2aad62fcfa7b6dfff8cb8b0813f9302258
|
4
|
+
data.tar.gz: 18abc83af13f89396ab1e13736731697c20d2f14
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e6f6c2791d7231e58f421d4803d2320b532b9304ffdaa55d0fc4c21743d97635eb5132933f8ae210d0ba292c94e420cb1e1666f460e4b977d7567b32bf6ec712
|
7
|
+
data.tar.gz: 942b44852d0234d08eae35cc9579d3b573f1e864318d6bf3686ac151c1a5358dc071cb2e95fd389d3d4db5ee85359b715c7d728d04442aeca7615e81918787be
|
data/README.md
CHANGED
@@ -66,7 +66,7 @@ Controller usage example:
|
|
66
66
|
|
67
67
|
```
|
68
68
|
def index
|
69
|
-
render json: Filters::UsersFilter.new(params, page: params[:page]).call
|
69
|
+
render json: Filters::UsersFilter.new(params, page: params[:page], order: :asc).call
|
70
70
|
end
|
71
71
|
```
|
72
72
|
If you don't use Kaminari just delete page from initializer
|
data/lib/filtering/base.rb
CHANGED
@@ -1,50 +1,62 @@
|
|
1
1
|
class Filtering::Base
|
2
2
|
def initialize(params, args = {})
|
3
|
-
@plain_params =
|
4
|
-
@complex_params =
|
3
|
+
@plain_params = permit(params, plain_acessible_params)
|
4
|
+
@complex_params = permit(params, complex_acessible_params)
|
5
5
|
@page = args[:page]
|
6
|
-
@
|
6
|
+
@order = args[:order]
|
7
7
|
end
|
8
8
|
|
9
9
|
def call
|
10
|
-
|
10
|
+
@result = relation
|
11
11
|
|
12
12
|
filter_by_plain_params unless plain_params.empty?
|
13
13
|
filter_by_complex_params unless complex_params.empty?
|
14
14
|
|
15
|
-
|
16
|
-
|
15
|
+
paginate if result.respond_to?(:page)
|
16
|
+
ordering if order.present?
|
17
|
+
|
18
|
+
return result
|
17
19
|
end
|
18
20
|
|
19
21
|
protected
|
20
22
|
|
21
|
-
attr_reader :plain_params, :complex_params, :page, :
|
23
|
+
attr_reader :plain_params, :complex_params, :page, :order, :result
|
22
24
|
|
23
25
|
def relation
|
24
26
|
raise 'relation method not implemented. Add this method to calling class and add AR relation, for example Model.all'
|
25
27
|
end
|
26
28
|
|
27
|
-
def
|
28
|
-
|
29
|
-
end
|
29
|
+
def permit(params, acessible_params)
|
30
|
+
return {} if acessible_params.nil?
|
30
31
|
|
31
|
-
|
32
|
-
|
32
|
+
if params.respond_to?(:permit)
|
33
|
+
params.permit(acessible_params)
|
34
|
+
else
|
35
|
+
params.symbolize_keys.slice(*acessible_params.map(&:to_sym))
|
36
|
+
end
|
33
37
|
end
|
34
38
|
|
39
|
+
def plain_acessible_params; end
|
40
|
+
|
41
|
+
def complex_acessible_params; end
|
42
|
+
|
35
43
|
private
|
36
44
|
|
37
45
|
def filter_by_plain_params
|
46
|
+
return if plain_params.empty?
|
47
|
+
|
38
48
|
plain_params.each do |key, value|
|
39
|
-
@
|
49
|
+
@result = result.where("#{key}": value) if value.present?
|
40
50
|
end
|
41
51
|
end
|
42
52
|
|
43
53
|
def filter_by_complex_params
|
54
|
+
return if complex_params.empty?
|
55
|
+
|
44
56
|
raise_if_complex_method_not_implemented
|
45
57
|
|
46
58
|
complex_params.each do |key, value|
|
47
|
-
@
|
59
|
+
@result = send("filter_by_#{key}", value)
|
48
60
|
end
|
49
61
|
end
|
50
62
|
|
@@ -54,4 +66,12 @@ class Filtering::Base
|
|
54
66
|
raise "#{method_name} method not implemented. Add #{method_name} private method to #{self.class.name} or remove #{param} param from complex_acessible_params" unless self.private_methods.include?(method_name.to_sym)
|
55
67
|
end
|
56
68
|
end
|
69
|
+
|
70
|
+
def paginate
|
71
|
+
@result = result.page(page)
|
72
|
+
end
|
73
|
+
|
74
|
+
def ordering
|
75
|
+
@result = result.order(order)
|
76
|
+
end
|
57
77
|
end
|
data/lib/filtering/version.rb
CHANGED