Skip to content

Commit af24aa1

Browse files
committed
Filter instead of failing for nil string values
This currently fails with `Unsupported argument type: NilClass. Construct an Arel node instead.` when the scope is resolved. Prior art from #410
1 parent 808bad4 commit af24aa1

3 files changed

Lines changed: 43 additions & 4 deletions

File tree

lib/graphiti/adapters/active_record.rb

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,22 +41,24 @@ def filter_not_eq(scope, attribute, value)
4141
alias_method :filter_enum_not_eql, :filter_not_eq
4242

4343
def filter_string_eq(scope, attribute, value, is_not: false)
44+
return filter_string_eql(scope, attribute, nil, is_not: is_not) if Array(value).compact.blank?
45+
4446
column = column_for(scope, attribute)
45-
clause = column.lower.eq_any(value.map(&:downcase))
47+
clause = column.lower.eq_any(value.map { |val| val.downcase })
4648
is_not ? scope.where.not(clause) : scope.where(clause)
4749
end
4850

4951
def filter_string_eql(scope, attribute, value, is_not: false)
50-
clause = {attribute => value}
52+
clause = {attribute => value.presence}
5153
is_not ? scope.where.not(clause) : scope.where(clause)
5254
end
5355

5456
def filter_string_not_eq(scope, attribute, value)
55-
filter_string_eq(scope, attribute, value, is_not: true)
57+
filter_string_eq(scope, attribute, value.presence, is_not: true)
5658
end
5759

5860
def filter_string_not_eql(scope, attribute, value)
59-
filter_string_eql(scope, attribute, value, is_not: true)
61+
filter_string_eql(scope, attribute, value.presence, is_not: true)
6062
end
6163

6264
# Arel has different match escaping behavior before rails 5.

spec/integration/rails/finders_spec.rb

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,15 @@ def resource
281281
it "executes case-insensitive search" do
282282
expect(ids).to eq([author2.id, author3.id])
283283
end
284+
285+
context "with nil value" do
286+
let(:value) { nil }
287+
let!(:author3) { Legacy::Author.create! }
288+
289+
it "works" do
290+
expect(ids).to eq([author3.id])
291+
end
292+
end
284293
end
285294

286295
context "nothing" do
@@ -297,6 +306,15 @@ def resource
297306
it "executes case-sensitive search" do
298307
expect(ids).to eq([author3.id])
299308
end
309+
310+
context "with nil value" do
311+
let(:value) { nil }
312+
let!(:author3) { Legacy::Author.create! }
313+
314+
it "works" do
315+
expect(ids).to eq([author3.id])
316+
end
317+
end
300318
end
301319

302320
context "!eq" do
@@ -305,6 +323,15 @@ def resource
305323
it "executes case-insensitive NOT search" do
306324
expect(ids).to eq([author1.id])
307325
end
326+
327+
context "with nil value" do
328+
let(:value) { {'!eq': nil} }
329+
let!(:author3) { Legacy::Author.create! }
330+
331+
it "works" do
332+
expect(ids).to eq([author1.id, author2.id])
333+
end
334+
end
308335
end
309336

310337
# test not_ alternative to !
@@ -314,6 +341,15 @@ def resource
314341
it "executes case-insensitive NOT search" do
315342
expect(ids).to eq([author1.id])
316343
end
344+
345+
context "with nil value" do
346+
let(:value) { {not_eq: nil} }
347+
let!(:author3) { Legacy::Author.create! }
348+
349+
it "works" do
350+
expect(ids).to eq([author1.id, author2.id])
351+
end
352+
end
317353
end
318354

319355
# test not_ alternative to !

spec/tmp/local_secret.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
85b70ecb76ac772627f629a92a3b6f480e5ef6f12d4ba704656d87a8e9c2c2bad3155ea4fab11f4837d94dba3436c9c31bd9468f0603a4c9f0aa28641ac6a5d5

0 commit comments

Comments
 (0)