fetcheable_on_api 0.1.8 → 0.1.9.0

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
  SHA256:
3
- metadata.gz: d557420a13e2668f210f92dd25263770b8345dc82b1cc68ec49171028a708b0c
4
- data.tar.gz: e4c9edbd515862b132c67d6a8cefd4a2e3d76283d931ffa7d9f43b436f271ef3
3
+ metadata.gz: 05a553bcde4c9048e8293d114936e2e9b4192e1499adcbc22bfdf78997a6e856
4
+ data.tar.gz: 532213cc5c36c522a8f2bcb0f46f7cbbf56ffeb94c8e1d83072bf18db6f6a768
5
5
  SHA512:
6
- metadata.gz: 5154c9bf6a13a635cbad72e6ab9f3a793598e3082c92714d397ef3f81687df8d69f83b6ab80c723cdb544f19e50479e79a2e43519e689ec0102f92a021c9c4bc
7
- data.tar.gz: 8de8a403e545f21477c2d2124d0c92a765ef1eae194d3635fc233352c4157086aaf03a4864987e571dfddecdbf70e1ce68c5182efb0e42aef99d1f7831bc01f8
6
+ metadata.gz: 02b31b34d84a9ff79b034881a3fe8d1cdbf62620572625ba977183daf498cbdd9a82134f3bafc9ddd413c348b9e08f9151c78f7abde4fb14542cbe3b586ef781
7
+ data.tar.gz: 61dda83e7a0a5fe2d07b6e251347c42282be5ac904ca64632c00f46314441feb012628f1a462e5a3ae6f7c6d499dfdc900852473ab7b924b0ae5ba0c96e15bd3
@@ -5,6 +5,28 @@ module FetcheableOnApi
5
5
  #
6
6
  # Supports
7
7
  #
8
+ PREDICATES_WITH_ARRAY = [
9
+ :does_not_match_all,
10
+ :does_not_match_any,
11
+ :eq_all,
12
+ :eq_any,
13
+ :gt_all,
14
+ :gt_any,
15
+ :gteq_all,
16
+ :gteq_any,
17
+ :in_all,
18
+ :in_any,
19
+ :lt_all,
20
+ :lt_any,
21
+ :lteq_all,
22
+ :lteq_any,
23
+ :matches_all,
24
+ :matches_any,
25
+ :not_eq_all,
26
+ :not_eq_any,
27
+ :not_in_all,
28
+ :not_in_any
29
+ ].freeze
8
30
 
9
31
  #
10
32
  # Public class methods
@@ -44,13 +66,28 @@ module FetcheableOnApi
44
66
  #
45
67
  protected
46
68
 
47
- def try_parse_array(values, format)
48
- array = JSON.parse(values)
49
- array.map! { |el| foa_string_to_datetime(el.to_s) } if format == :datetime
69
+ # def try_parse_array(values, format)
70
+ # array = JSON.parse(values)
71
+ # array.map! { |el| foa_string_to_datetime(el.to_s) } if format == :datetime
50
72
 
51
- [array]
52
- rescue JSON::ParserError
53
- nil
73
+ # [array]
74
+ # rescue JSON::ParserError
75
+ # nil
76
+ # end
77
+
78
+ def convert_to_datetime(array)
79
+ array.map { |el| foa_string_to_datetime(el.to_s) }
80
+ end
81
+
82
+ def valid_keys
83
+ keys = filters_configuration.keys
84
+ keys.each_with_index do |key, index|
85
+ predicate = filters_configuration[key.to_sym].fetch(:with, :ilike).to_sym
86
+
87
+ keys[index] = { key => [] } if PREDICATES_WITH_ARRAY.include?(predicate)
88
+ end
89
+
90
+ keys
54
91
  end
55
92
 
56
93
  def apply_filters(collection)
@@ -58,95 +95,105 @@ module FetcheableOnApi
58
95
  foa_valid_parameters!(:filter)
59
96
 
60
97
  filter_params = params.require(:filter)
61
- .permit(filters_configuration.keys)
98
+ .permit(valid_keys)
62
99
  .to_hash
63
100
 
64
101
  filtering = filter_params.map do |column, values|
