-
-
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
Search through association fields #1005
Search through association fields #1005
Conversation
spec/lib/administrate/search_spec.rb
Outdated
end | ||
|
||
it "builds the 'where' clause using the joined tables" do | ||
allow(scoped_object).to receive(:joins).with([:role, :address]).and_return(scoped_object) |
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.
Use %i or %I for an array of symbols.
Line is too long. [97/80]
spec/lib/administrate/search_spec.rb
Outdated
it "joins with the correct association table to query" do | ||
allow(scoped_object).to receive(:where) | ||
|
||
expect(scoped_object).to receive(:joins).with([:role, :address]).and_return(scoped_object) |
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.
Use %i or %I for an array of symbols.
Line is too long. [98/80]
spec/lib/administrate/search_spec.rb
Outdated
Administrate::Search.new( | ||
scoped_object, | ||
MockDashboardWithAssociation, | ||
"Тест 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.
Put a comma after the last parameter of a multiline method call.
spec/lib/administrate/search_spec.rb
Outdated
class MockDashboardWithAssociation | ||
ATTRIBUTE_TYPES = { | ||
role: Administrate::Field::BelongsTo.with_options(searchable: true, searchable_field: 'name'), | ||
address: Administrate::Field::HasOne.with_options(searchable: true, searchable_field: 'street') |
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.
Put a comma after the last item of a multiline hash.
Line is too long. [99/80]
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
spec/lib/administrate/search_spec.rb
Outdated
@@ -13,6 +13,13 @@ class MockDashboard | |||
} | |||
end | |||
|
|||
class MockDashboardWithAssociation | |||
ATTRIBUTE_TYPES = { | |||
role: Administrate::Field::BelongsTo.with_options(searchable: true, searchable_field: 'name'), |
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.
Line is too long. [98/80]
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
primary_key: :code, | ||
foreign_key: :country_code, | ||
searchable: true, | ||
searchable_field: 'name' |
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.
Put a comma after the last parameter of a multiline method call.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
lib/administrate/search.rb
Outdated
[ | ||
Administrate::Field::BelongsTo, | ||
Administrate::Field::HasMany, | ||
Administrate::Field::HasOne |
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.
Put a comma after the last item of a multiline array.
lib/administrate/search.rb
Outdated
attr_reader :resolver, :term | ||
|
||
private |
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.
Useless private access modifier.
lib/administrate/search.rb
Outdated
|
||
def column_to_query(attr) | ||
if association_search?(attr) | ||
ActiveRecord::Base.connection.quote_column_name(attribute_types[attr].searchable_field) |
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.
Line is too long. [95/80]
lib/administrate/search.rb
Outdated
if association_search?(attr) | ||
ActiveRecord::Base.connection.quote_table_name(attr.to_s.pluralize) | ||
else | ||
ActiveRecord::Base.connection.quote_table_name(@scoped_resource.table_name) |
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.
Line is too long. [83/80]
spec/lib/administrate/search_spec.rb
Outdated
|
||
it "builds the 'where' clause using the joined tables" do | ||
allow(scoped_object).to receive(:joins).with(%i(role address)) | ||
.and_return(scoped_object) |
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.
Place the . on the previous line, together with the method call receiver.
spec/lib/administrate/search_spec.rb
Outdated
allow(scoped_object).to receive(:where) | ||
|
||
expect(scoped_object).to receive(:joins).with(%i(role address)) | ||
.and_return(scoped_object) |
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.
Place the . on the previous line, together with the method call receiver.
spec/lib/administrate/search_spec.rb
Outdated
), | ||
address: Administrate::Field::HasOne.with_options( | ||
searchable: true, | ||
searchable_field: "street" |
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.
Put a comma after the last parameter of a multiline method call.
spec/lib/administrate/search_spec.rb
Outdated
ATTRIBUTE_TYPES = { | ||
role: Administrate::Field::BelongsTo.with_options( | ||
searchable: true, | ||
searchable_field: "name" |
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.
Put a comma after the last parameter of a multiline method call.
end | ||
|
||
class MockDashboardWithAssociation | ||
ATTRIBUTE_TYPES = { |
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.
Freeze mutable objects assigned to constants.
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.
Don't know why this still here, it's fixed
spec/lib/administrate/order_spec.rb
Outdated
double(columns_hash: { column.to_s => :column_info }) | ||
double( | ||
columns_hash: { column.to_s => :column_info }, | ||
table_name: "table_name" |
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.
Put a comma after the last parameter of a multiline method call.
lib/administrate/search.rb
Outdated
def column_to_query(attr) | ||
if association_search?(attr) | ||
ActiveRecord::Base.connection | ||
.quote_column_name(attribute_types[attr].searchable_field) |
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.
Place the . on the previous line, together with the method call receiver.
lib/administrate/search.rb
Outdated
ActiveRecord::Base.connection.quote_table_name(attr.to_s.pluralize) | ||
else | ||
ActiveRecord::Base.connection | ||
.quote_table_name(@scoped_resource.table_name) |
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.
Place the . on the previous line, together with the method call receiver.
63b56cd
to
6141d82
Compare
Up |
up |
Hi @jonatasrancan! Thank you for contributing this. This looks good to me! Would you be able to look at that final Hound comment? And add something to the docs about the new feature? |
@nickcharlton I think that last Hound comment, is a bug from github, I fixed that, but it continue to appear. |
6141d82
to
89cfaba
Compare
spec/lib/administrate/order_spec.rb
Outdated
@@ -171,6 +171,7 @@ def relation_with_column(column) | |||
double( | |||
klass: double(reflect_on_association: nil), | |||
columns_hash: { column.to_s => :column_info }, | |||
table_name: "table_name" |
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.
Put a comma after the last parameter of a multiline method call.
@@ -43,7 +43,7 @@ | |||
|
|||
scenario "user clicks through to the new page" do | |||
visit admin_log_entries_path | |||
click_on("New log entry") | |||
click_on t("administrate.actions.new_resource", name: 'log entries') |
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.
Prefer double-quoted strings unless you need single quotes to avoid extra backslashes for escaping.
lib/administrate/order.rb
Outdated
@@ -9,7 +9,7 @@ def apply(relation) | |||
return order_by_association(relation) unless | |||
reflect_association(relation).nil? | |||
|
|||
return relation.reorder("#{attribute} #{direction}") if | |||
return relation.reorder("#{relation.table_name}.#{attribute} #{direction}") if |
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.
Line is too long. [84/80]
89cfaba
to
d569835
Compare
lib/administrate/order.rb
Outdated
relation.columns_hash.keys.include?(attribute.to_s) | ||
order = "#{relation.table_name}.#{attribute} #{direction}" | ||
|
||
return relation.reorder(order) if relation.columns_hash.keys.include?(attribute.to_s) |
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.
Line is too long. [91/80]
lib/administrate/order.rb
Outdated
relation.columns_hash.keys.include?(attribute.to_s) | ||
order = "#{relation.table_name}.#{attribute} #{direction}" | ||
|
||
return relation.reorder(order) if relation.columns_hash.keys.include?(attribute.to_s) |
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.
Line is too long. [91/80]
d569835
to
e999cb4
Compare
@nickcharlton This is ready. |
Hi @jonatasrancan! #1054 is now merged. |
76d5087
to
0b57c02
Compare
@nickcharlton the PR is updated and all green 😄 |
d27bdb5
to
0ae7f5a
Compare
@nickcharlton It would be so nice to have this in next release :) |
Thanks for doing that! @jonatasrancan I had a quick go myself, but rebasing got a bit knarly. Would you be able to take another look so we can get this merged in? |
0ae7f5a
to
693b047
Compare
@nickcharlton all done 😃 |
693b047
to
1a47be0
Compare
Great, thanks! Merging away… |
After searching the documentation I didn't find anything about searching using the
association
fields, and also I didn't find any open PR related to this, so I decided to open this PR.The approach I used was using
with_options
, when declaring a field on the dashboard, and added a new option calledsearchable_field
.The dashboard will look like this: