diff options
| author | Teddy Wing | 2017-07-17 18:19:23 +0200 |
|---|---|---|
| committer | Teddy Wing | 2017-07-21 15:59:53 +0200 |
| commit | 0166ef93691416b687091c4ff0aa6fb308a449e2 (patch) | |
| tree | a020939742335e0df4809afd1aa5fedfba0d0d23 | |
| parent | 161f3184783ba3419238d3ce6b2291ef9fabfa01 (diff) | |
| download | chouette-core-0166ef93691416b687091c4ff0aa6fb308a449e2.tar.bz2 | |
VehicleJourneyAtStop: Add validation messages to day offset validator
Since we can't use the lambda syntax when passing a message to a
validator, write a custom validation method that allows us to be more
flexible when setting a message. The correct message with the correct
data is now used for the offset error.
This eliminates our validation of `numberiality`, making our two
validation specs no longer valid.
Refs #3597
| -rw-r--r-- | app/models/chouette/vehicle_journey_at_stop.rb | 42 | ||||
| -rw-r--r-- | spec/models/chouette/vehicle_journey_at_stop_spec.rb | 13 |
2 files changed, 28 insertions, 27 deletions
diff --git a/app/models/chouette/vehicle_journey_at_stop.rb b/app/models/chouette/vehicle_journey_at_stop.rb index f58773d18..6e719fc61 100644 --- a/app/models/chouette/vehicle_journey_at_stop.rb +++ b/app/models/chouette/vehicle_journey_at_stop.rb @@ -26,34 +26,34 @@ module Chouette end end - validates :arrival_day_offset, numericality: { - greater_than_or_equal_to: 0, - less_than_or_equal_to: DAY_OFFSET_MAX, - message: ->(object, data) do - byebug - I18n.t( - 'vehicle_journey_at_stops.errors.day_offset_must_not_exceed_max', - local_id: object.vehicle_journey.objectid.local_id, - max: DAY_OFFSET_MAX + 1 - ) + validate :day_offset_must_be_within_range + + after_initialize :set_virtual_attributes + def set_virtual_attributes + @_destroy = false + @dummy = false + end + + def day_offset_must_be_within_range + def outside_range(offset) + offset < 0 || offset > DAY_OFFSET_MAX end - } - validates :departure_day_offset, numericality: { - greater_than_or_equal_to: 0, - less_than_or_equal_to: DAY_OFFSET_MAX, - message: ->(object, data) do + + def error_message I18n.t( 'vehicle_journey_at_stops.errors.day_offset_must_not_exceed_max', - local_id: object.vehicle_journey.objectid.local_id, + local_id: vehicle_journey.objectid.local_id, max: DAY_OFFSET_MAX + 1 ) end - } - after_initialize :set_virtual_attributes - def set_virtual_attributes - @_destroy = false - @dummy = false + if outside_range(arrival_day_offset) + errors.add(:arrival_day_offset, error_message) + end + + if outside_range(departure_day_offset) + errors.add(:departure_day_offset, error_message) + end end end diff --git a/spec/models/chouette/vehicle_journey_at_stop_spec.rb b/spec/models/chouette/vehicle_journey_at_stop_spec.rb index 82b0783a6..23f33fc8f 100644 --- a/spec/models/chouette/vehicle_journey_at_stop_spec.rb +++ b/spec/models/chouette/vehicle_journey_at_stop_spec.rb @@ -22,15 +22,16 @@ RSpec.describe Chouette::VehicleJourneyAtStop, type: :model do arrival_day_offset: bad_offset, departure_day_offset: bad_offset ) + error_message = I18n.t( + 'vehicle_journey_at_stops.errors.day_offset_must_not_exceed_max', + local_id: at_stop.vehicle_journey.objectid.local_id, + max: bad_offset + ) at_stop.validate - expect(at_stop.errors[:arrival_day_offset]).to include( - I18n.t('vehicle_journey_at_stops.errors.day_offset_must_not_exceed_max') - ) - expect(at_stop.errors[:departure_day_offset]).to include( - I18n.t('vehicle_journey_at_stops.errors.day_offset_must_not_exceed_max') - ) + expect(at_stop.errors[:arrival_day_offset]).to include(error_message) + expect(at_stop.errors[:departure_day_offset]).to include(error_message) end end end |