65
- format = filters_configuration[column.to_sym].fetch(:format, :string)
66
- elements = try_parse_array(values, format)
67
- elements ||= values.split(',')
68
-
69
- elements.map do |value|
70
- column_name = filters_configuration[column.to_sym].fetch(:as, column)
71
- klass = filters_configuration[column.to_sym].fetch(:class_name, collection.klass)
72
- predicate = filters_configuration[column.to_sym].fetch(:with, :ilike)
73
-
74
- case predicate
75
- when :between
76
- klass.arel_table[column_name].between(value.first..value.last)
77
- when :does_not_match
78
- klass.arel_table[column_name].does_not_match("%#{value}%")
79
- when :does_not_match_all
80
- klass.arel_table[column_name].does_not_match_all(value)
81
- when :does_not_match_any
82
- klass.arel_table[column_name].does_not_match_any(value)
83
- when :eq
84
- klass.arel_table[column_name].eq(value)
85
- when :eq_all
86
- klass.arel_table[column_name].eq_all(value)
87
- when :eq_any
88
- klass.arel_table[column_name].eq_any(value)
89
- when :gt
90
- klass.arel_table[column_name].gt(value)
91
- when :gt_all
92
- klass.arel_table[column_name].gt_all(value)
93
- when :gt_any
94
- klass.arel_table[column_name].gt_any(value)
95
- when :gteq
96
- klass.arel_table[column_name].gteq(value)
97
- when :gteq_all
98
- klass.arel_table[column_name].gteq_all(value)
99
- when :gteq_any
100
- klass.arel_table[column_name].gteq_any(value)
101
- when :in
102
- klass.arel_table[column_name].in(value)
103
- when :in_all
104
- klass.arel_table[column_name].in_all(value)
105
- when :in_any
106
- klass.arel_table[column_name].in_any(value)
107
- when :lt
108
- klass.arel_table[column_name].lt(value)
109
- when :lt_all
110
- klass.arel_table[column_name].lt_all(value)
111
- when :lt_any
112
- klass.arel_table[column_name].lt_any(value)
113
- when :lteq
114
- klass.arel_table[column_name].lteq(value)
115
- when :lteq_all
116
- klass.arel_table[column_name].lteq_all(value)
117
- when :lteq_any
118
- klass.arel_table[column_name].lteq_any(value)
119
- when :ilike
120
- klass.arel_table[column_name].matches("%#{value}%")
121
- when :matches
122
- klass.arel_table[column_name].matches(value)
123
- when :matches_all
124
- klass.arel_table[column_name].matches_all(value)
125
- when :matches_any
126
- klass.arel_table[column_name].matches_any(value)
127
- when :not_between
128
- klass.arel_table[column_name].not_between(value.first..value.last)
129
- when :not_eq
130
- klass.arel_table[column_name].not_eq(value)
131
- when :not_eq_all
132
- klass.arel_table[column_name].not_eq_all(value)
133
- when :not_eq_any
134
- klass.arel_table[column_name].not_eq_any(value)
135
- when :not_in
136
- klass.arel_table[column_name].not_in(value)
137
- when :not_in_all
138
- klass.arel_table[column_name].not_in_all(value)
139
- when :not_in_any
140
- klass.arel_table[column_name].not_in_any(value)
141
- else
142
- raise ArgumentError, "unsupported predicate `#{predicate}`" unless predicate.respond_to?(:call)
143
-
144
- predicate.call(collection, value)
145
- end
146
- end.inject(:or)
102
+ format = filters_configuration[column.to_sym].fetch(:format, :string)
103
+ column_name = filters_configuration[column.to_sym].fetch(:as, column)
104
+ klass = filters_configuration[column.to_sym].fetch(:class_name, collection.klass)
105
+ predicate = filters_configuration[column.to_sym].fetch(:with, :ilike)
106
+
107
+ if values.is_a?(String)
108
+ values.split(',').map do |value|
109
+ predicates(predicate, collection, klass, column_name, value)
110
+ end.inject(:or)
111
+ else
112
+ values.map! { |el| el.split(',') }
113
+ predicates(predicate, collection, klass, column_name, values)
114
+ end
147
115
  end
