filterameter 0.8.0 → 0.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: abb0f5a22c735d69e3e44625f0fd4a0cd451f35499677f4f0d51d5a7aff8ea3a
4
- data.tar.gz: 359eefe9c128bd46651e475e6fc1620d145c68478429e8d49ab3d601fa95f2bb
3
+ metadata.gz: f62d4fc49f10adca7c1fec20fb17e430740035d69b154f973d8f300405f64f9c
4
+ data.tar.gz: 7369430dbb71a29faf17853a35eaa69f3a35fae52b82d63e85f5c14bd550aa51
5
5
  SHA512:
6
- metadata.gz: 3b11d1b2281e340ecc32ea75e37c857f888cce526fda98b4c2d96a5bbfd8a7ba0f2cbd901480c10eb8b6fc4ca4d48080e43f79f66e7bdb879819d407c896b237
7
- data.tar.gz: 285384ed0b9506937406aa0d83744b99239666a4d84b7b0d6893bc1351ac26e227eb37cd1d9baa2c3e7be720fea36a9acc1a7b7d3979006fd8be0eaaa72dd21b
6
+ metadata.gz: e5ee234ae90460282e2618135b96f91882af00361b1c16eda99beb42640cf9c4186d1bb61fa1989235015a1f4dff07f7611ca5dea55ec6277c27dc9239329bc8
7
+ data.tar.gz: 6f802f89428a06de2f077d5c4b38464bc8ad1ee4327d334856f3421481cba3af84ef789cb2e85863fe933f8b45f90fe2b7640910d16618bb97e35c65d9bda691
data/README.md CHANGED
@@ -200,6 +200,7 @@ Rails conventions are used to determine the controller's model. For example, the
200
200
  filter_model 'Picture'
201
201
  ```
202
202
 
203
+ _Important:_ If the `filter_model` declaration is used, it must be before any filter or sort declarations.
203
204
 
204
205
  ### Building the Query
205
206
 
@@ -299,25 +300,48 @@ For example, the following sorts by size descending:
299
300
 
300
301
  `/widgets?filter[sort]=-size`
301
302
 
303
+ ### Configuration
302
304
 
303
- #### Override the Filter Key
305
+ There are three configuration options:
304
306
 
305
- To change the source of the query parameters, override the `filter_parameters` method. Here is another way to provide a default filter:
307
+ - action_on_undeclared_parameters
308
+ - action_on_validation_failure
309
+ - filter_key
306
310
 
307
- ```ruby
308
- def filter_parameters
309
- super.with_defaults(active: true)
310
- end
311
- ```
311
+ The configuration options can be set in an initializer, an environment file, or in `application.rb`.
312
+
313
+ The options can be set directly...
312
314
 
313
- This also provides an easy way to nest the criteria under a key other than `filter`:
315
+ `Filterameter.configuration.action_on_undeclared_parameters = :log`
316
+
317
+ ...or the configuration can be yielded:
314
318
 
315
319
  ```ruby
316
- def filter_parameters
317
- params.to_unsafe_h.fetch(:criteria, {})
320
+ Filterameter.configure do |config|
321
+ config.action_on_undeclared_parameters = :log
322
+ config.action_on_validation_failuer = :log
323
+ config.filter_key = :f
318
324
  end
