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

Add table name to search query #830

Merged
merged 2 commits into from
Apr 28, 2017
Merged
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
7 changes: 6 additions & 1 deletion lib/administrate/search.rb
Original file line number Diff line number Diff line change
@@ -21,7 +21,12 @@ def run
delegate :resource_class, to: :resolver

def query
search_attributes.map { |attr| "lower(#{attr}) LIKE ?" }.join(" OR ")
search_attributes.map do |attr|
table_name = ActiveRecord::Base.connection.
quote_table_name(resource_class.table_name)
attr_name = ActiveRecord::Base.connection.quote_column_name(attr)
"lower(#{table_name}.#{attr_name}) LIKE ?"
end.join(" OR ")
end

def search_terms
5 changes: 3 additions & 2 deletions spec/lib/administrate/search_spec.rb
Original file line number Diff line number Diff line change
@@ -43,11 +43,12 @@ class User; end

it "searches using lower() + LIKE for all searchable fields" do
begin
class User; end
class User < ActiveRecord::Base; end
resolver = double(resource_class: User, dashboard_class: MockDashboard)
search = Administrate::Search.new(resolver, "test")
expected_query = [
"lower(name) LIKE ? OR lower(email) LIKE ?",
"lower(\"users\".\"name\") LIKE ?"\
" OR lower(\"users\".\"email\") LIKE ?",
"%test%",
"%test%",
]