filterameter 1.0.0 → 1.0.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: b14f3434137044896e6273279573a9ec00bbb9ef6b1fbe03c99a01851df5a4d3
4
- data.tar.gz: d2afe0937ee82dea9c8ec4933c39bb13a083c6183772ecb847ef7a28d582afbd
3
+ metadata.gz: a250b39d9c80245166bcfe14a7133cd5ec83f9746989d7e461fa0c121358f57a
4
+ data.tar.gz: 5d7c64cb79fd73ee94e4e94316b0b5964fac440b14fb0bc58c0c1d805d0f9d60
5
5
  SHA512:
6
- metadata.gz: 2ebdc7ca579a967fc95e3843df78e6137291a1136e60fe9636306d5899327f4a456e581e70251c4ad1203e253d82f24090f85cefe699476c8d928c2f22f2796e
7
- data.tar.gz: d5bb0bf69b24912dcd22b21f41abb5655bb225cf77316c20e7c753ca2dd060a47815cd6af66b4cc229ca33bfcf94010bbcfc84f5abcdfbf1fd6faee1dda6d8a1
6
+ metadata.gz: d170eb98f80c24173fa92d0251aa68af7d5f3d002d4c3ff473e086aba8d16f07fd05422af6dced018331e883431c93292b4d0b7e5bf3476587b40df8dbeab8d3
7
+ data.tar.gz: e2342236255b79da8ee96aa7eec0e33c6e9f0131473eaadffd31f833c7212d01d2123eeb6a70cdff8d619ff8678f4d6aafc8d9b52151de9dc6583983f3accd92
@@ -6,17 +6,129 @@ module Filterameter
6
6
  # Mixin DeclarativeFilters should be included in controllers to enable the filter DSL.
7
7
  module DeclarativeFilters
8
8
  extend ActiveSupport::Concern
9
- include Filterameter::Filterable
10
- include Filterameter::Sortable
11
9
 
12
10
  class_methods do
13
11
  delegate :declarations_validator, to: :filter_coordinator
14
12
 
13
+ # Declares a filter that can be read from the parameters and applied to the
14
+ # ActiveRecord query. The `name` identifies the name of the parameter and is the
15
+ # default value to determine the criteria to be applied. The name can be either
16
+ # an attribute or a scope.
17
+ #
18
+ # ## Options
19
+ #
20
+ # The declaration can also include an `options` hash to specialize the behavior of the filter.
21
+ #
22
+ # * **name**: Specify the attribute or scope name if the parameter name is not the same. The default value is the
23
+ # parameter name, so if the two match this can be left out.
24
+ #
25
+ # * **association**: Specify the name of the association if the attribute or scope is nested. Use an array if the
26
+ # asociation is more than one level.
27
+ #
28
+ # * **validates**: Specify a validation if the parameter value should be validated. This uses ActiveModel
29
+ # validations; please review those for types of validations and usage.
30
+ #
31
+ # * **partial**: Specify the partial option if the filter should do a partial search (SQL's `LIKE`). The partial
32
+ # option accepts a hash to specify the search behavior.
33
+ #
34
+ # Here are the available options:
35
+ # * match: `:anywhere` (default), `:from_start`, `:dynamic`
36
+ # * case_sensitive: `true`, `false` (default)
37
+ #
38
+ # There are two shortcuts: the partial option can be declared with `true`,
39
+ # which just uses the defaults; or the partial option can be declared with
40
+ # the match option directly, such as `partial: :from_start`.
41
+ #
42
+ # * **range**: Specify a range option if the filter also allows ranges to be searched. The range option accepts
43
+ # the following options:
44
+ # * true: enables two additional parameters with attribute name plus
45
+ # suffixes `_min` and `_max`
46
+ # * min_only: enables additional parameter with attribute name plus
47
+ # suffix `_min`
48
+ # * max_only: enables additional parameter with attribute name plus
49
+ # suffix `_max`
50
+ #
51
+ # * **sortable**: By default most filters are sortable. To prevent an attribute filter from being sortable, set
52
+ # the option to `false`.
53
+ #
54
+ # Options examples:
55
+ #
56
+ # filter :status, name: :current_status
57
+ # filter :manager_id, association: :department
58
+ # filter :business_unit_name, name: :name, association: [:department, :business_unit]
59
+ # filter :size, validates: { inclusion: { in: %w[Small Medium Large], allow_multiple_values: true } }
60
+ # filter :description, partial: true
61
+ # filter :department_name, partial: :from_start
62
+ # filter :reason, partial: { match: :dynamic, case_sensitive: true }
63
+ # filter :price, range: true
64
+ def filter(name, options = {})
65
+ filter_coordinator.add_filter(name, options)
66
+ end
67
+
68
+ # Declares a list of filters without options. Filters that require options must be declared with `filter`.
69
+ def filters(*names)
70
+ names.each { |name| filter(name) }
71
+ end
72
+
73
+ # Declares a sort that can be read from the parameters and applied to the
74
+ # ActiveRecord query. The `parameter_name` identifies the name of the parameter
75
+ # and is the default value for the attribute name when none is specified in the
76
+ # options.
77
+ #
78
+ # ## Options
79
+ #
80
+ # The declaration can also include an `options` hash to specialize the behavior of the sort.
81
+ #
82
+ # * **name**: Specify the attribute or scope name if the parameter name is not the same. The default value is the
83
+ # parameter name, so if the two match this can be left out.
84
+ #
85
+ # * **association**: Specify the name of the association if the attribute or scope is nested.
86
+ #
87
+ # Options example:
88
+ #
89
+ # sort :project_created_at, name: :created_at, association: :project
90
+ def sort(parameter_name, options = {})
91
+ filter_coordinator.add_sort(parameter_name, options)
92
+ end
93
+
94
+ # Declares a list of sorts without options. Sorts that require options must be declared with `sort`.
95
+ def sorts(*parameter_names)
96
+ parameter_names.each { |parameter_name| filter(parameter_name) }
97
+ end
98
+
99
+ def default_sort(sort_and_direction_pairs)
100
+ filter_coordinator.default_sort = sort_and_direction_pairs
101
+ end
102
+
103
+ # Rails conventions are used to determine the controller's model. For example, the PhotosController builds a query
104
+ # against the Photo model. If a controller is namespaced, the model will first be looked up without the namespace,
105
+ # then with the namespace.
106
+ #
107
+ # **If the conventions do not provide the correct model**, the model can be named explicitly with the following:
108
+ #
109
+ # ```ruby
110
+ # filter_model 'Picture'
111
+ # ```
112
+ #
113
+ # An optional second parameter can be used to specify the variable name. Both the model and the variable name can
114
+ # be specified with this short-cut. For example, to use the Picture model and store the results as `@data`, use
115
+ # the following:
116
+ #
117
+ # filter_model 'Picture', :data
118
+ #
119
+ # **Important:** If the `filter_model` declaration is used, it must be before any filter or sort declarations.
15
120
  def filter_model(model_class, query_var_name = nil)
