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
Show file tree
Hide file tree
Changes from 1 commit
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
3 changes: 2 additions & 1 deletion lib/administrate/search.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,10 @@ def run
private

delegate :resource_class, to: :resolver
delegate :table_name, to: :resource_class
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

kind of thinking this was a bad idea, and that using resource_class.table_name might be a bit more clear. let me know what you think!

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would use resource_class.table_name because you aren't' frequently changing data types as you chain and you don't expect resource_class to become possibly nil at any point.


def query
search_attributes.map { |attr| "lower(#{attr}) LIKE ?" }.join(" OR ")
search_attributes.map { |attr| "lower(#{table_name}.#{attr}) LIKE ?" }.join(" OR ")
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@klaseskilson someone just opened a PR showing that this code doesn't align with the SQL standard in #829. Are you able to quote the table name and, just to leave things in better shape than they were before, the column name as well?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing, on it.

end

def search_terms
Expand Down
4 changes: 2 additions & 2 deletions spec/lib/administrate/search_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,11 @@ 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 ?",
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The SQL should probably be:

lower("users"."name") LIKE ? OR lower("users"."email") LIKE ?

"%test%",
"%test%",
]
Expand Down