diff --git a/lib/graphiti/adapters/active_record.rb b/lib/graphiti/adapters/active_record.rb index 00d2c8e4..8b2adfee 100644 --- a/lib/graphiti/adapters/active_record.rb +++ b/lib/graphiti/adapters/active_record.rb @@ -41,22 +41,24 @@ def filter_not_eq(scope, attribute, value) alias_method :filter_enum_not_eql, :filter_not_eq def filter_string_eq(scope, attribute, value, is_not: false) + return filter_string_eql(scope, attribute, nil, is_not: is_not) if Array(value).compact.blank? + column = column_for(scope, attribute) - clause = column.lower.eq_any(value.map(&:downcase)) + clause = column.lower.eq_any(value.map { |val| val.downcase }) is_not ? scope.where.not(clause) : scope.where(clause) end def filter_string_eql(scope, attribute, value, is_not: false) - clause = {attribute => value} + clause = {attribute => value.presence} is_not ? scope.where.not(clause) : scope.where(clause) end def filter_string_not_eq(scope, attribute, value) - filter_string_eq(scope, attribute, value, is_not: true) + filter_string_eq(scope, attribute, value.presence, is_not: true) end def filter_string_not_eql(scope, attribute, value) - filter_string_eql(scope, attribute, value, is_not: true) + filter_string_eql(scope, attribute, value.presence, is_not: true) end # Arel has different match escaping behavior before rails 5. diff --git a/spec/integration/rails/finders_spec.rb b/spec/integration/rails/finders_spec.rb index dca3e7f7..45a452cb 100644 --- a/spec/integration/rails/finders_spec.rb +++ b/spec/integration/rails/finders_spec.rb @@ -281,6 +281,15 @@ def resource it "executes case-insensitive search" do expect(ids).to eq([author2.id, author3.id]) end + + context "with nil value" do + let(:value) { nil } + let!(:author3) { Legacy::Author.create! } + + it "works" do + expect(ids).to eq([author3.id]) + end + end end context "nothing" do @@ -297,6 +306,15 @@ def resource it "executes case-sensitive search" do expect(ids).to eq([author3.id]) end + + context "with nil value" do + let(:value) { nil } + let!(:author3) { Legacy::Author.create! } + + it "works" do + expect(ids).to eq([author3.id]) + end + end end context "!eq" do @@ -305,6 +323,15 @@ def resource it "executes case-insensitive NOT search" do expect(ids).to eq([author1.id]) end + + context "with nil value" do + let(:value) { {'!eq': nil} } + let!(:author3) { Legacy::Author.create! } + + it "works" do + expect(ids).to eq([author1.id, author2.id]) + end + end end # test not_ alternative to ! @@ -314,6 +341,15 @@ def resource it "executes case-insensitive NOT search" do expect(ids).to eq([author1.id]) end + + context "with nil value" do + let(:value) { {not_eq: nil} } + let!(:author3) { Legacy::Author.create! } + + it "works" do + expect(ids).to eq([author1.id, author2.id]) + end + end end # test not_ alternative to ! diff --git a/spec/tmp/local_secret.txt b/spec/tmp/local_secret.txt new file mode 100644 index 00000000..6fe8fe7e --- /dev/null +++ b/spec/tmp/local_secret.txt @@ -0,0 +1 @@ +85b70ecb76ac772627f629a92a3b6f480e5ef6f12d4ba704656d87a8e9c2c2bad3155ea4fab11f4837d94dba3436c9c31bd9468f0603a4c9f0aa28641ac6a5d5 \ No newline at end of file