Skip to content

Commit

Permalink
Cast all search queries to text. (thoughtbot#1079)
Browse files Browse the repository at this point in the history
All Postgres versions since 8.4 require this to be a string. This casts
the table and column to be strings as it's possible that the column name
is nil.

Fixes thoughtbot#620.
  • Loading branch information
nickcharlton authored Jan 31, 2018
1 parent 6bfc705 commit 642d737
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
2 changes: 1 addition & 1 deletion lib/administrate/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def query
table_name = ActiveRecord::Base.connection.
quote_table_name(@scoped_resource.table_name)
attr_name = ActiveRecord::Base.connection.quote_column_name(attr)
"lower(#{table_name}.#{attr_name}) LIKE ?"
"LOWER(TEXT(#{table_name}.#{attr_name})) LIKE ?"
end.join(" OR ")
end

Expand Down
15 changes: 7 additions & 8 deletions spec/lib/administrate/search_spec.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
require "spec_helper"
require "support/constant_helpers"
require "rails_helper"
require "administrate/field/string"
require "administrate/field/email"
require "administrate/field/number"
Expand Down Expand Up @@ -45,16 +44,16 @@ class User < ActiveRecord::Base; end
end
end

it "searches using lower() + LIKE for all searchable fields" do
it "searches using LOWER + LIKE for all searchable fields" do
begin
class User < ActiveRecord::Base; end
scoped_object = User.default_scoped
search = Administrate::Search.new(scoped_object,
MockDashboard,
"test")
expected_query = [
"lower(\"users\".\"name\") LIKE ?"\
" OR lower(\"users\".\"email\") LIKE ?",
"LOWER(TEXT(\"users\".\"name\")) LIKE ?"\
" OR LOWER(TEXT(\"users\".\"email\")) LIKE ?",
"%test%",
"%test%",
]
Expand All @@ -66,16 +65,16 @@ class User < ActiveRecord::Base; end
end
end

it "converts search term lower case for latin and cyrillic strings" do
it "converts search term LOWER case for latin and cyrillic strings" do
begin
class User < ActiveRecord::Base; end
scoped_object = User.default_scoped
search = Administrate::Search.new(scoped_object,
MockDashboard,
"Тест Test")
expected_query = [
"lower(\"users\".\"name\") LIKE ?"\
" OR lower(\"users\".\"email\") LIKE ?",
"LOWER(TEXT(\"users\".\"name\")) LIKE ?"\
" OR LOWER(TEXT(\"users\".\"email\")) LIKE ?",
"%тест test%",
"%тест test%",
]
Expand Down

0 comments on commit 642d737

Please sign in to comment.