16
121
  filter_coordinator.model_class = model_class
17
122
  filter_query_var_name(query_var_name) if query_var_name.present?
18
123
  end
19
124
 
125
+ # When using the before action callback `build_filtered_query`, the query is assigned to an instance variable with
126
+ # the name of the model pluralized. For example, the Photo model will use the variable `@photos`.
127
+ #
128
+ # To use a different variable name with the callback, assign it with `filter_query_var_name`. For example, if the
129
+ # query is stored as `@data`, use the following:
130
+ #
131
+ # filter_query_var_name :data
20
132
  def filter_query_var_name(query_variable_name)
21
133
  filter_coordinator.query_variable_name = query_variable_name
22
134
  end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Filterameter
4
- VERSION = '1.0.0'
4
+ VERSION = '1.0.1'
5
5
  end
metadata CHANGED
@@ -1,14 +1,13 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: filterameter
3
3
  version: !ruby/object:Gem::Version
4
- version: 1.0.0
4
+ version: 1.0.1
5
5
  platform: ruby
6
6
  authors:
7
7
  - Todd Kummer
8
- autorequire:
9
8
  bindir: bin
10
9
  cert_chain: []
11
- date: 2024-08-19 00:00:00.000000000 Z
10
+ date: 2025-01-09 00:00:00.000000000 Z
12
11
  dependencies:
13
12
  - !ruby/object:Gem::Dependency
14
13
  name: rails
@@ -100,14 +99,14 @@ dependencies:
100
99
  requirements:
101
100
  - - "~>"
102
101
  - !ruby/object:Gem::Version
103
- version: '6.1'
102
+ version: '7.0'
104
103
  type: :development
105
104
  prerelease: false
106
105
  version_requirements: !ruby/object:Gem::Requirement
107
106
  requirements:
108
107
  - - "~>"
109
108
  - !ruby/object:Gem::Version
110
- version: '6.1'
109
+ version: '7.0'
111
110
  - !ruby/object:Gem::Dependency
112
111
  name: rubocop
113
112
  requirement: !ruby/object:Gem::Requirement
@@ -156,14 +155,14 @@ dependencies:
156
155
  requirements:
157
156
  - - "~>"
158
157
  - !ruby/object:Gem::Version
159
- version: 3.0.3
158
+ version: 3.2.0
160
159
  type: :development
161
160
  prerelease: false
162
161
  version_requirements: !ruby/object:Gem::Requirement
163
162
  requirements:
164
163
  - - "~>"
165
164
  - !ruby/object:Gem::Version
166
- version: 3.0.3
165
+ version: 3.2.0
167
166
  - !ruby/object:Gem::Dependency
168
167
  name: rubocop-rspec_rails
169
168
  requirement: !ruby/object:Gem::Requirement
@@ -223,7 +222,6 @@ files:
223
222
  - lib/filterameter/filter_coordinator.rb
224
223
  - lib/filterameter/filter_declaration.rb
225
224
  - lib/filterameter/filter_factory.rb
226
- - lib/filterameter/filterable.rb
227
225
  - lib/filterameter/filters/arel_filter.rb
228
226
  - lib/filterameter/filters/attribute_filter.rb
