From 8aa258ab7bea85e423ed98b4d864ba7ab5481c13 Mon Sep 17 00:00:00 2001 From: Zog Date: Mon, 26 Feb 2018 12:07:22 +0100 Subject: Refs #6021 @1h; Allow journeys to span over 3 days And fix the way offsets are computed to use the timezones --- app/models/chouette/vehicle_journey_at_stop.rb | 23 ++++--- .../vehicle_journey_at_stops_day_offset.rb | 33 +++++----- .../vehicle_journey_at_stops_day_offset_spec.rb | 71 ++++++++++++++++++++++ 3 files changed, 101 insertions(+), 26 deletions(-) diff --git a/app/models/chouette/vehicle_journey_at_stop.rb b/app/models/chouette/vehicle_journey_at_stop.rb index d875442ee..9ea918ca5 100644 --- a/app/models/chouette/vehicle_journey_at_stop.rb +++ b/app/models/chouette/vehicle_journey_at_stop.rb @@ -4,7 +4,7 @@ module Chouette include Chouette::ForAlightingEnumerations include ChecksumSupport - DAY_OFFSET_MAX = 1 + DAY_OFFSET_MAX = 2 belongs_to :stop_point @@ -82,12 +82,12 @@ module Chouette format_time arrival_time.utc end - def departure_local_time - local_time departure_time + def departure_local_time offset=nil + local_time departure_time, offset end - def arrival_local_time - local_time arrival_time + def arrival_local_time offset=nil + local_time arrival_time, offset end def departure_local @@ -98,12 +98,15 @@ module Chouette format_time arrival_local_time end + def time_zone_offset + return 0 unless stop_point&.stop_area&.time_zone.present? + ActiveSupport::TimeZone[stop_point.stop_area.time_zone]&.utc_offset || 0 + end + private - def local_time time - return unless time - return time unless stop_point&.stop_area&.time_zone.present? - return time unless ActiveSupport::TimeZone[stop_point.stop_area.time_zone].present? - time + ActiveSupport::TimeZone[stop_point.stop_area.time_zone].utc_offset + def local_time time, offset=nil + return nil unless time + time + (offset || time_zone_offset) end def format_time time diff --git a/app/models/chouette/vehicle_journey_at_stops_day_offset.rb b/app/models/chouette/vehicle_journey_at_stops_day_offset.rb index 7497cd72c..cfa0e8bfc 100644 --- a/app/models/chouette/vehicle_journey_at_stops_day_offset.rb +++ b/app/models/chouette/vehicle_journey_at_stops_day_offset.rb @@ -4,31 +4,32 @@ module Chouette @at_stops = at_stops end - def calculate! - arrival_offset = 0 - departure_offset = 0 + def time_from_fake_date fake_date + fake_date - fake_date.to_date.to_time + end + def calculate! + offset = 0 + tz_offset = @at_stops.first&.time_zone_offset @at_stops.inject(nil) do |prior_stop, stop| next stop if prior_stop.nil? # we only compare time of the day, not actual times - stop_arrival_time = stop.arrival_time - stop.arrival_time.to_date.to_time - stop_departure_time = stop.departure_time - stop.departure_time.to_date.to_time - prior_stop_arrival_time = prior_stop.arrival_time - prior_stop.arrival_time.to_date.to_time - prior_stop_departure_time = prior_stop.departure_time - prior_stop.departure_time.to_date.to_time - - if stop_arrival_time < prior_stop_departure_time || - stop_arrival_time < prior_stop_arrival_time - arrival_offset += 1 + stop_arrival_time = time_from_fake_date stop.arrival_local_time(tz_offset) + stop_departure_time = time_from_fake_date stop.departure_local_time(tz_offset) + prior_stop_departure_time = time_from_fake_date prior_stop.departure_local_time(tz_offset) + + if stop_arrival_time < prior_stop_departure_time + offset += 1 end - if stop_departure_time < stop_arrival_time || - stop_departure_time < prior_stop_departure_time - departure_offset += 1 + stop.arrival_day_offset = offset + + if stop_departure_time < stop_arrival_time + offset += 1 end - stop.arrival_day_offset = arrival_offset - stop.departure_day_offset = departure_offset + stop.departure_day_offset = offset stop end diff --git a/spec/models/chouette/vehicle_journey_at_stops_day_offset_spec.rb b/spec/models/chouette/vehicle_journey_at_stops_day_offset_spec.rb index 69a2d5cb9..91cbf9097 100644 --- a/spec/models/chouette/vehicle_journey_at_stops_day_offset_spec.rb +++ b/spec/models/chouette/vehicle_journey_at_stops_day_offset_spec.rb @@ -86,5 +86,76 @@ describe Chouette::VehicleJourneyAtStop do expect(at_stops[3].arrival_day_offset).to eq(2) expect(at_stops[3].departure_day_offset).to eq(2) end + + context "with stops in a different timezone" do + before do + allow_any_instance_of(Chouette::VehicleJourneyAtStop).to receive(:local_time).and_wrap_original {|m, t| m.call(t - 12.hours)} + end + + it "should apply the TZ" do + at_stops = [] + [ + ['22:30', '22:35'], + ['01:02', '01:14'], + ['12:02', '12:14'], + ].each do |arrival_time, departure_time| + at_stops << build_stubbed( + :vehicle_journey_at_stop, + arrival_time: arrival_time, + departure_time: departure_time + ) + end + offsetter = Chouette::VehicleJourneyAtStopsDayOffset.new(at_stops) + + offsetter.calculate! + + expect(at_stops[0].arrival_day_offset).to eq(0) + expect(at_stops[0].departure_day_offset).to eq(0) + + expect(at_stops[1].arrival_day_offset).to eq(0) + expect(at_stops[1].departure_day_offset).to eq(0) + + expect(at_stops[2].arrival_day_offset).to eq(1) + expect(at_stops[2].departure_day_offset).to eq(1) + end + end + + context "with stops in different timezones" do + + it "should apply the TZ" do + at_stops = [] + + stop_area = create(:stop_area, time_zone: "Atlantic Time (Canada)") + stop_point = create(:stop_point, stop_area: stop_area) + vehicle_journey_at_stop = build_stubbed( + :vehicle_journey_at_stop, + stop_point: stop_point, + arrival_time: '09:00', + departure_time: '09:05' + ) + + at_stops << vehicle_journey_at_stop + + stop_area = create(:stop_area, time_zone: "Paris") + stop_point = create(:stop_point, stop_area: stop_area) + vehicle_journey_at_stop = build_stubbed( + :vehicle_journey_at_stop, + stop_point: stop_point, + arrival_time: '05:00', + departure_time: '05:05' + ) + at_stops << vehicle_journey_at_stop + + offsetter = Chouette::VehicleJourneyAtStopsDayOffset.new(at_stops) + + offsetter.calculate! + + expect(at_stops[0].arrival_day_offset).to eq(0) + expect(at_stops[0].departure_day_offset).to eq(0) + + expect(at_stops[1].arrival_day_offset).to eq(1) + expect(at_stops[1].departure_day_offset).to eq(1) + end + end end end -- cgit v1.2.3 From 18ae1f30c497f2ec7f4e9bcd2eb4966dad8e67ba Mon Sep 17 00:00:00 2001 From: Zog Date: Mon, 12 Mar 2018 15:28:56 +0100 Subject: Refs #6021: Make DAY_OFFSET_MAX configurable --- app/models/chouette/vehicle_journey_at_stop.rb | 8 +++++--- spec/models/chouette/vehicle_journey_at_stop_spec.rb | 6 +++--- 2 files changed, 8 insertions(+), 6 deletions(-) diff --git a/app/models/chouette/vehicle_journey_at_stop.rb b/app/models/chouette/vehicle_journey_at_stop.rb index 9ea918ca5..3f5bd5abf 100644 --- a/app/models/chouette/vehicle_journey_at_stop.rb +++ b/app/models/chouette/vehicle_journey_at_stop.rb @@ -6,6 +6,8 @@ module Chouette DAY_OFFSET_MAX = 2 + @@day_offset_max = DAY_OFFSET_MAX + mattr_accessor :day_offset_max belongs_to :stop_point belongs_to :vehicle_journey @@ -40,7 +42,7 @@ module Chouette I18n.t( 'vehicle_journey_at_stops.errors.day_offset_must_not_exceed_max', short_id: vehicle_journey&.get_objectid&.short_id, - max: DAY_OFFSET_MAX + 1 + max: Chouette::VehicleJourneyAtStop.day_offset_max + 1 ) ) end @@ -51,7 +53,7 @@ module Chouette I18n.t( 'vehicle_journey_at_stops.errors.day_offset_must_not_exceed_max', short_id: vehicle_journey&.get_objectid&.short_id, - max: DAY_OFFSET_MAX + 1 + max: Chouette::VehicleJourneyAtStop.day_offset_max + 1 ) ) end @@ -62,7 +64,7 @@ module Chouette # nil offsets. Handle these gracefully by forcing them to a 0 offset. offset ||= 0 - offset < 0 || offset > DAY_OFFSET_MAX + offset < 0 || offset > Chouette::VehicleJourneyAtStop.day_offset_max end def checksum_attributes diff --git a/spec/models/chouette/vehicle_journey_at_stop_spec.rb b/spec/models/chouette/vehicle_journey_at_stop_spec.rb index f79d19c88..ae9823243 100644 --- a/spec/models/chouette/vehicle_journey_at_stop_spec.rb +++ b/spec/models/chouette/vehicle_journey_at_stop_spec.rb @@ -27,13 +27,13 @@ RSpec.describe Chouette::VehicleJourneyAtStop, type: :model do it "disallows offsets greater than DAY_OFFSET_MAX" do expect(at_stop.day_offset_outside_range?( - Chouette::VehicleJourneyAtStop::DAY_OFFSET_MAX + 1 + Chouette::VehicleJourneyAtStop.day_offset_max + 1 )).to be true end it "allows offsets between 0 and DAY_OFFSET_MAX inclusive" do expect(at_stop.day_offset_outside_range?( - Chouette::VehicleJourneyAtStop::DAY_OFFSET_MAX + Chouette::VehicleJourneyAtStop.day_offset_max )).to be false end @@ -79,7 +79,7 @@ RSpec.describe Chouette::VehicleJourneyAtStop, type: :model do describe "#validate" do it "displays the proper error message when day offset exceeds the max" do - bad_offset = Chouette::VehicleJourneyAtStop::DAY_OFFSET_MAX + 1 + bad_offset = Chouette::VehicleJourneyAtStop.day_offset_max + 1 at_stop = build_stubbed( :vehicle_journey_at_stop, -- cgit v1.2.3 From 8f5fbe3f7fdc217be28fec416d5dbbbf4b551c7c Mon Sep 17 00:00:00 2001 From: Zog Date: Wed, 14 Mar 2018 09:44:20 +0100 Subject: Refs #6012; Update STIF initializer --- config/initializers/stif.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/config/initializers/stif.rb b/config/initializers/stif.rb index a73e4931b..2ddadbc7e 100644 --- a/config/initializers/stif.rb +++ b/config/initializers/stif.rb @@ -27,8 +27,6 @@ Rails.application.config.to_prepare do Organisation.before_validation(on: :create) do |organisation| organisation.custom_view = "stif" end -end - -Rails.application.config.to_prepare do Dashboard.default_class = Stif::Dashboard + Chouette::VehicleJourneyAtStop.day_offset_max = 1 end -- cgit v1.2.3