filterer 1.0.2 → 2.0.0.beta.1

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: f8c5e4ebbe2716f8bbb6b199fa58db26d536cdaa
4
- data.tar.gz: 3dbe734ec47b6942f1632995cf6481024d721b48
3
+ metadata.gz: e4c80e1f46cf7a449a248bd2098a0cf25a2533f3
4
+ data.tar.gz: ed7d30e5cb98b8b906d8c92d5c0c0a351bcc4566
5
5
  SHA512:
6
- metadata.gz: 36d5f7860944f04ce7c22c3118ae45367625472a7c414191a7f0771e32e593b63b20a1aa6f30136026a08860436f42c21c4443a0e9284873135068037feb0aa5
7
- data.tar.gz: 54aaf732a80fe0fa6ac8142e839dd8380011bd6819c9f7bea7acd56eeab944832d63917a1538daa9f0d2ee4aad00671d38b4c134ca08070e38a64af1a821f295
6
+ metadata.gz: f264ae332fd44cf42559a460a4455e58a737b106a5e3979473107fec4cafa6654b9606643990d5f9cb66b69554840030080fcc2c171fe9d7bd98cff5ae7a5ada
7
+ data.tar.gz: dfe8ec0acd0c53303729459f521a4c7314782d00de12c859e25b569e2b70dcb887b00101f9a4234d1e473cd5c9c4e3d7b71d8b2c124d2113651c9a124baf561d
@@ -2,18 +2,23 @@ module Filterer
2
2
  module ActiveRecord
3
3
  extend ActiveSupport::Concern
4
4
 
5
- included do
6
- def self.filter(params = {}, opts = {})
7
- filterer_class(opts[:filterer_class]).
8
- filter(params, { starting_query: all }.merge(opts))
5
+ class_methods do
6
+ def filter(params = {}, opts = {})
7
+ delegate_to_filterer(:filter, params, opts)
8
+ end
9
+
10
+ def chain(params = {}, opts = {})
11
+ delegate_to_filterer(:chain, params, opts)
9
12
  end
10
13
 
11
- def self.chain(params = {}, opts = {})
14
+ private
15
+
16
+ def delegate_to_filterer(method, params, opts)
12
17
  filterer_class(opts[:filterer_class]).
13
- chain(params, { starting_query: all }.merge(opts))
18
+ send(method, params, { starting_query: all }.merge(opts))
14
19
  end
15
20
 
16
- def self.filterer_class(override)
21
+ def filterer_class(override)
17
22
  if override
18
23
  override.constantize
19
24
  else
@@ -72,11 +72,12 @@ module Filterer
72
72
 
73
73
  def initialize(params = {}, opts = {})
74
74
  self.opts = opts
75
- self.params = defaults.merge(params).with_indifferent_access
75
+ self.params = defaults.merge(parse_strong_parameters(params)).
76
+ with_indifferent_access
76
77
  self.results = opts[:starting_query] || starting_query
77
78
  self.results = apply_default_filters || results
78
79
  add_params_to_query
79
- order_results unless opts[:skip_ordering]
80
+ self.results = ordered_results unless opts[:skip_ordering]
80
81
  paginate_results unless opts[:skip_pagination]
81
82
  extend_active_record_relation
82
83
  end
@@ -137,7 +138,7 @@ module Filterer
137
138
  method_name = "param_#{k}"
138
139
 
139
140
  if respond_to?(method_name)
140
- self.results = send(method_name, v)
141
+ self.results = send(method_name, v) || results
141
142
  end
142
143
  end
143
144
  end
@@ -148,22 +149,14 @@ module Filterer
148
149
  end
149
150
  end
150
151
 
