kiroshi 0.1.1 → 0.3.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 +4 -4
- data/.codacy.yml +13 -0
- data/.markdownlint.json +6 -0
- data/.rubocop_todo.yml +4 -8
- data/README.md +62 -68
- data/lib/kiroshi/filter.rb +46 -24
- data/lib/kiroshi/filter_query/exact.rb +3 -3
- data/lib/kiroshi/filter_query/like.rb +30 -6
- data/lib/kiroshi/filter_query.rb +10 -14
- data/lib/kiroshi/filter_runner.rb +47 -35
- data/lib/kiroshi/filters/class_methods.rb +172 -0
- data/lib/kiroshi/filters.rb +89 -78
- data/lib/kiroshi/version.rb +1 -1
- data/lib/kiroshi.rb +3 -23
- data/spec/lib/kiroshi/filter_query/exact_spec.rb +41 -7
- data/spec/lib/kiroshi/filter_query/like_spec.rb +53 -12
- data/spec/lib/kiroshi/filter_runner_spec.rb +51 -26
- data/spec/lib/kiroshi/filter_spec.rb +9 -12
- data/spec/lib/kiroshi/filters/class_methods_spec.rb +103 -0
- data/spec/lib/kiroshi/filters_spec.rb +315 -9
- data/spec/spec_helper.rb +1 -0
- data/spec/support/db/schema.rb +1 -0
- metadata +6 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5748e560809cbfa28ea2b9d6ad06578ad1f4d0c67038bed6aca1a1978f27aa7c
|
4
|
+
data.tar.gz: b15225c3e02cbb634ec4fec69c240cf7bb623e8d89d9fa216c0c5521e98f08a1
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e7ad5fe225a10041b672270a6f4407985c4be7b9761f9ae2ad5bfcebd45318c3caa03633dc066faf845e5b818fc36e8408e5991abf125d394b29b8af96ade5a3
|
7
|
+
data.tar.gz: b7ba3b33bda40558ebc76e9a1cf4c9fd803fb4881e28eac16f38e64f5562ca6748bbdb206a12a416ab19376f619be26d52986339bedd9b929dc33b65fa68a36a
|
data/.codacy.yml
ADDED
data/.markdownlint.json
ADDED
data/.rubocop_todo.yml
CHANGED
@@ -1,6 +1,6 @@
|
|
1
1
|
# This configuration was generated by
|
2
2
|
# `rubocop --auto-gen-config`
|
3
|
-
# on 2025-08-
|
3
|
+
# on 2025-08-18 22:27:27 UTC using RuboCop version 1.79.2.
|
4
4
|
# The point is for the user to remove these configuration records
|
5
5
|
# one by one as the offenses are removed from the code base.
|
6
6
|
# Note that changes in the inspected code, or installation of new
|
@@ -13,11 +13,7 @@ RSpec/NoExpectationExample:
|
|
13
13
|
Exclude:
|
14
14
|
- 'spec/lib/kiroshi_spec.rb'
|
15
15
|
|
16
|
-
# Offense count:
|
17
|
-
|
18
|
-
Style/Documentation:
|
16
|
+
# Offense count: 1
|
17
|
+
RSpec/PendingWithoutReason:
|
19
18
|
Exclude:
|
20
|
-
- 'spec
|
21
|
-
- 'test/**/*'
|
22
|
-
- 'lib/kiroshi/filter.rb'
|
23
|
-
- 'lib/kiroshi/filters.rb'
|
19
|
+
- 'spec/lib/kiroshi/filters_spec.rb'
|
data/README.md
CHANGED
@@ -7,16 +7,16 @@
|
|
7
7
|
|
8
8
|
## Yard Documentation
|
9
9
|
|
10
|
-
[https://www.rubydoc.info/gems/kiroshi/0.
|
10
|
+
[https://www.rubydoc.info/gems/kiroshi/0.3.0](https://www.rubydoc.info/gems/kiroshi/0.3.0)
|
11
11
|
|
12
12
|
Kiroshi has been designed to make filtering ActiveRecord queries easier
|
13
13
|
by providing a flexible and reusable filtering system. It allows you to
|
14
14
|
define filter sets that can be applied to any ActiveRecord scope,
|
15
15
|
supporting both exact matches and partial matching using SQL LIKE operations.
|
16
16
|
|
17
|
-
Current Release: [0.
|
17
|
+
Current Release: [0.3.0](https://github.com/darthjee/kiroshi/tree/0.3.0)
|
18
18
|
|
19
|
-
[Next release](https://github.com/darthjee/kiroshi/compare/0.
|
19
|
+
[Next release](https://github.com/darthjee/kiroshi/compare/0.3.0...master)
|
20
20
|
|
21
21
|
## Installation
|
22
22
|
|
@@ -67,6 +67,9 @@ Kiroshi supports two types of matching:
|
|
67
67
|
- `:exact` - Exact match (default)
|
68
68
|
- `:like` - Partial match using SQL LIKE
|
69
69
|
|
70
|
+
<details>
|
71
|
+
<summary>Specifying filter types</summary>
|
72
|
+
|
70
73
|
```ruby
|
71
74
|
class UserFilters < Kiroshi::Filters
|
72
75
|
filter_by :email, match: :like # Partial matching
|
@@ -78,11 +81,15 @@ filters = UserFilters.new(email: 'admin', role: 'moderator')
|
|
78
81
|
filtered_users = filters.apply(User.all)
|
79
82
|
# Generates: WHERE email LIKE '%admin%' AND role = 'moderator'
|
80
83
|
```
|
84
|
+
</details>
|
81
85
|
|
82
86
|
#### Advanced Examples
|
83
87
|
|
84
88
|
##### Multiple Filter Types
|
85
89
|
|
90
|
+
<details>
|
91
|
+
<summary>Applying only some filters</summary>
|
92
|
+
|
86
93
|
```ruby
|
87
94
|
class ProductFilters < Kiroshi::Filters
|
88
95
|
filter_by :name, match: :like
|
@@ -96,9 +103,13 @@ filters = ProductFilters.new(name: 'laptop', category: 'electronics')
|
|
96
103
|
products = filters.apply(Product.all)
|
97
104
|
# Only name and category filters are applied, price and brand are ignored
|
98
105
|
```
|
106
|
+
</details>
|
99
107
|
|
100
108
|
##### Controller Integration
|
101
109
|
|
110
|
+
<details>
|
111
|
+
<summary>Using filters in Rails controllers</summary>
|
112
|
+
|
102
113
|
```ruby
|
103
114
|
# URL: /documents?filter[name]=report&filter[status]=published&filter[author]=john
|
104
115
|
class DocumentsController < ApplicationController
|
@@ -125,9 +136,13 @@ class DocumentFilters < Kiroshi::Filters
|
|
125
136
|
filter_by :author, match: :like
|
126
137
|
end
|
127
138
|
```
|
139
|
+
</details>
|
128
140
|
|
129
141
|
##### Nested Resource Filtering
|
130
142
|
|
143
|
+
<details>
|
144
|
+
<summary>Filtering nested resources</summary>
|
145
|
+
|
131
146
|
```ruby
|
132
147
|
# URL: /users/123/articles?filter[title]=ruby&filter[published]=true&filter[tag]=tutorial
|
133
148
|
class ArticleFilters < Kiroshi::Filters
|
@@ -146,9 +161,13 @@ def article_filters
|
|
146
161
|
ArticleFilters.new(params[:filter]&.permit(:title, :published, :tag))
|
147
162
|
end
|
148
163
|
```
|
164
|
+
</details>
|
149
165
|
|
150
166
|
##### Joined Tables and Table Qualification
|
151
167
|
|
168
|
+
<details>
|
169
|
+
<summary>Working with joined tables</summary>
|
170
|
+
|
152
171
|
When working with joined tables that have columns with the same name, you can specify which table to filter on using the `table` parameter:
|
153
172
|
|
154
173
|
```ruby
|
@@ -165,9 +184,13 @@ filters = DocumentFilters.new(tag_name: 'ruby', status: 'published')
|
|
165
184
|
filtered_documents = filters.apply(scope)
|
166
185
|
# Generates: WHERE tags.name LIKE '%ruby%' AND documents.status = 'published'
|
167
186
|
```
|
187
|
+
</details>
|
168
188
|
|
169
189
|
###### Table Qualification Examples
|
170
190
|
|
191
|
+
<details>
|
192
|
+
<summary>Advanced table qualification scenarios</summary>
|
193
|
+
|
171
194
|
```ruby
|
172
195
|
# Filter documents by tag name and document status
|
173
196
|
class DocumentTagFilters < Kiroshi::Filters
|
@@ -201,83 +224,54 @@ result = filters.apply(scope)
|
|
201
224
|
```
|
202
225
|
|
203
226
|
The `table` parameter accepts both symbols and strings, and helps resolve column name ambiguity in complex joined queries.
|
227
|
+
</details>
|
204
228
|
|
205
|
-
|
229
|
+
##### Custom Column Mapping
|
206
230
|
|
207
|
-
|
208
|
-
|
209
|
-
It's automatically used by `Kiroshi::Filters`, but can also be used standalone.
|
231
|
+
<details>
|
232
|
+
<summary>Using different filter keys from database columns</summary>
|
210
233
|
|
211
|
-
|
234
|
+
Sometimes you may want to use a different filter key name from the database column name. The `column` parameter allows you to specify which database column to query while keeping a descriptive filter key:
|
212
235
|
|
213
236
|
```ruby
|
214
|
-
|
215
|
-
|
216
|
-
|
217
|
-
|
218
|
-
|
219
|
-
scope = Document.all
|
220
|
-
scope = name_filter.apply(scope, { name: 'report' })
|
221
|
-
scope = status_filter.apply(scope, { status: 'published' })
|
222
|
-
```
|
223
|
-
|
224
|
-
#### Filter Options
|
225
|
-
|
226
|
-
- `match: :exact` - Performs exact matching (default)
|
227
|
-
- `match: :like` - Performs partial matching using SQL LIKE
|
228
|
-
- `table: :table_name` - Specifies which table to filter on (useful for joined queries)
|
237
|
+
class UserFilters < Kiroshi::Filters
|
238
|
+
filter_by :full_name, column: :name, match: :like # Filter key 'full_name' queries 'name' column
|
239
|
+
filter_by :user_email, column: :email, match: :like # Filter key 'user_email' queries 'email' column
|
240
|
+
filter_by :account_status, column: :status # Filter key 'account_status' queries 'status' column
|
241
|
+
end
|
229
242
|
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
exact_filter.apply(Document.all, { status: 'published' })
|
234
|
-
# Generates: WHERE status = 'published'
|
235
|
-
|
236
|
-
# LIKE match filter
|
237
|
-
like_filter = Kiroshi::Filter.new(:title, match: :like)
|
238
|
-
like_filter.apply(Document.all, { title: 'Ruby' })
|
239
|
-
# Generates: WHERE title LIKE '%Ruby%'
|
240
|
-
|
241
|
-
# Table-qualified filter for joined queries
|
242
|
-
tag_filter = Kiroshi::Filter.new(:name, match: :like, table: :tags)
|
243
|
-
tag_filter.apply(Document.joins(:tags), { name: 'programming' })
|
244
|
-
# Generates: WHERE tags.name LIKE '%programming%'
|
245
|
-
|
246
|
-
# Document-specific filter in joined query
|
247
|
-
doc_filter = Kiroshi::Filter.new(:title, match: :exact, table: :documents)
|
248
|
-
doc_filter.apply(Document.joins(:tags), { title: 'Ruby Guide' })
|
249
|
-
# Generates: WHERE documents.title = 'Ruby Guide'
|
243
|
+
filters = UserFilters.new(full_name: 'John', user_email: 'admin', account_status: 'active')
|
244
|
+
result = filters.apply(User.all)
|
245
|
+
# Generates: WHERE name LIKE '%John%' AND email LIKE '%admin%' AND status = 'active'
|
250
246
|
```
|
247
|
+
</details>
|
248
|
+
|
249
|
+
###### Column Mapping with Table Qualification
|
251
250
|
|
252
|
-
|
251
|
+
<details>
|
252
|
+
<summary>Combining column mapping with table qualification</summary>
|
253
253
|
|
254
|
-
|
254
|
+
You can combine `column` and `table` parameters for complex scenarios:
|
255
255
|
|
256
256
|
```ruby
|
257
|
-
|
258
|
-
|
259
|
-
|
260
|
-
|
261
|
-
|
262
|
-
```
|
257
|
+
class DocumentFilters < Kiroshi::Filters
|
258
|
+
filter_by :author_name, column: :name, table: :users, match: :like # Filter key 'author_name' queries 'users.name'
|
259
|
+
filter_by :doc_title, column: :title, table: :documents, match: :like # Filter key 'doc_title' queries 'documents.title'
|
260
|
+
filter_by :tag_label, column: :name, table: :tags, match: :like # Filter key 'tag_label' queries 'tags.name'
|
261
|
+
end
|
263
262
|
|
264
|
-
|
263
|
+
scope = Document.joins(:user, :tags)
|
264
|
+
filters = DocumentFilters.new(author_name: 'John', doc_title: 'Ruby', tag_label: 'tutorial')
|
265
|
+
result = filters.apply(scope)
|
266
|
+
# Generates: WHERE users.name LIKE '%John%' AND documents.title LIKE '%Ruby%' AND tags.name LIKE '%tutorial%'
|
267
|
+
```
|
265
268
|
|
266
|
-
|
269
|
+
This feature is particularly useful when:
|
270
|
+
- Creating more descriptive filter parameter names for APIs
|
271
|
+
- Avoiding naming conflicts between filter keys and existing method names
|
272
|
+
- Building user-friendly filter interfaces with intuitive parameter names
|
273
|
+
</details>
|
267
274
|
|
268
|
-
|
269
|
-
# Without table specification - may cause ambiguity
|
270
|
-
scope = Document.joins(:tags) # Both documents and tags have 'name' column
|
271
|
-
|
272
|
-
# Specify which table to filter on
|
273
|
-
name_filter = Kiroshi::Filter.new(:name, match: :like, table: :tags)
|
274
|
-
result = name_filter.apply(scope, { name: 'ruby' })
|
275
|
-
# Generates: WHERE tags.name LIKE '%ruby%'
|
276
|
-
|
277
|
-
# Or filter by document name specifically
|
278
|
-
doc_name_filter = Kiroshi::Filter.new(:name, match: :like, table: :documents)
|
279
|
-
result = doc_name_filter.apply(scope, { name: 'guide' })
|
280
|
-
# Generates: WHERE documents.name LIKE '%guide%'
|
281
|
-
```
|
275
|
+
## API Reference
|
282
276
|
|
283
|
-
|
277
|
+
Kiroshi provides a simple, clean API focused on the `Kiroshi::Filters` class. Individual filters are handled internally and don't require direct interaction in most use cases.
|
data/lib/kiroshi/filter.rb
CHANGED
@@ -1,6 +1,7 @@
|
|
1
1
|
# frozen_string_literal: true
|
2
2
|
|
3
3
|
module Kiroshi
|
4
|
+
# @api private
|
4
5
|
# @author darthjee
|
5
6
|
#
|
6
7
|
# A filter class that applies filtering logic to ActiveRecord scopes
|
@@ -10,22 +11,26 @@ module Kiroshi
|
|
10
11
|
#
|
11
12
|
# @example Creating and applying an exact filter
|
12
13
|
# filter = Kiroshi::Filter.new(:name)
|
13
|
-
# filtered_scope = filter.apply(Document.all,
|
14
|
+
# filtered_scope = filter.apply(scope: Document.all, value: 'John')
|
14
15
|
#
|
15
16
|
# @example Creating and applying a LIKE filter
|
16
17
|
# filter = Kiroshi::Filter.new(:title, match: :like)
|
17
|
-
# filtered_scope = filter.apply(Article.all,
|
18
|
+
# filtered_scope = filter.apply(scope: Article.all, value: 'Ruby')
|
19
|
+
#
|
20
|
+
# @example Creating and applying a filter with specific value
|
21
|
+
# filter = Kiroshi::Filter.new(:status)
|
22
|
+
# filtered_scope = filter.apply(scope: Document.all, value: 'published')
|
18
23
|
#
|
19
24
|
# @since 0.1.0
|
20
25
|
class Filter
|
21
|
-
attr_reader :
|
26
|
+
attr_reader :filter_key, :match, :table_name
|
22
27
|
|
23
|
-
# @!method
|
28
|
+
# @!method filter_key
|
24
29
|
# @api private
|
25
30
|
#
|
26
|
-
# Returns the
|
31
|
+
# Returns the filter key name to identify this filter
|
27
32
|
#
|
28
|
-
# @return [Symbol] the
|
33
|
+
# @return [Symbol] the filter key name to identify this filter
|
29
34
|
|
30
35
|
# @!method match
|
31
36
|
# @api private
|
@@ -37,15 +42,16 @@ module Kiroshi
|
|
37
42
|
# @!method table_name
|
38
43
|
# @api private
|
39
44
|
#
|
40
|
-
# Returns the table name to qualify the
|
45
|
+
# Returns the table name to qualify the column
|
41
46
|
#
|
42
47
|
# @return [String, String, nil] the table name or nil if not specified
|
43
48
|
|
44
49
|
# Creates a new Filter instance
|
45
50
|
#
|
46
|
-
# @param
|
51
|
+
# @param filter_key [Symbol] the filter key name to identify this filter
|
47
52
|
# @param match [Symbol] the matching type, defaults to :exact
|
48
|
-
# @param table [String, Symbol, nil] the table name to qualify the
|
53
|
+
# @param table [String, Symbol, nil] the table name to qualify the column, defaults to nil
|
54
|
+
# @param column [Symbol] the column name to use in database queries, defaults to filter_key
|
49
55
|
# @option match [Symbol] :exact performs exact matching (default)
|
50
56
|
# @option match [Symbol] :like performs partial matching using SQL LIKE
|
51
57
|
#
|
@@ -58,52 +64,68 @@ module Kiroshi
|
|
58
64
|
# @example Creating a filter with table qualification
|
59
65
|
# filter = Kiroshi::Filter.new(:name, table: 'documents')
|
60
66
|
#
|
61
|
-
# @
|
62
|
-
|
63
|
-
|
67
|
+
# @example Creating a filter with custom column name
|
68
|
+
# filter = Kiroshi::Filter.new(:user_name, column: :full_name)
|
69
|
+
#
|
70
|
+
# @since 0.3.0
|
71
|
+
def initialize(filter_key, match: :exact, table: nil, column: nil)
|
72
|
+
@filter_key = filter_key
|
64
73
|
@match = match
|
65
74
|
@table_name = table
|
75
|
+
@column = column
|
76
|
+
end
|
77
|
+
|
78
|
+
# Returns the column name to use in database queries
|
79
|
+
#
|
80
|
+
# Uses lazy initialization - defaults to filter_key if no column was specified.
|
81
|
+
# This allows for flexible column mapping while maintaining backward compatibility.
|
82
|
+
#
|
83
|
+
# @return [Symbol] the column name to use in database queries
|
84
|
+
#
|
85
|
+
# @since 0.3.0
|
86
|
+
def column
|
87
|
+
@column ||= filter_key
|
66
88
|
end
|
67
89
|
|
68
90
|
# Applies the filter to the given scope
|
69
91
|
#
|
70
|
-
# This method
|
71
|
-
#
|
72
|
-
#
|
92
|
+
# This method applies the appropriate WHERE clause to the scope using the
|
93
|
+
# provided value. If no value is present or the value is blank, the original
|
94
|
+
# scope is returned unchanged.
|
73
95
|
#
|
74
96
|
# @param scope [ActiveRecord::Relation] the ActiveRecord scope to filter
|
75
|
-
# @param
|
97
|
+
# @param value [Object, nil] the value to use for filtering, defaults to nil
|
76
98
|
#
|
77
99
|
# @return [ActiveRecord::Relation] the filtered scope
|
78
100
|
#
|
79
101
|
# @example Applying an exact filter
|
80
102
|
# filter = Kiroshi::Filter.new(:status)
|
81
|
-
# filter.apply(Document.all,
|
103
|
+
# filter.apply(scope: Document.all, value: 'published')
|
82
104
|
# # Generates: WHERE status = 'published'
|
83
105
|
#
|
84
106
|
# @example Applying a LIKE filter
|
85
107
|
# filter = Kiroshi::Filter.new(:title, match: :like)
|
86
|
-
# filter.apply(Article.all,
|
108
|
+
# filter.apply(scope: Article.all, value: 'Ruby')
|
87
109
|
# # Generates: WHERE title LIKE '%Ruby%'
|
88
110
|
#
|
89
111
|
# @example Applying a filter with table qualification
|
90
112
|
# filter = Kiroshi::Filter.new(:name, table: 'documents')
|
91
|
-
# filter.apply(Document.joins(:tags),
|
113
|
+
# filter.apply(scope: Document.joins(:tags), value: 'report')
|
92
114
|
# # Generates: WHERE documents.name = 'report'
|
93
115
|
#
|
94
116
|
# @example Applying a filter with table qualification for tags
|
95
117
|
# filter = Kiroshi::Filter.new(:name, table: 'tags')
|
96
|
-
# filter.apply(Document.joins(:tags),
|
118
|
+
# filter.apply(scope: Document.joins(:tags), value: 'ruby')
|
97
119
|
# # Generates: WHERE tags.name = 'ruby'
|
98
120
|
#
|
99
121
|
# @example With empty filter value
|
100
122
|
# filter = Kiroshi::Filter.new(:name)
|
101
|
-
# filter.apply(User.all,
|
123
|
+
# filter.apply(scope: User.all, value: nil)
|
102
124
|
# # Returns the original scope unchanged
|
103
125
|
#
|
104
|
-
# @since 0.
|
105
|
-
def apply(scope
|
106
|
-
runner = FilterRunner.new(filter: self, scope: scope,
|
126
|
+
# @since 0.2.0
|
127
|
+
def apply(scope:, value: nil)
|
128
|
+
runner = FilterRunner.new(filter: self, scope: scope, value: value)
|
107
129
|
runner.apply
|
108
130
|
end
|
109
131
|
end
|
@@ -13,14 +13,14 @@ module Kiroshi
|
|
13
13
|
# @example Applying exact match query
|
14
14
|
# query = Kiroshi::FilterQuery::Exact.new(filter_runner)
|
15
15
|
# query.apply
|
16
|
-
# # Generates: WHERE
|
16
|
+
# # Generates: WHERE table_name.column = value
|
17
17
|
#
|
18
18
|
# @since 0.1.1
|
19
19
|
class Exact < FilterQuery
|
20
20
|
# Applies exact match filtering to the scope
|
21
21
|
#
|
22
22
|
# This method generates a WHERE clause with exact equality matching
|
23
|
-
# for the filter's
|
23
|
+
# for the filter's column and value.
|
24
24
|
#
|
25
25
|
# @return [ActiveRecord::Relation] the filtered scope with exact match
|
26
26
|
#
|
@@ -31,7 +31,7 @@ module Kiroshi
|
|
31
31
|
#
|
32
32
|
# @since 0.1.1
|
33
33
|
def apply
|
34
|
-
scope.where(table_name => {
|
34
|
+
scope.where(table_name => { column => value })
|
35
35
|
end
|
36
36
|
end
|
37
37
|
end
|
@@ -13,7 +13,7 @@ module Kiroshi
|
|
13
13
|
# @example Applying LIKE match query
|
14
14
|
# query = Kiroshi::FilterQuery::Like.new(filter_runner)
|
15
15
|
# query.apply
|
16
|
-
# # Generates: WHERE table_name.
|
16
|
+
# # Generates: WHERE "table_name"."column" LIKE '%value%'
|
17
17
|
#
|
18
18
|
# @since 0.1.1
|
19
19
|
class Like < FilterQuery
|
@@ -28,14 +28,38 @@ module Kiroshi
|
|
28
28
|
# @example Applying LIKE match
|
29
29
|
# query = Like.new(filter_runner)
|
30
30
|
# query.apply
|
31
|
-
# # Generates: WHERE documents.name LIKE '%ruby%'
|
31
|
+
# # Generates: WHERE "documents"."name" LIKE '%ruby%'
|
32
32
|
#
|
33
33
|
# @since 0.1.1
|
34
34
|
def apply
|
35
|
-
scope.where(
|
36
|
-
|
37
|
-
|
38
|
-
|
35
|
+
scope.where(sql_query, "%#{value}%")
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
# @api private
|
41
|
+
# @private
|
42
|
+
#
|
43
|
+
# Builds the SQL query string for LIKE matching
|
44
|
+
#
|
45
|
+
# This method constructs the SQL fragment with proper table and column
|
46
|
+
# qualification using double quotes to avoid conflicts with reserved words.
|
47
|
+
#
|
48
|
+
# @return [String] the SQL query fragment for LIKE matching
|
49
|
+
#
|
50
|
+
# @example Generated SQL fragment
|
51
|
+
# sql_query # => ' # Constructs the parameterized SQL query string for column matching
|
52
|
+
#
|
53
|
+
# @return [String] The SQL query string with placeholders
|
54
|
+
# @example
|
55
|
+
# sql_query
|
56
|
+
# # Returns: '"table_name"."column" LIKE ?''
|
57
|
+
#
|
58
|
+
# @since 0.3.0
|
59
|
+
def sql_query
|
60
|
+
<<~SQL.squish
|
61
|
+
"#{table_name}"."#{column}" LIKE ?
|
62
|
+
SQL
|
39
63
|
end
|
40
64
|
end
|
41
65
|
end
|
data/lib/kiroshi/filter_query.rb
CHANGED
@@ -94,38 +94,34 @@ module Kiroshi
|
|
94
94
|
#
|
95
95
|
# @return [Kiroshi::FilterRunner] the filter runner instance
|
96
96
|
|
97
|
-
delegate :scope, :
|
97
|
+
delegate :scope, :column, :table_name, :value, to: :filter_runner
|
98
98
|
|
99
99
|
# @!method scope
|
100
100
|
# @api private
|
101
|
-
# @private
|
102
101
|
#
|
103
|
-
# Returns the
|
102
|
+
# Returns the current scope being filtered
|
104
103
|
#
|
105
|
-
# @return [ActiveRecord::Relation] the scope
|
104
|
+
# @return [ActiveRecord::Relation] the scope
|
106
105
|
|
107
|
-
# @!method
|
106
|
+
# @!method column
|
108
107
|
# @api private
|
109
|
-
# @private
|
110
108
|
#
|
111
|
-
# Returns the
|
109
|
+
# Returns the column name to use in database queries
|
112
110
|
#
|
113
|
-
# @return [Symbol] the
|
111
|
+
# @return [Symbol] the column name
|
114
112
|
|
115
113
|
# @!method table_name
|
116
114
|
# @api private
|
117
|
-
# @private
|
118
115
|
#
|
119
|
-
# Returns the table name
|
116
|
+
# Returns the table name for the filter
|
120
117
|
#
|
121
118
|
# @return [String] the table name
|
122
119
|
|
123
|
-
# @!method
|
120
|
+
# @!method value
|
124
121
|
# @api private
|
125
|
-
# @private
|
126
122
|
#
|
127
|
-
# Returns the filter value
|
123
|
+
# Returns the filter value
|
128
124
|
#
|
129
|
-
# @return [Object
|
125
|
+
# @return [Object] the filter value
|
130
126
|
end
|
131
127
|
end
|