diff options
| -rw-r--r-- | app/models/vehicle_translation.rb | 54 | ||||
| -rw-r--r-- | app/views/vehicle_translations/_translate_form.html.erb | 8 | ||||
| -rw-r--r-- | config/locales/vehicle_translations.yml | 12 | ||||
| -rw-r--r-- | spec/models/vehicle_translation_spec.rb | 32 | 
4 files changed, 79 insertions, 27 deletions
diff --git a/app/models/vehicle_translation.rb b/app/models/vehicle_translation.rb index 0457da0fa..639981d63 100644 --- a/app/models/vehicle_translation.rb +++ b/app/models/vehicle_translation.rb @@ -4,12 +4,14 @@ class VehicleTranslation    extend ActiveModel::Naming    attr_accessor :vehicle_journey_id, :count, :duration -  attr_accessor :first_stop_arrival_time, :first_stop_departure_time +  attr_accessor :first_stop_time, :departure_or_arrival +  attr_accessor :delta -  validates_presence_of :count, :duration +  validates_presence_of :count, :duration, :first_stop_time, :departure_or_arrival +  validates_inclusion_of :departure_or_arrival, :in =>  %w( departure arrival)    validates_numericality_of :count, greater_than: 0    validates_numericality_of :duration, greater_than: 0 -  validate :starting_time_provided +  validate :firts_stop_time_format    validate :vehicle_has_stop_times    def initialize(attributes = {}) @@ -18,15 +20,17 @@ class VehicleTranslation      end    end -  def starting_time_provided -    if ( first_stop_arrival_time.blank? && first_stop_departure_time.blank?) -      errors.add :first_stop_arrival_time, I18n.t('activemodel.errors.models.vehicle_translation.missing_start_time') -      errors.add :first_stop_departure_time, I18n.t('activemodel.errors.models.vehicle_translation.missing_start_time') -      #errors.add( :first_stop_departure_time => "un horaire de départ ou d'arrivée au premier arrêt doit être renseigné") -    elsif first_stop_departure_time.blank? -      errors.add :first_stop_arrival_time, I18n.t('activemodel.errors.models.vehicle_translation.unreadable_time') unless Time.parse( self.first_stop_arrival_time) rescue false -    elsif first_stop_arrival_time.blank? -      errors.add :first_stop_departure_time, I18n.t('activemodel.errors.models.vehicle_translation.unreadable_time') unless Time.parse( self.first_stop_departure_time) rescue false +  def firts_stop_time_format +    if first_stop_time.blank? +      errors.add :first_stop_time, I18n.t('activemodel.errors.models.vehicle_translation.unreadable_time') unless self.class.time_format_text?( self.first_stop_time) +    end +  end + +  def self.time_format_text?( text) +    begin +      Time.parse text +    rescue +      false      end    end @@ -40,23 +44,43 @@ class VehicleTranslation      false    end +  def move_time_in_current_time_zone( time ) +    Time.parse( "#{time.hour}:#{time.min}") +  end +  def evaluate_delta( actual_time) +    Time.parse( first_stop_time) - move_time_in_current_time_zone( actual_time) +  end +  def vjas_time_attribute +    "#{departure_or_arrival}_time" +  end +  def first_vjas_time +    vehicle_journey.vehicle_journey_at_stops.first.send( vjas_time_attribute) +  end +  def first_delta +    evaluate_delta( first_vjas_time) +  end +    def translate      vehicle = vehicle_journey      copied_attributes = vehicle_journey.attributes      copied_attributes.delete( "id")      copied_attributes.delete( "objectid") +    # time shift for current duplicated vehicle +    delta = first_delta +      1.upto( count.to_i) do |index|        translated = Chouette::VehicleJourney.create( copied_attributes)        translated.time_tables = vehicle.time_tables +        vehicle.vehicle_journey_at_stops.each do |vjas|          vjas_attributes = vjas.attributes.merge( "vehicle_journey_id" => translated.id) -        vjas_attributes.merge! "departure_time" => ( vjas_attributes[ "departure_time"] + (index * duration.to_i.minutes) ), -          "arrival_time" => ( vjas_attributes[ "arrival_time"] + (index * duration.to_i.minutes) ) +        vjas_attributes.merge! "departure_time" => ( vjas_attributes[ "departure_time"] + delta), +                               "arrival_time" => ( vjas_attributes[ "arrival_time"] + delta)          Chouette::VehicleJourneyAtStop.create( vjas_attributes)        end - +      delta += duration.to_i.minutes      end    end diff --git a/app/views/vehicle_translations/_translate_form.html.erb b/app/views/vehicle_translations/_translate_form.html.erb index ea7e15bf0..8759d89f9 100644 --- a/app/views/vehicle_translations/_translate_form.html.erb +++ b/app/views/vehicle_translations/_translate_form.html.erb @@ -3,11 +3,9 @@    <%= semantic_form_for [@referential, @line, @route, @vehicle_journey, @vehicle_translation], remote: true do |form| %>    <div class="modal-body">      <%= form.inputs do %> -      <%= form.input :first_stop_arrival_time, -        as: :time_picker, label: t( ".first_stop_arrival_time", stop_name: @vehicle_translation.first_stop_name), -        input_html: { class: "form-control input-sm timepicker_basic"}, wrapper_html: { class: "input-append bootstrap-timepicker" } %> -      <%= form.input :first_stop_departure_time, -        as: :time_picker, label: t( ".first_stop_departure_time", stop_name: @vehicle_translation.first_stop_name), +      <%= form.input :departure_or_arrival, as: :select, label: t('.set'), collection: [ [ t('.first_stop_departure_time', stop_name: @vehicle_translation.first_stop_name), "departure"], [t('.first_stop_arrival_time', stop_name: @vehicle_translation.first_stop_name), "arrival"] ], include_blank: false, required: true %> +      <%= form.input :first_stop_time, +        as: :time_picker, label: t( ".to"),          input_html: { class: "form-control input-sm timepicker_basic"}, wrapper_html: { class: "input-append bootstrap-timepicker" } %>        <div class="panel-group" id="accordion">        <div class="panel panel-default"> diff --git a/config/locales/vehicle_translations.yml b/config/locales/vehicle_translations.yml index 9c413f2b3..58dc5c6b8 100644 --- a/config/locales/vehicle_translations.yml +++ b/config/locales/vehicle_translations.yml @@ -2,9 +2,11 @@ en:    vehicle_translations:      success: "%{count} vehicle journeys created by translation"      failure: "Fail when creating vehicle journeys by translation" -    first_stop_arrival_time: "Arrival time at first stop (%{stop_name})" -    first_stop_departure_time: "Arrival time at first stop (%{stop_name})"      translate_form: +      set: "Set" +      to: "at (hh:mm)" +      first_stop_arrival_time: "Arrival time at first stop '%{stop_name}'" +      first_stop_departure_time: "Arrival time at first stop '%{stop_name}'"        multiple_cloning_form: "Repeat cloning based on a time interval"    activemodel:      attributes: @@ -21,9 +23,11 @@ fr:    vehicle_translations:      success: "%{count} course(s) crée(s) par translation"      failure: "Echec de la création de courses par tanslation" -    first_stop_arrival_time: "Horaire d'arrivée au premier arrêt (%{stop_name})" -    first_stop_departure_time: "Horaire de départ au premier arrêt (%{stop_name})"      translate_form: +      set: "Fixer" +      to: "à (hh:mm)" +      first_stop_arrival_time: "Horaire d'arrivée au premier arrêt '%{stop_name}'" +      first_stop_departure_time: "Horaire de départ au premier arrêt '%{stop_name}'"        multiple_cloning_form: "Répéter le clonage à intervalle régulier"    activemodel: diff --git a/spec/models/vehicle_translation_spec.rb b/spec/models/vehicle_translation_spec.rb index 47be9ff3a..bc16f2260 100644 --- a/spec/models/vehicle_translation_spec.rb +++ b/spec/models/vehicle_translation_spec.rb @@ -13,7 +13,32 @@ describe VehicleTranslation do                                    )}    subject {Factory.build(:vehicle_translation,                           :vehicle_journey_id => vehicle_journey.id, -                         :first_stop_departure_time => "12:00")} +                         :first_stop_time => "12:00", +                         :departure_or_arrival => "departure", +                         :duration => 9, +                         :count => 2)} + +  describe "#first_stop_time=" do +    context "when setting invalid value" do +      it "should have an error on first_stop_departure_time" do +        subject.first_stop_time = "dummy" +        subject.valid? +        subject.errors[ :first_stop_time].should_not be_nil +      end +    end + +  end +  describe "#evaluate_delta" do +    it "should return 3600 seconds" do +      subject.evaluate_delta( Time.parse( "11:00")).should == 3600.0 +    end +  end +  describe "#first_delta" do +    it "should return 3600 seconds" do +      subject.should_receive( :first_vjas_time).and_return( Time.parse( "11:00")) +      subject.first_delta +    end +  end    describe "#translate" do      it "should add new vehicle" do @@ -51,10 +76,11 @@ describe VehicleTranslation do      end      it "should add vehicle where vehicle_journey_at_stops are translated with #duration" do        read_vehicle = Chouette::VehicleJourney.find(vehicle_journey.id)  # read from bd, change time values +      delta = subject.first_delta        subject.translate        last_created_vehicle.vehicle_journey_at_stops.each_with_index do |vjas, index| -        vjas.departure_time.should == (read_vehicle.vehicle_journey_at_stops[index].departure_time + subject.duration.minutes) -        vjas.arrival_time.should == (read_vehicle.vehicle_journey_at_stops[index].arrival_time + subject.duration.minutes) +        vjas.departure_time.should == (read_vehicle.vehicle_journey_at_stops[index].departure_time + delta + subject.duration.minutes) +        vjas.arrival_time.should == (read_vehicle.vehicle_journey_at_stops[index].arrival_time + delta + subject.duration.minutes)        end      end    end  | 
