Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use exact match for 'is' and '=' #3000

Merged
merged 1 commit into from
Nov 5, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Use exact match for 'is' and '='
This is in response to #1819 and is inspired by #2988
This changes less code than #2988, which is currently not
passing CI, and preserves postgres checks.
  • Loading branch information
evanboho committed Mar 7, 2018
commit 1a3ddfd8d30e9b0c6c07d28a71b6dac9c1a281ba
4 changes: 2 additions & 2 deletions lib/rails_admin/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,8 @@ def build_statement_for_belongs_to_association
def build_statement_for_string_or_text
return if @value.blank?

return ["(#{@column} = ?)", @value] if ['is', '='].include?(@operator)

unless ['postgresql', 'postgis'].include? ar_adapter
@value = @value.mb_chars.downcase
end
Expand All @@ -232,8 +234,6 @@ def build_statement_for_string_or_text
"#{@value}%"
when 'ends_with'
"%#{@value}"
when 'is', '='
@value
else
return
end
Expand Down
26 changes: 13 additions & 13 deletions spec/rails_admin/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@
require 'timecop'

describe 'RailsAdmin::Adapters::ActiveRecord', active_record: true do
before do
@like = if ['postgresql', 'postgis'].include? ::ActiveRecord::Base.configurations[Rails.env]['adapter']
'(field ILIKE ?)'
else
'(LOWER(field) LIKE ?)'
end
let(:like) do
if ['postgresql', 'postgis'].include? ::ActiveRecord::Base.configurations[Rails.env]['adapter']
'(field ILIKE ?)'
else
'(LOWER(field) LIKE ?)'
end
end

def predicates_for(scope)
Expand Down Expand Up @@ -204,17 +204,17 @@ def build_statement(type, value, operator)
it 'supports string type query' do
expect(build_statement(:string, '', nil)).to be_nil
expect(build_statement(:string, 'foo', 'was')).to be_nil
expect(build_statement(:string, 'foo', 'default')).to eq([@like, '%foo%'])
expect(build_statement(:string, 'foo', 'like')).to eq([@like, '%foo%'])
expect(build_statement(:string, 'foo', 'starts_with')).to eq([@like, 'foo%'])
expect(build_statement(:string, 'foo', 'ends_with')).to eq([@like, '%foo'])
expect(build_statement(:string, 'foo', 'is')).to eq([@like, 'foo'])
expect(build_statement(:string, 'foo', 'default')).to eq([like, '%foo%'])
expect(build_statement(:string, 'foo', 'like')).to eq([like, '%foo%'])
expect(build_statement(:string, 'foo', 'starts_with')).to eq([like, 'foo%'])
expect(build_statement(:string, 'foo', 'ends_with')).to eq([like, '%foo'])
expect(build_statement(:string, 'foo', 'is')).to eq(['(field = ?)', 'foo'])
end

it 'performs case-insensitive searches' do
unless ['postgresql', 'postgis'].include?(::ActiveRecord::Base.configurations[Rails.env]['adapter'])
expect(build_statement(:string, 'foo', 'default')).to eq([@like, '%foo%'])
expect(build_statement(:string, 'FOO', 'default')).to eq([@like, '%foo%'])
expect(build_statement(:string, 'foo', 'default')).to eq([like, '%foo%'])
expect(build_statement(:string, 'FOO', 'default')).to eq([like, '%foo%'])
end
end

Expand Down