148
116
 
149
117
  collection.where(filtering.flatten.compact.inject(:and))
150
118
  end
119
+
120
+ def predicates(predicate, collection, klass, column_name, value)
121
+ case predicate
122
+ when :between
123
+ klass.arel_table[column_name].between(value.first..value.last)
124
+ when :does_not_match
125
+ klass.arel_table[column_name].does_not_match("%#{value}%")
126
+ when :does_not_match_all
127
+ klass.arel_table[column_name].does_not_match_all(value)
128
+ when :does_not_match_any
129
+ klass.arel_table[column_name].does_not_match_any(value)
130
+ when :eq
131
+ klass.arel_table[column_name].eq(value)
132
+ when :eq_all
133
+ klass.arel_table[column_name].eq_all(value)
134
+ when :eq_any
135
+ klass.arel_table[column_name].eq_any(value)
136
+ when :gt
137
+ klass.arel_table[column_name].gt(value)
138
+ when :gt_all
139
+ klass.arel_table[column_name].gt_all(value)
140
+ when :gt_any
141
+ klass.arel_table[column_name].gt_any(value)
142
+ when :gteq
143
+ klass.arel_table[column_name].gteq(value)
144
+ when :gteq_all
145
+ klass.arel_table[column_name].gteq_all(value)
146
+ when :gteq_any
147
+ klass.arel_table[column_name].gteq_any(value)
148
+ when :in
149
+ klass.arel_table[column_name].in(value)
150
+ when :in_all
151
+ klass.arel_table[column_name].in_all(value)
152
+ when :in_any
153
+ klass.arel_table[column_name].in_any(value)
154
+ when :lt
155
+ klass.arel_table[column_name].lt(value)
156
+ when :lt_all
157
+ klass.arel_table[column_name].lt_all(value)
158
+ when :lt_any
159
+ klass.arel_table[column_name].lt_any(value)
160
+ when :lteq
161
+ klass.arel_table[column_name].lteq(value)
162
+ when :lteq_all
163
+ klass.arel_table[column_name].lteq_all(value)
164
+ when :lteq_any
165
+ klass.arel_table[column_name].lteq_any(value)
166
+ when :ilike
167
+ klass.arel_table[column_name].matches("%#{value}%")
168
+ when :matches
169
+ klass.arel_table[column_name].matches(value)
170
+ when :matches_all
171
+ klass.arel_table[column_name].matches_all(value)
172
+ when :matches_any
173
+ klass.arel_table[column_name].matches_any(value)
174
+ when :not_between
175
+ klass.arel_table[column_name].not_between(value.first..value.last)
176
+ when :not_eq
177
+ klass.arel_table[column_name].not_eq(value)
178
+ when :not_eq_all
179
+ klass.arel_table[column_name].not_eq_all(value)
180
+ when :not_eq_any
181
+ klass.arel_table[column_name].not_eq_any(value)
182
+ when :not_in
183
+ klass.arel_table[column_name].not_in(value)
184
+ when :not_in_all
185
+ klass.arel_table[column_name].not_in_all(value)
186
+ when :not_in_any
187
+ klass.arel_table[column_name].not_in_any(value)
188
+ else
189
+ raise ArgumentError, "unsupported predicate `#{predicate}`" unless predicate.respond_to?(:call)
190
+
191
+ predicate.call(collection, value)
192
+ end
193
+ end
194
+
195
+ def foa_default_permitted_types
196
+ [ActionController::Parameters, Hash, Array]
197
+ end
151
198
  end
152
199
  end
@@ -49,7 +49,7 @@ module FetcheableOnApi
49
49
 
50
50
  def apply_sort(collection)
51
51
  return collection if params[:sort].blank?
52
- foa_valid_parameters!(:sort, permitted_types: [String])
52
+ foa_valid_parameters!(:sort, foa_permitted_types: [String])
53
53
 
54
54
  ordering = {}
55
55
  sorted_params = params[:sort].split(',')
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module FetcheableOnApi
4
- VERSION = '0.1.8'.freeze
4
+ VERSION = '0.1.9.0'.freeze
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fetcheable_on_api
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.1.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Fabien