diff options
| author | Alban Peignier | 2018-03-14 15:36:22 +0100 |
|---|---|---|
| committer | GitHub | 2018-03-14 15:36:22 +0100 |
| commit | e67f5db85a228a930f7d33b5c495b2ee80ed93bd (patch) | |
| tree | d7196a61df6224b30e1a175345287f9a5d887c90 | |
| parent | 8abcd5a71a29f987c20f513f622ade7eced82176 (diff) | |
| parent | 8f5fbe3f7fdc217be28fec416d5dbbbf4b551c7c (diff) | |
| download | chouette-core-e67f5db85a228a930f7d33b5c495b2ee80ed93bd.tar.bz2 | |
Merge pull request #342 from af83/6021-handlke-offsets-in-journeys
Allow journeys to span over 3 days. Refs #6021
| -rw-r--r-- | app/models/chouette/vehicle_journey_at_stop.rb | 31 | ||||
| -rw-r--r-- | app/models/chouette/vehicle_journey_at_stops_day_offset.rb | 33 | ||||
| -rw-r--r-- | config/initializers/stif.rb | 4 | ||||
| -rw-r--r-- | spec/models/chouette/vehicle_journey_at_stop_spec.rb | 6 | ||||
| -rw-r--r-- | spec/models/chouette/vehicle_journey_at_stops_day_offset_spec.rb | 71 |
5 files changed, 110 insertions, 35 deletions
diff --git a/app/models/chouette/vehicle_journey_at_stop.rb b/app/models/chouette/vehicle_journey_at_stop.rb index d875442ee..3f5bd5abf 100644 --- a/app/models/chouette/vehicle_journey_at_stop.rb +++ b/app/models/chouette/vehicle_journey_at_stop.rb @@ -4,8 +4,10 @@ module Chouette include Chouette::ForAlightingEnumerations include ChecksumSupport - DAY_OFFSET_MAX = 1 + 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 @@ -82,12 +84,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 +100,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/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 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, 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 |
