activerecord-filter 8.1.0 → 9.0.0
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:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a42ca6ff3bdbd4318c13518684d31f6bdf3b7a98aa02e877e2d8147c83d2efef
|
|
4
|
+
data.tar.gz: 5b595328b4129bced6fa43a87c6f93398d6cb35942bb0aa693874b49a062de91
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 3e1500189bf99dae963016fff43cb78cda9aa42fe771907fd7842562d67ea704b4609822e85a3f559f96776c3f084a54e790cfdd4bc1026731da44989b7a5109
|
|
7
|
+
data.tar.gz: 6f9aeae084bf0b7efde2b69dc20a1ac15b8312bbedd800820f5b84fa7e733147357a09fdcf8272e2c1a263077f2fff93a40c5aebd60b9ef3ce2adbd90537385a
|
data/README.md
CHANGED
|
@@ -1,9 +1,13 @@
|
|
|
1
1
|
# ActiveRecord::Filter
|
|
2
2
|
|
|
3
|
-
`ActiveRecord::Filter` provides
|
|
3
|
+
`ActiveRecord::Filter` provides an easy way to accept user input and filter a query by that input.
|
|
4
4
|
|
|
5
|
-
|
|
6
|
-
|
|
5
|
+
## Requirements
|
|
6
|
+
|
|
7
|
+
- Ruby >= 3.3
|
|
8
|
+
- ActiveRecord >= 8.0
|
|
9
|
+
|
|
10
|
+
## Installation
|
|
7
11
|
|
|
8
12
|
- Add `gem 'activerecord-filter', require: 'active_record/filter'`
|
|
9
13
|
- Run `bundle install`
|
|
@@ -124,10 +128,39 @@ Property.filter("metadata.key": { eq: 'value' }).to_sql
|
|
|
124
128
|
# => "...WHERE "properties"."metadata" #> '{key}' = 'value'..."
|
|
125
129
|
```
|
|
126
130
|
|
|
127
|
-
It can also
|
|
131
|
+
It can also filter across associations. Any association (`belongs_to`,
|
|
132
|
+
`has_many`, `has_one`, `has_and_belongs_to_many`, `has_many :through`) can be
|
|
133
|
+
nested, and the join is added for you:
|
|
128
134
|
|
|
129
135
|
```ruby
|
|
130
136
|
Photo.filter(property: {name: 'Empire State'}).to_sql
|
|
131
137
|
# => "... LEFT OUTER JOIN properties ON properties.id = photos.property_id ...
|
|
132
138
|
# => "... WHERE properties.name = 'Empire State'"
|
|
133
139
|
```
|
|
140
|
+
|
|
141
|
+
Pass `true`/`false` against an association to filter on its presence:
|
|
142
|
+
|
|
143
|
+
```ruby
|
|
144
|
+
Account.filter(photos: true).to_sql # accounts that have photos
|
|
145
|
+
Account.filter(photos: false).to_sql # accounts with no photos
|
|
146
|
+
```
|
|
147
|
+
|
|
148
|
+
For a polymorphic association, scope it to a type with `as:`:
|
|
149
|
+
|
|
150
|
+
```ruby
|
|
151
|
+
View.filter(subject: {as: 'Property', name: 'Name'}).to_sql
|
|
152
|
+
```
|
|
153
|
+
|
|
154
|
+
Combining conditions with AND / OR
|
|
155
|
+
----------------------------------
|
|
156
|
+
|
|
157
|
+
Pass an array to group conditions. Use the strings `'AND'` / `'OR'` as
|
|
158
|
+
separators, and nest arrays for precedence:
|
|
159
|
+
|
|
160
|
+
```ruby
|
|
161
|
+
Property.filter([{id: 10}, 'OR', {name: 'name'}]).to_sql
|
|
162
|
+
# => "... WHERE ((properties.id = 10) OR (properties.name = 'name'))"
|
|
163
|
+
|
|
164
|
+
Property.filter([{id: 10}, 'AND', [{id: 10}, 'OR', {name: 'name'}]]).to_sql
|
|
165
|
+
# => "... WHERE properties.id = 10 AND ((properties.id = 10) OR (properties.name = 'name'))"
|
|
166
|
+
```
|
|
@@ -28,6 +28,15 @@ module ActiveRecord::Filter::PredicateBuilderExtension
|
|
|
28
28
|
relations << j
|
|
29
29
|
end
|
|
30
30
|
end
|
|
31
|
+
elsif js.is_a?(Proc)
|
|
32
|
+
resolved = js.call(value)
|
|
33
|
+
Array(resolved).compact.each do |j|
|
|
34
|
+
if j.is_a?(String)
|
|
35
|
+
custom << j
|
|
36
|
+
else
|
|
37
|
+
relations << j
|
|
38
|
+
end
|
|
39
|
+
end
|
|
31
40
|
elsif js
|
|
32
41
|
if js.is_a?(String)
|
|
33
42
|
custom << js
|
|
@@ -55,17 +64,17 @@ module ActiveRecord::Filter::PredicateBuilderExtension
|
|
|
55
64
|
]
|
|
56
65
|
else
|
|
57
66
|
{
|
|
58
|
-
|
|
67
|
+
reflection.name => build_filter_joins(reflection.klass, value, [], custom)
|
|
59
68
|
}
|
|
60
69
|
end
|
|
61
70
|
elsif value.is_a?(Array)
|
|
62
71
|
value.each do |v|
|
|
63
72
|
relations << {
|
|
64
|
-
|
|
73
|
+
reflection.name => build_filter_joins(reflection.klass, v, [], custom)
|
|
65
74
|
}
|
|
66
75
|
end
|
|
67
76
|
elsif value != true && value != false && value != 'true' && value != 'false' && !value.nil?
|
|
68
|
-
relations <<
|
|
77
|
+
relations << reflection.name
|
|
69
78
|
end
|
|
70
79
|
elsif !klass.columns_hash.has_key?(key.to_s) && key.to_s.end_with?('_ids') && reflection = klass._reflections[key.to_s.gsub(/_ids$/, 's').to_sym]
|
|
71
80
|
relations << reflection.name
|
|
@@ -10,11 +10,7 @@ private
|
|
|
10
10
|
left_joins = select_named_joins(left_outer_joins_values, stashed_left_joins) do |left_join|
|
|
11
11
|
if left_join.is_a?(ActiveRecord::QueryMethods::CTEJoin)
|
|
12
12
|
buckets[:join_node] << build_with_join_node(left_join.name, Arel::Nodes::OuterJoin)
|
|
13
|
-
|
|
14
|
-
# Changed a line https://github.com/rails/rails/blob/ae2983a75ca658d84afa414dea8eaf1cca87aa23/activerecord/lib/active_record/relation/query_methods.rb#L1769
|
|
15
|
-
# that was probably a bug beforehand but allowed nodes to be joined
|
|
16
|
-
# which I think was and still is supported?
|
|
17
|
-
elsif left_join.is_a?(Arel::Nodes::OuterJoin)
|
|
13
|
+
elsif left_join.is_a?(Arel::Nodes::Join)
|
|
18
14
|
buckets[:join_node] << left_join
|
|
19
15
|
else
|
|
20
16
|
raise ArgumentError, "only Hash, Symbol and Array are allowed"
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: activerecord-filter
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version:
|
|
4
|
+
version: 9.0.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Jon Bracy
|
|
@@ -15,28 +15,28 @@ dependencies:
|
|
|
15
15
|
requirements:
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
|
-
version:
|
|
18
|
+
version: 8.0.0
|
|
19
19
|
type: :runtime
|
|
20
20
|
prerelease: false
|
|
21
21
|
version_requirements: !ruby/object:Gem::Requirement
|
|
22
22
|
requirements:
|
|
23
23
|
- - ">="
|
|
24
24
|
- !ruby/object:Gem::Version
|
|
25
|
-
version:
|
|
25
|
+
version: 8.0.0
|
|
26
26
|
- !ruby/object:Gem::Dependency
|
|
27
27
|
name: arel-extensions
|
|
28
28
|
requirement: !ruby/object:Gem::Requirement
|
|
29
29
|
requirements:
|
|
30
30
|
- - ">="
|
|
31
31
|
- !ruby/object:Gem::Version
|
|
32
|
-
version:
|
|
32
|
+
version: 8.0.0
|
|
33
33
|
type: :runtime
|
|
34
34
|
prerelease: false
|
|
35
35
|
version_requirements: !ruby/object:Gem::Requirement
|
|
36
36
|
requirements:
|
|
37
37
|
- - ">="
|
|
38
38
|
- !ruby/object:Gem::Version
|
|
39
|
-
version:
|
|
39
|
+
version: 8.0.0
|
|
40
40
|
- !ruby/object:Gem::Dependency
|
|
41
41
|
name: pg
|
|
42
42
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -57,14 +57,14 @@ dependencies:
|
|
|
57
57
|
requirements:
|
|
58
58
|
- - ">="
|
|
59
59
|
- !ruby/object:Gem::Version
|
|
60
|
-
version:
|
|
60
|
+
version: 8.0.0
|
|
61
61
|
type: :development
|
|
62
62
|
prerelease: false
|
|
63
63
|
version_requirements: !ruby/object:Gem::Requirement
|
|
64
64
|
requirements:
|
|
65
65
|
- - ">="
|
|
66
66
|
- !ruby/object:Gem::Version
|
|
67
|
-
version:
|
|
67
|
+
version: 8.0.0
|
|
68
68
|
- !ruby/object:Gem::Dependency
|
|
69
69
|
name: bundler
|
|
70
70
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -155,14 +155,14 @@ dependencies:
|
|
|
155
155
|
requirements:
|
|
156
156
|
- - ">="
|
|
157
157
|
- !ruby/object:Gem::Version
|
|
158
|
-
version:
|
|
158
|
+
version: 8.0.0
|
|
159
159
|
type: :development
|
|
160
160
|
prerelease: false
|
|
161
161
|
version_requirements: !ruby/object:Gem::Requirement
|
|
162
162
|
requirements:
|
|
163
163
|
- - ">="
|
|
164
164
|
- !ruby/object:Gem::Version
|
|
165
|
-
version:
|
|
165
|
+
version: 8.0.0
|
|
166
166
|
- !ruby/object:Gem::Dependency
|
|
167
167
|
name: faker
|
|
168
168
|
requirement: !ruby/object:Gem::Requirement
|
|
@@ -227,7 +227,11 @@ files:
|
|
|
227
227
|
homepage: https://github.com/malomalo/activerecord-filter
|
|
228
228
|
licenses:
|
|
229
229
|
- MIT
|
|
230
|
-
metadata:
|
|
230
|
+
metadata:
|
|
231
|
+
homepage_uri: https://github.com/malomalo/activerecord-filter
|
|
232
|
+
source_code_uri: https://github.com/malomalo/activerecord-filter
|
|
233
|
+
changelog_uri: https://github.com/malomalo/activerecord-filter/blob/master/CHANGELOG.md
|
|
234
|
+
rubygems_mfa_required: 'true'
|
|
231
235
|
rdoc_options:
|
|
232
236
|
- "--main"
|
|
233
237
|
- README.md
|
|
@@ -237,14 +241,14 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
237
241
|
requirements:
|
|
238
242
|
- - ">="
|
|
239
243
|
- !ruby/object:Gem::Version
|
|
240
|
-
version: '
|
|
244
|
+
version: '3.3'
|
|
241
245
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
242
246
|
requirements:
|
|
243
247
|
- - ">="
|
|
244
248
|
- !ruby/object:Gem::Version
|
|
245
249
|
version: '0'
|
|
246
250
|
requirements: []
|
|
247
|
-
rubygems_version: 4.0.
|
|
251
|
+
rubygems_version: 4.0.11
|
|
248
252
|
specification_version: 4
|
|
249
253
|
summary: A safe way to accept user parameters and query against your ActiveRecord
|
|
250
254
|
Models
|