Navigation Menu

Skip to content

Commit

Permalink
Merge pull request #1414 from sporkd/ar-case-insensitive-search
Browse files Browse the repository at this point in the history
Make activerecord adapter perform case-insensitive searches
  • Loading branch information
sferik committed Nov 27, 2012
2 parents 4b06b94 + 8a3b253 commit c911cc3
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 12 deletions.
10 changes: 5 additions & 5 deletions lib/rails_admin/adapters/active_record.rb
Expand Up @@ -201,17 +201,17 @@ def build_statement(column, type, value, operator)
return if value.blank?
value = case operator
when 'default', 'like'
"%#{value}%"
"%#{value.downcase}%"
when 'starts_with'
"#{value}%"
"#{value.downcase}%"
when 'ends_with'
"%#{value}"
"%#{value.downcase}"
when 'is', '='
"#{value}"
"#{value.downcase}"
else
return
end
["(#{column} #{like_operator} ?)", value]
["(LOWER(#{column}) #{like_operator} ?)", value]
when :date
start_date, end_date = get_filtering_duration(operator, value)

Expand Down
18 changes: 11 additions & 7 deletions spec/unit/adapters/active_record_spec.rb
Expand Up @@ -153,8 +153,8 @@ class ARComment < ActiveRecord::Base
expect(param[:primary_key_proc].call).to eq('id')
expect(param[:model_proc].call).to eq(ARComment)
end


it 'has correct opposite model lookup for polymorphic associations' do
RailsAdmin::Config.stub!(:models_pool).and_return(["ARBlog", "ARPost", "ARCategory", "ARUser", "ARProfile", "ARComment"])
expect(@category.associations.find{|a| a[:name] == :librarian}[:model_proc].call).to eq [ARUser]
Expand Down Expand Up @@ -362,13 +362,17 @@ class ARComment < ActiveRecord::Base
it "supports string type query" do
expect(@abstract_model.send(:build_statement, :field, :string, "", nil)).to be_nil
expect(@abstract_model.send(:build_statement, :field, :string, "foo", "was")).to be_nil
expect(@abstract_model.send(:build_statement, :field, :string, "foo", "default")).to eq(["(field #{@like} ?)", "%foo%"])
expect(@abstract_model.send(:build_statement, :field, :string, "foo", "like")).to eq(["(field #{@like} ?)", "%foo%"])
expect(@abstract_model.send(:build_statement, :field, :string, "foo", "starts_with")).to eq(["(field #{@like} ?)", "foo%"])
expect(@abstract_model.send(:build_statement, :field, :string, "foo", "ends_with")).to eq(["(field #{@like} ?)", "%foo"])
expect(@abstract_model.send(:build_statement, :field, :string, "foo", "is")).to eq(["(field #{@like} ?)", "foo"])
expect(@abstract_model.send(:build_statement, :field, :string, "foo", "default")).to eq(["(LOWER(field) #{@like} ?)", "%foo%"])
expect(@abstract_model.send(:build_statement, :field, :string, "foo", "like")).to eq(["(LOWER(field) #{@like} ?)", "%foo%"])
expect(@abstract_model.send(:build_statement, :field, :string, "foo", "starts_with")).to eq(["(LOWER(field) #{@like} ?)", "foo%"])
expect(@abstract_model.send(:build_statement, :field, :string, "foo", "ends_with")).to eq(["(LOWER(field) #{@like} ?)", "%foo"])
expect(@abstract_model.send(:build_statement, :field, :string, "foo", "is")).to eq(["(LOWER(field) #{@like} ?)", "foo"])
end

it "performs case-insensitive searches" do
expect(@abstract_model.send(:build_statement, :field, :string, "foo", "default")).to eq(["(LOWER(field) #{@like} ?)", "%foo%"])
expect(@abstract_model.send(:build_statement, :field, :string, "FOO", "default")).to eq(["(LOWER(field) #{@like} ?)", "%foo%"])
end

it "supports date type query" do
expect(@abstract_model.send(:filter_conditions, { "date_field" => { "1" => { :v => ["", "01/02/2012", "01/03/2012"], :o => 'between' } } })).to eq(["((field_tests.date_field BETWEEN ? AND ?))", Date.new(2012,1,2), Date.new(2012,1,3)])
Expand Down

0 comments on commit c911cc3

Please sign in to comment.