229
227
  - lib/filterameter/filters/attribute_validator.rb
@@ -247,7 +245,6 @@ files:
247
245
  - lib/filterameter/registries/sub_registry.rb
248
246
  - lib/filterameter/sort_declaration.rb
249
247
  - lib/filterameter/sort_factory.rb
250
- - lib/filterameter/sortable.rb
251
248
  - lib/filterameter/sorts/attribute_sort.rb
252
249
  - lib/filterameter/sorts/scope_sort.rb
253
250
  - lib/filterameter/validators/inclusion_validator.rb
@@ -256,7 +253,6 @@ homepage: https://github.com/RockSolt/filterameter
256
253
  licenses:
257
254
  - MIT
258
255
  metadata: {}
259
- post_install_message:
260
256
  rdoc_options: []
261
257
  require_paths:
262
258
  - lib
@@ -271,8 +267,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
271
267
  - !ruby/object:Gem::Version
272
268
  version: '0'
273
269
  requirements: []
274
- rubygems_version: 3.5.17
275
- signing_key:
270
+ rubygems_version: 3.6.2
276
271
  specification_version: 4
277
272
  summary: Declarative Filter Parameters
278
273
  test_files: []
@@ -1,69 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Filterameter
4
- # # Filterable
5
- #
6
- # Mixin Filterable provides class methods `filter` and `filters`.
7
- module Filterable
8
- extend ActiveSupport::Concern
9
-
10
- class_methods do
11
- # Declares a filter that can be read from the parameters and applied to the
12
- # ActiveRecord query. The `name` identifies the name of the parameter and is the
13
- # default value to determine the criteria to be applied. The name can be either
14
- # an attribute or a scope.
15
- #
16
- # ### Options
17
- #
18
- # :name
19
- # : Specify the attribute or scope name if the parameter name is not the same.
20
- # The default value is the parameter name, so if the two match this can be
21
- # left out.
22
- #
23
- #
24
- # :association
25
- # : Specify the name of the association if the attribute or scope is nested.
26
- #
27
- #
28
- # :validates
29
- # : Specify a validation if the parameter value should be validated. This uses
30
- # ActiveModel validations; please review those for types of validations and
31
- # usage.
32
- #
33
- #
34
- # :partial
35
- # : Specify the partial option if the filter should do a partial search (SQL's
36
- # `LIKE`). The partial option accepts a hash to specify the search behavior.
37
- #
38
- # Here are the available options:
39
- # * match: anywhere (default), from_start, dynamic
40
- # * case_sensitive: true, false (default)
41
- #
42
- # There are two shortcuts: : the partial option can be declared with `true`,
43
- # which just uses the defaults; or the partial option can be declared with
44
- # the match option directly, such as `partial: :from_start`.
45
- #
46
- #
47
- # :range
48
- # : Specify a range option if the filter also allows ranges to be searched.
49
- # The range option accepts the following options:
50
- # * true: enables two additional parameters with attribute name plus
51
- # suffixes `_min` and `_max`
52
- # * :min_only: enables additional parameter with attribute name plus
53
- # suffix `_min`
54
- # * :max_only: enables additional parameter with attribute name plus
55
- # suffix `_max`
56
- #
57
- def filter(name, options = {})
58
- filter_coordinator.add_filter(name, options)
59
- end
60
-
61
- # Declares a list of filters that can be read from the parameters and applied to
62
- # the query. The name can be either an attribute or a scope. Declare filters
63
- # individually with `filter` if more options are required.
64
- def filters(*names)
65
- names.each { |name| filter(name) }
66
- end
67
- end
68
- end
69
- end
@@ -1,45 +0,0 @@
1
- # frozen_string_literal: true
2
-
3
- module Filterameter
4
- # # Sortable
5
- #
6
- # Mixin Sortable provides class methods `sort` and `sorts`.
7
- module Sortable
8
- extend ActiveSupport::Concern
9
-
10
- class_methods do
11
- # Declares a sort that can be read from the parameters and applied to the
12
- # ActiveRecord query. The `parameter_name` identifies the name of the parameter
13
- # and is the default value for the attribute name when none is specified in the
14
- # options.
15
- #
16
- # The including class must implement `filter_coordinator`
17
- #
18
- # ### Options
19
- #
20
- # :name
21
- # : Specify the attribute or scope name if the parameter name is not the same.
22
- # The default value is the parameter name, so if the two match this can be
23
- # left out.
24
- #
25
- #
26
- # :association
27
- # : Specify the name of the association if the attribute or scope is nested.
28
- #
29
- def sort(parameter_name, options = {})
30
- filter_coordinator.add_sort(parameter_name, options)
31
- end
32
-
33
- # Declares a list of filters that can be read from the parameters and applied to
34
- # the query. The name can be either an attribute or a scope. Declare filters
35
- # individually with `filter` if more options are required.
36
- def sorts(*parameter_names)
37
- parameter_names.each { |parameter_name| filter(parameter_name) }
38
- end
39
-
40
- def default_sort(sort_and_direction_pairs)
41
- filter_coordinator.default_sort = sort_and_direction_pairs
42
- end
43
- end
44
- end
45
- end