From 7bf94bc6e6ce6558252252e68419e89a23213573 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Fri, 12 May 2017 16:53:00 +0200 Subject: VehicleJourneysController#collection: Integrate .departure_time_between Add our custom departure time range filter to the query. We pass the times sent from the frontend into the query in order to get the correct vehicle journeys back. Copied the `if params[:q]` check from `#ransack_periode_filter`, but added an additional condition on the existence of `:vehicle_journey_at_stops_departure_time_lteq` instead of blatantly assuming it's there just because its `..._gteq` counterpart is. Adding the filter necessitated some modifications to the `VehicleJourneys.departure_time_between` method. We needed to remove the `SELECT DISTINCT` and join parts of the query, reducing it to a simple "where" condition. These removals were due to the nature of the combination of this method with `VehicleJourneys.with_stops`. The `.with_stops` method already does a join on `vehicle_journey_at_stops`, and there's already a `SELECT DISTINCT` applied to the controller's query. Made the decision to limit this method's use to this specific use-case, and thus expect DISTINCT and the necessary join to already be defined on the `ActiveRecord` query object. We modify the test to implement this requirement. Finally, rename `.departure_time_between` to `.where_departure_time_between` to further indicate that this is a simple `where` condition, without the required extras. Refs #3370 --- app/controllers/vehicle_journeys_controller.rb | 10 ++++++++++ 1 file changed, 10 insertions(+) (limited to 'app/controllers/vehicle_journeys_controller.rb') diff --git a/app/controllers/vehicle_journeys_controller.rb b/app/controllers/vehicle_journeys_controller.rb index cca75af5b..c78613e99 100644 --- a/app/controllers/vehicle_journeys_controller.rb +++ b/app/controllers/vehicle_journeys_controller.rb @@ -78,6 +78,16 @@ class VehicleJourneysController < ChouetteController protected def collection scope = route.vehicle_journeys.with_stops + + if params[:q] && + params[:q][:vehicle_journey_at_stops_departure_time_gteq] && + params[:q][:vehicle_journey_at_stops_departure_time_lteq] + scope = scope.where_departure_time_between( + params[:q][:vehicle_journey_at_stops_departure_time_gteq], + params[:q][:vehicle_journey_at_stops_departure_time_lteq] + ) + end + @q = scope.search filtered_ransack_params # Fixme 3358 -- cgit v1.2.3