-
Notifications
You must be signed in to change notification settings - Fork 0
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
Remove Ransack from collection model; use new component instead for table header links. #2744
base: master
Are you sure you want to change the base?
Conversation
@@ -30,9 +26,23 @@ def index | |||
@department = params[:department] | |||
end | |||
|
|||
scope = scope.order(Arel.sql("#{params[:sort_field]} #{params[:sort_order]}")) |
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.
What if the link_maker
handles this, so the knowledge about what the param keys are is in ONE place?
scope = link_maker.add_order(scope)
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.
what do you think, nah?
I think this is a great approach, good work! Some comments! |
slight change to how we handle sort order
else | ||
# For columns OTHER than the one the table is currently sorted by, | ||
# clicking the title of the column will sort the table by that column, | ||
# in an arbitrary order. |
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 think I understand, but what do you mean by "arbitrary"? WE only have two kinds, asc and desc, don't we know for sure which one we are doing? With "asc" being the default?
# Also sets default sort column and order. | ||
# This does not actually perform any sorting or filtering. | ||
def index_params | ||
@index_params ||= Kithe::Parameters.new(params).permit( |
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.
You don't need to use Kithe::Parameters here, let's just use ordinary rails strong params. https://api.rubyonrails.org/classes/ActionController/StrongParameters.html
Which means this can just be params.permit...
restore ransack behavior of which order when changing sort field
Ref #2450
Background
Ransack did two things for us:
This PR separates these two tasks. The first is done in the controller; the second, inside a new component class.
More specifically:
1) Creating the list of collections to show:
Searching, filtering, sorting and pagination happen entirely in the
#index
method, using sane and legible code.2) Table header links:
index_params
method, which returns a permitted strongparams object.:link_maker
.:link_maker
is instantiated in abefore_action
call, so it doesn't crowd the#index
method.:link_maker
stores and encapsulates the current state of the sorted table.:link_maker
is then used to generate severalSortedTableHeaderLinkComponent
objects, one per sortable column, which are then rendered to display the actual links.Tradeoffs
Since we intend to use this component in at least three (and likely more) admin controllers, this PR aims to simplify the model, controller and view, at the expense of slightly complicating the component.
Note: The
#index
method was not using permitted params, so we created anindex_params
method to return sanitized params that#index
can use. These are then handed directly to the component code, along with keys that tell the component which keys important for what it's trying to do.Note: Ransack search is currently broken in dev on the master branch.
This is one of the many reasons we want to get rid of it, but it's easy to forget about.
(Try going to
http://localhost:3000/admin/works
in dev onmaster
and searching forGoat
. You get anArgumentError
inAdmin::WorksController#index
with error messageInvalid search term q
unless you comment out
c.ignore_unknown_conditions = Rails.env.production?
in
ransack.rb
.What this means
If you are testing this PR in dev, and note that the search is broken on the admin works page, that's not because this PR broke it. It's because it is broken in
master
and this PR does not modify the admin works page.