aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorTeddy Wing2017-05-12 16:53:00 +0200
committerTeddy Wing2017-05-12 17:56:27 +0200
commit7bf94bc6e6ce6558252252e68419e89a23213573 (patch)
tree9eb150d5cd41b8dbfad6b50b8610485c519a9d48
parent0898f39c35f2f853e051269ae0945eafa475d483 (diff)
downloadchouette-core-7bf94bc6e6ce6558252252e68419e89a23213573.tar.bz2
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
-rw-r--r--app/controllers/vehicle_journeys_controller.rb10
-rw-r--r--app/models/chouette/vehicle_journey.rb19
-rw-r--r--spec/models/chouette/vehicle_journey_spec.rb8
3 files changed, 28 insertions, 9 deletions
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
diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb
index 5d7ddafd7..0b4db3d9d 100644
--- a/app/models/chouette/vehicle_journey.rb
+++ b/app/models/chouette/vehicle_journey.rb
@@ -220,16 +220,19 @@ module Chouette
.order('"vehicle_journey_at_stops"."departure_time"')
end
+ # Requires a SELECT DISTINCT and a join with
+ # "vehicle_journey_at_stops".
+ #
# Example:
- # .departure_time_between('08:00', '09:45')
- def self.departure_time_between(start_time, end_time)
+ # .select('DISTINCT "vehicle_journeys".*')
+ # .joins('
+ # LEFT JOIN "vehicle_journey_at_stops"
+ # ON "vehicle_journey_at_stops"."vehicle_journey_id" =
+ # "vehicle_journeys"."id"
+ # ')
+ # .where_departure_time_between('08:00', '09:45')
+ def self.where_departure_time_between(start_time, end_time)
self
- .select('DISTINCT "vehicle_journeys".*')
- .joins('
- LEFT JOIN "vehicle_journey_at_stops"
- ON "vehicle_journey_at_stops"."vehicle_journey_id" =
- "vehicle_journeys"."id"
- ')
.where(
%Q(
"vehicle_journey_at_stops"."departure_time" >= ?
diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb
index 231cbfaaf..caa4af203 100644
--- a/spec/models/chouette/vehicle_journey_spec.rb
+++ b/spec/models/chouette/vehicle_journey_spec.rb
@@ -336,7 +336,13 @@ describe Chouette::VehicleJourney, :type => :model do
expect(route
.vehicle_journeys
- .departure_time_between('02:30', '03:30')
+ .select('DISTINCT "vehicle_journeys".*')
+ .joins('
+ LEFT JOIN "vehicle_journey_at_stops"
+ ON "vehicle_journey_at_stops"."vehicle_journey_id" =
+ "vehicle_journeys"."id"
+ ')
+ .where_departure_time_between('02:30', '03:30')
.to_a
).to eq([journey_middle])
end