RoutingConstraintZones#index: Fix sort in table
Created by: teddywing
Sorting by stop point count and route name wasn't working. Only sorting by name worked.
This was because these HTML table columns get their data from methods on
the RoutingConstraintZone model (#stop_points_count and
#route_name). Those methods can't be sorted on in the database.
Furthermore, when setting up the order ActiveRecord call, only database
column names on routing_constraint_zones were taken into account. When
a sort parameter specified an identifier that wasn't a column name,
the name column would be used to sort the list.
In this change, we enable sorting by stop point count and by route name. The SQL work for ordering comes from a couple of new scope methods defined on the model. Xin explained that he had previously used scope methods to enable exotic sorting of Referentials on the Workbenches#show page. I couldn't think of a better way to deal with these, and it seems like a reasonable thing to do here, so I went with the same setup.
In #sort_column, tack on our custom sorting keys to the list of
accepted keys. This borrows the pattern used in other #sort_column
methods in other controllers. Ended up using "route" as the route name
sorting key because that's what gets passed from the frontend.
Eliminated the if condition in #collection that defaults to name
sorting because it was redundant. The #sort_column method already
defaults to sorting by name if no params[:sort] is passed in. This
makes the method a lot cleaner, nice.
Add a new private method #sort_collection that determines what method
to call to sort the query. When using real table column names, it calls
#order like before. But now, if the user asked to sort by stop point
count or route name, we delegate instead to our scope methods,
#order_by_stop_points_count and #order_by_route_name.
Refs #4130