151
- def order_results
152
- self.results = if !sort_option
153
- order_by_sort_option(default_sort_option)
154
- elsif sort_option[:string_or_proc].is_a?(String)
155
- order_by_sort_option(sort_option)
156
- elsif sort_option[:string_or_proc].is_a?(Proc)
157
- sort_string = sort_proc_to_string(sort_option)
158
-
159
- if sort_string
160
- order_by_sort_option(sort_option.merge(
161
- string_or_proc: sort_string
162
- ))
163
- else
164
- order_by_sort_option(filterer_default_sort_option)
165
- end
166
- end
152
+ def ordered_results
153
+ if sort_option && sort_option[:string_or_proc].is_a?(String)
154
+ order_by_sort_option(sort_option)
155
+ elsif sort_option && sort_option[:string_or_proc].is_a?(Proc)
156
+ order_by_sort_proc
157
+ else
158
+ order_by_sort_option(default_sort_option)
159
+ end
167
160
  end
168
161
 
169
162
  def per_page
@@ -194,6 +187,16 @@ module Filterer
194
187
  end
195
188
  end
196
189
 
190
+ def order_by_sort_proc
191
+ if (sort_string = sort_proc_to_string(sort_option))
192
+ order_by_sort_option(sort_option.merge(
193
+ string_or_proc: sort_string
194
+ ))
195
+ else
196
+ order_by_sort_option(filterer_default_sort_option)
197
+ end
198
+ end
199
+
197
200
  def order_by_sort_option(opt)
198
201
  results.order %{
199
202
  #{opt[:string_or_proc]}
@@ -238,5 +241,9 @@ module Filterer
238
241
  end
239
242
  end
240
243
  end
244
+
245
+ def parse_strong_parameters(params)
246
+ params.try(:to_unsafe_h) || params
247
+ end
241
248
  end
242
249
  end
@@ -1,3 +1,3 @@
1
1
  module Filterer
2
- VERSION = '1.0.2'
2
+ VERSION = '2.0.0.beta.1'
3
3
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filterer
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.2
4
+ version: 2.0.0.beta.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Adam Becker
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2016-05-12 00:00:00.000000000 Z
11
+ date: 2016-09-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails
@@ -94,20 +94,6 @@ dependencies:
94
94
  - - ">="
95
95
  - !ruby/object:Gem::Version
96
96
  version: '0'
97
- - !ruby/object:Gem::Dependency
98
- name: launchy
99
- requirement: !ruby/object:Gem::Requirement
100
- requirements:
101
- - - ">="
102
- - !ruby/object:Gem::Version
103
- version: '0'
104
- type: :development
105
- prerelease: false
106
- version_requirements: !ruby/object:Gem::Requirement
107
- requirements:
108
- - - ">="
109
- - !ruby/object:Gem::Version
110
- version: '0'
111
97
  - !ruby/object:Gem::Dependency
112
98
  name: pry
113
99
  requirement: !ruby/object:Gem::Requirement
@@ -150,20 +136,6 @@ dependencies:
150
136
  - - ">="
151
137
  - !ruby/object:Gem::Version
152
138
  version: '0'
153
- - !ruby/object:Gem::Dependency
154
- name: terminal-notifier-guard
155
- requirement: !ruby/object:Gem::Requirement
156
- requirements:
157
- - - ">="
158
- - !ruby/object:Gem::Version
159
- version: '0'
160
- type: :development
161
- prerelease: false
162
- version_requirements: !ruby/object:Gem::Requirement
163
- requirements:
164
- - - ">="
165
- - !ruby/object:Gem::Version
166
- version: '0'
167
139
  description: Filterer lets your users easily filter results from your ActiveRecord
168
140
  models.
169
141
  email:
@@ -199,9 +171,9 @@ required_ruby_version: !ruby/object:Gem::Requirement
199
171
  version: '0'
200
172
  required_rubygems_version: !ruby/object:Gem::Requirement
201
173
  requirements:
202
- - - ">="
174
+ - - ">"
203
175
  - !ruby/object:Gem::Version
204
- version: '0'
176
+ version: 1.3.1
205
177
  requirements: []
206
178
  rubyforge_project:
207
179
  rubygems_version: 2.5.1
@@ -209,4 +181,3 @@ signing_key:
209
181
  specification_version: 4
210
182
  summary: Easily filter results from your ActiveRecord models.
211
183
  test_files: []
212
- has_rdoc: