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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 9a507bc2561032ac497b5b4b5c7f83fbb8f545b1
4
- data.tar.gz: 8ef5df369108a7fdbc82ee8b3e5916611bd55040
3
+ metadata.gz: 4b170e2aad62fcfa7b6dfff8cb8b0813f9302258
4
+ data.tar.gz: 18abc83af13f89396ab1e13736731697c20d2f14
5
5
  SHA512:
6
- metadata.gz: f74fc8bcae8e7b94cee4428ad458d639641b487754d523fa714dbd08d3b2a445ea5673d22b103d96280c3542cee994f0198855ebea6b611c95f86c519b9e9cfd
7
- data.tar.gz: 399a1d6b716513cdea90060564d3443f3e0b80c0dcf455397f8103b26535ead923ee47f0650db57f999cae9fdef1347373a8e618976f63a9bcb12fe6c722767a
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
@@ -1,50 +1,62 @@
1
1
  class Filtering::Base
2
2
  def initialize(params, args = {})
3
- @plain_params = params.permit(plain_acessible_params) unless plain_acessible_params.empty?
4
- @complex_params = params.permit(complex_acessible_params) unless complex_acessible_params.empty?
3
+ @plain_params = permit(params, plain_acessible_params)
4
+ @complex_params = permit(params, complex_acessible_params)
5
5
  @page = args[:page]
6
- @results = relation
6
+ @order = args[:order]
7
7
  end
8
8
 
9
9
  def call
10
- raise "@relation not initialized at #{self.class.name}. Add @relation = Offer for example." if relation.nil?
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
- return results.page(page) if results.respond_to?(:page)
16
- results
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, :results
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 plain_acessible_params
28
- []
29
- end
29
+ def permit(params, acessible_params)
30
+ return {} if acessible_params.nil?
30
31
 
31
- def complex_acessible_params
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
- @results = results.where("#{key}": value) if value.present?
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
- @results = send("filter_by_#{key}", value)
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
@@ -1,3 +1,3 @@
1
1
  module Filtering
2
- VERSION = "0.1.2"
2
+ VERSION = "0.1.3"
3
3
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filtering
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - Vitaliy