Skip to content

Commit

Permalink
Cast all search queries to text.
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 #620.
  • Loading branch information
nickcharlton committed Jan 27, 2018
1 parent b98cde5 commit 993e16f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 7 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
11 changes: 5 additions & 6 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 @@ -53,8 +52,8 @@ class User < ActiveRecord::Base; end
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 @@ -74,8 +73,8 @@ class User < ActiveRecord::Base; end
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 993e16f

Please sign in to comment.