319
325
  ```
320
326
 
327
+ #### Action On Undeclared Parameters
328
+
329
+ Occurs when the filter parameter contains any keys that are not defined. Valid actions are `:log`, `:raise`, and `false` (do not take action). By default, development will log, test will raise, and production will do nothing.
330
+
331
+ #### Action on Validation Failure
332
+
333
+ Occurs when a filter parameter fails a validation. Valid actions are `:log`, `:raise`, and `false` (do not take action). By default, development will log, test will raise, and production will do nothing.
334
+
335
+ #### Filter Key
336
+
337
+ By default, the filter parameters are nested under the key `:filter`. Use this setting to override the key.
338
+
339
+ If the filter parameters are NOT nested, set this to false. Doing so will restrict the filter parameters to only
340
+ those that have been declared, meaning undeclared parameters are ignored (and the action_on_undeclared_parameters
341
+ configuration option does not come into play).
342
+
343
+
344
+
321
345
 
322
346
  ## Installation
323
347
  Add this line to your application's Gemfile:
@@ -6,16 +6,27 @@ module Filterameter
6
6
  # Class Configuration stores the following settings:
7
7
  # - action_on_undeclared_parameters
8
8
  # - action_on_validation_failure
9
+ # - filter_key
9
10
  #
10
11
  # == Action on Undeclared Parameters
12
+ #
11
13
  # Occurs when the filter parameter contains any keys that are not defined. Valid actions are :log, :raise, and
12
14
  # false (do not take action). By default, development will log, test will raise, and production will do nothing.
13
15
  #
14
16
  # == Action on Validation Failure
17
+ #
15
18
  # Occurs when a filter parameter fails a validation. Valid actions are :log, :raise, and false (do not take action).
16
19
  # By default, development will log, test will raise, and production will do nothing.
20
+ #
21
+ # == Filter Key
22
+ #
23
+ # By default, the filter parameters are nested under the key :filter. Use this setting to override the key.
24
+ #
25
+ # If the filter parameters are NOT nested, set this to false. Doing so will restrict the filter parameters to only
26
+ # those that have been declared, meaning undeclared parameters are ignored (and the action_on_undeclared_parameters
27
+ # configuration option does not come into play).
17
28
  class Configuration
18
- attr_accessor :action_on_undeclared_parameters, :action_on_validation_failure
29
+ attr_accessor :action_on_undeclared_parameters, :action_on_validation_failure, :filter_key
19
30
 
20
31
  def initialize
21
32
  @action_on_undeclared_parameters =
@@ -27,6 +38,8 @@ module Filterameter
27
38
  else
28
39
  false
29
40
  end
41
+
42
+ @filter_key = :filter
30
43
  end
31
44
  end
32
45
  end
@@ -29,7 +29,13 @@ module Filterameter
29
29
  end
30
30
 
31
31
  def filter_parameters
32
- params.to_unsafe_h.fetch(:filter, {})
32
+ filter_key = Filterameter.configuration.filter_key
33
+
34
+ if filter_key
35
+ params.to_unsafe_h.fetch(filter_key, {})
36
+ else
37
+ params.to_unsafe_h.slice(*self.class.filter_coordinator.filter_parameter_names, :sort)
38
+ end
33
39
  end
34
40
 
35
41
  private
@@ -19,7 +19,7 @@ module Filterameter
19
19
  class FilterCoordinator
20
20
  attr_writer :query_variable_name
21
21
 
22
- delegate :add_filter, :add_sort, to: :registry
22
+ delegate :add_filter, :add_sort, :filter_parameter_names, to: :registry
23
23
  delegate :build_query, to: :query_builder
24
24
 
25
25
  def initialize(controller_name, controller_path)
@@ -14,8 +14,8 @@ module Filterameter
14
14
 
15
15
  def undeclared_parameters(event)
16
16
  debug do
17
- keys = event.payload[:keys]
18
- " Undeclared filter parameter#{'s' if keys.size > 1}: #{keys.map { |e| ":#{e}" }.join(', ')}"
17
+ key = event.payload[:key]
18
+ " Undeclared filter parameter: #{key}"
19
19
  end
20
20
  end
21
21
  end
@@ -24,6 +24,10 @@ module Filterameter
24
24
  @declarations.values
25
25
  end
26
26
 
27
+ def filter_parameter_names
28
+ @declarations.keys
29
+ end
30
+
27
31
  private
28
32
 
29
33
  # if range is enabled, then in addition to the attribute filter this also adds min and/or max filters
@@ -6,7 +6,7 @@ module Filterameter
6
6
  #
7
7
  # Class Registry records declarations and allows resulting filters and sorts to be fetched from sub-registries.
8
8
  class Registry
9
- delegate :filter_declarations, :ranges, to: :@filter_registry
9
+ delegate :filter_declarations, :filter_parameter_names, :ranges, to: :@filter_registry
10
10
 
11
11
  def initialize(model_class)
12
12
  @filter_registry = Filterameter::Registries::FilterRegistry.new(Filterameter::FilterFactory.new(model_class))
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Filterameter
4
- VERSION = '0.8.0'
4
+ VERSION = '0.9.0'
5
5
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filterameter
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.0
4
+ version: 0.9.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Todd Kummer
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2024-07-16 00:00:00.000000000 Z
11
+ date: 2024-07-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rails