-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -19,9 +19,10 @@ def run | |
private | ||
|
||
delegate :resource_class, to: :resolver | ||
delegate :table_name, to: :resource_class | ||
|
||
def query | ||
search_attributes.map { |attr| "lower(#{attr}) LIKE ?" }.join(" OR ") | ||
search_attributes.map { |attr| "lower(#{table_name}.#{attr}) LIKE ?" }.join(" OR ") | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Sure thing, on it. |
||
end | ||
|
||
def search_terms | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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 ?", | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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%", | ||
] | ||
|
There was a problem hiding this comment.
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!There was a problem hiding this comment.
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 expectresource_class
to become possiblynil
at any point.