diff options
| author | Teddy Wing | 2017-05-23 15:21:32 +0200 |
|---|---|---|
| committer | Robert | 2017-05-29 08:49:52 +0200 |
| commit | 95ebdc82c85acdf7cae208294a750e40e9e418c2 (patch) | |
| tree | 5280c2669134c4bc44432f5baada1bf3fe3c546f | |
| parent | 9aaa337d0b5e44736834f98f9aca8b4f31c63afc (diff) | |
| download | chouette-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
4 files changed, 46 insertions, 21 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 diff --git a/spec/models/chouette/vehicle_journey_at_stops_are_in_increasing_order_validator_spec.rb b/spec/models/chouette/vehicle_journey_at_stops_are_in_increasing_order_validator_spec.rb new file mode 100644 index 000000000..6c5541edd --- /dev/null +++ b/spec/models/chouette/vehicle_journey_at_stops_are_in_increasing_order_validator_spec.rb @@ -0,0 +1,25 @@ +require 'spec_helper' + +describe Chouette::VehicleJourneyAtStopsAreInIncreasingOrderValidator do + subject { create(:vehicle_journey_odd) } + + describe "#increasing_times" do + before(:each) do + subject.vehicle_journey_at_stops[0].departure_time = + subject.vehicle_journey_at_stops[1].departure_time - 5.hour + subject.vehicle_journey_at_stops[0].arrival_time = + subject.vehicle_journey_at_stops[0].departure_time + subject.vehicle_journey_at_stops[1].arrival_time = + subject.vehicle_journey_at_stops[1].departure_time + end + + it "should make instance invalid" do + subject.validate + + expect( + subject.vehicle_journey_at_stops[1].errors[:departure_time] + ).not_to be_blank + expect(subject).not_to be_valid + end + end +end diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index ed76c278c..4a108d7c0 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -466,18 +466,6 @@ describe Chouette::VehicleJourney, :type => :model do end context "when following departure times exceeds gap" do - describe "#increasing_times" do - before(:each) do - subject.vehicle_journey_at_stops[0].departure_time = subject.vehicle_journey_at_stops[1].departure_time - 5.hour - subject.vehicle_journey_at_stops[0].arrival_time = subject.vehicle_journey_at_stops[0].departure_time - subject.vehicle_journey_at_stops[1].arrival_time = subject.vehicle_journey_at_stops[1].departure_time - end - it "should make instance invalid" do - subject.increasing_times - expect(subject.vehicle_journey_at_stops[1].errors[:departure_time]).not_to be_blank - expect(subject).not_to be_valid - end - end describe "#update_attributes" do let!(:params){ {"vehicle_journey_at_stops_attributes" => { "0"=>{"id" => subject.vehicle_journey_at_stops[0].id ,"arrival_time" => 1.minutes.ago,"departure_time" => 1.minutes.ago}, |
