aboutsummaryrefslogtreecommitdiffstats
path: root/app
diff options
context:
space:
mode:
authorTeddy Wing2017-05-23 15:21:32 +0200
committerRobert2017-05-29 08:49:52 +0200
commit95ebdc82c85acdf7cae208294a750e40e9e418c2 (patch)
tree5280c2669134c4bc44432f5baada1bf3fe3c546f /app
parent9aaa337d0b5e44736834f98f9aca8b4f31c63afc (diff)
downloadchouette-core-95ebdc82c85acdf7cae208294a750e40e9e418c2.tar.bz2
VehicleJourney: Move `#increasing_times` validator to a new class
Create a new validator class for this validation. By splitting the increasing time validation into a separate group, we can unify the functionality that's currently split between `VehicleJourney` and `VehicleJourneyAtStop`. This will allow us to go to a single place to look for the validation. Move the `#increasing_times` method into our new class with minor modification to make it work. Move the test for the method into a new spec file. Refs #870
Diffstat (limited to 'app')
-rw-r--r--app/models/chouette/vehicle_journey.rb11
-rw-r--r--app/models/chouette/vehicle_journey_at_stops_are_in_increasing_order_validator.rb19
2 files changed, 21 insertions, 9 deletions
diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb
index d20a3d315..20213213f 100644
--- a/app/models/chouette/vehicle_journey.rb
+++ b/app/models/chouette/vehicle_journey.rb
@@ -28,7 +28,8 @@ module Chouette
has_and_belongs_to_many :time_tables, :class_name => 'Chouette::TimeTable', :foreign_key => "vehicle_journey_id", :association_foreign_key => "time_table_id"
has_many :stop_points, -> { order("stop_points.position") }, :through => :vehicle_journey_at_stops
- validate :increasing_times
+ validates :vehicle_journey_at_stops,
+ vehicle_journey_at_stops_are_in_increasing_order: true
validates_presence_of :number
before_validation :set_default_values
@@ -156,14 +157,6 @@ module Chouette
attrs
end
- def increasing_times
- previous = nil
- vehicle_journey_at_stops.select{|vjas| vjas.departure_time && vjas.arrival_time}.each do |vjas|
- errors.add( :vehicle_journey_at_stops, 'time gap overflow') unless vjas.increasing_times_validate( previous)
- previous = vjas
- end
- end
-
def missing_stops_in_relation_to_a_journey_pattern(selected_journey_pattern)
selected_journey_pattern.stop_points - self.stop_points
end
diff --git a/app/models/chouette/vehicle_journey_at_stops_are_in_increasing_order_validator.rb b/app/models/chouette/vehicle_journey_at_stops_are_in_increasing_order_validator.rb
new file mode 100644
index 000000000..ab454725a
--- /dev/null
+++ b/app/models/chouette/vehicle_journey_at_stops_are_in_increasing_order_validator.rb
@@ -0,0 +1,19 @@
+module Chouette
+ # TODO: Rename to IncreasingTimeOrder
+ class VehicleJourneyAtStopsAreInIncreasingOrderValidator <
+ ActiveModel::EachValidator
+ def validate_each(record, attribute, value)
+ increasing_times(record)
+ end
+
+ def increasing_times(vehicle_journey)
+ # TODO: Rename `previous`
+ previous = nil
+ # TODO: Move `select` to a new named method
+ vehicle_journey.vehicle_journey_at_stops.select{|vjas| vjas.departure_time && vjas.arrival_time}.each do |vjas|
+ vehicle_journey.errors.add( :vehicle_journey_at_stops, 'time gap overflow') unless vjas.increasing_times_validate( previous)
+ previous = vjas
+ end
+ end
+ end
+end