Skip to content

Commit

Permalink
Query: Added ILIKE support for PostGIS adapter and multibyte downcase…
Browse files Browse the repository at this point in the history
… for other adapters
  • Loading branch information
Sergey Baev committed Nov 14, 2016
1 parent 9e1c66e commit 2ad8661
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 12 deletions.
15 changes: 10 additions & 5 deletions lib/rails_admin/adapters/active_record.rb
Original file line number Diff line number Diff line change
Expand Up @@ -221,22 +221,27 @@ def build_statement_for_belongs_to_association

def build_statement_for_string_or_text
return if @value.blank?

unless ['postgresql', 'postgis'].include? ar_adapter
@value = @value.mb_chars.downcase
end

@value = begin
case @operator
when 'default', 'like'
"%#{@value.downcase}%"
"%#{@value}%"
when 'starts_with'
"#{@value.downcase}%"
"#{@value}%"
when 'ends_with'
"%#{@value.downcase}"
"%#{@value}"
when 'is', '='
@value.downcase
@value
else
return
end
end

if ar_adapter == 'postgresql'
if ['postgresql', 'postgis'].include? ar_adapter
["(#{@column} ILIKE ?)", @value]
else
["(LOWER(#{@column}) LIKE ?)", @value]
Expand Down
27 changes: 20 additions & 7 deletions spec/rails_admin/adapters/active_record_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

describe 'RailsAdmin::Adapters::ActiveRecord', active_record: true do
before do
@like = if ::ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'postgresql'
@like = if ['postgresql', 'postgis'].include? ::ActiveRecord::Base.configurations[Rails.env]['adapter']
'(field ILIKE ?)'
else
'(LOWER(field) LIKE ?)'
Expand Down Expand Up @@ -36,7 +36,11 @@ def predicates_for(scope)
let(:abstract_model) { RailsAdmin::AbstractModel.new('Player') }

before do
@players = FactoryGirl.create_list(:player, 3)
@players = FactoryGirl.create_list(:player, 3) + [
# Multibyte players
FactoryGirl.create(:player, name: 'Антоха'),
FactoryGirl.create(:player, name: 'Петруха'),
]
end

it '#new returns instance of AbstractObject' do
Expand Down Expand Up @@ -77,7 +81,7 @@ class PlayerWithDefaultScope < Player

it '#destroy destroys multiple items' do
abstract_model.destroy(@players[0..1])
expect(Player.all).to eq(@players[2..2])
expect(Player.all).to eq(@players[2..-1])
end

it '#where returns filtered results' do
Expand All @@ -102,8 +106,8 @@ class PlayerWithDefaultScope < Player
end

it 'supports pagination' do
expect(abstract_model.all(sort: 'id', page: 2, per: 1)).to eq(@players[1..1])
expect(abstract_model.all(sort: 'id', page: 1, per: 2)).to eq(@players[1..2].reverse)
expect(abstract_model.all(sort: 'id', page: 2, per: 1)).to eq(@players[-2, 1])
expect(abstract_model.all(sort: 'id', page: 1, per: 2)).to eq(@players[-2, 2].reverse)
end

it 'supports ordering' do
Expand All @@ -115,6 +119,13 @@ class PlayerWithDefaultScope < Player
expect(results).to eq(@players[1..1])
end

it 'supports multibyte querying' do
unless ::ActiveRecord::Base.configurations[Rails.env]['adapter'] == 'sqlite3'
results = abstract_model.all(query: @players[4].name)
expect(results).to eq(@players[4, 1])
end
end

it 'supports filtering' do
expect(abstract_model.all(filters: {'name' => {'0000' => {o: 'is', v: @players[1].name}}})).to eq(@players[1..1])
end
Expand Down Expand Up @@ -201,8 +212,10 @@ def build_statement(type, value, operator)
end

it 'performs case-insensitive searches' do
expect(build_statement(:string, 'foo', 'default')).to eq([@like, '%foo%'])
expect(build_statement(:string, 'FOO', 'default')).to eq([@like, '%foo%'])
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%'])
end
end

it "supports '_blank' operator" do
Expand Down

0 comments on commit 2ad8661

Please sign in to comment.