diff options
| -rw-r--r-- | app/controllers/vehicle_journeys_controller.rb | 85 | ||||
| -rw-r--r-- | app/javascript/routes/form_helper.js | 17 | ||||
| -rw-r--r-- | app/javascript/routes/reducers/stopPoints.js | 10 | ||||
| -rw-r--r-- | app/models/chouette/vehicle_journey.rb | 8 | ||||
| -rw-r--r-- | app/views/vehicle_journeys/show.rabl | 6 | ||||
| -rw-r--r-- | spec/javascript/routes/reducers/stop_points_spec.js | 588 | 
6 files changed, 211 insertions, 503 deletions
| diff --git a/app/controllers/vehicle_journeys_controller.rb b/app/controllers/vehicle_journeys_controller.rb index c941aeae4..454f9c3dd 100644 --- a/app/controllers/vehicle_journeys_controller.rb +++ b/app/controllers/vehicle_journeys_controller.rb @@ -40,43 +40,46 @@ class VehicleJourneysController < ChouetteController    end    def index -    @stop_points_list = [] -    route.stop_points.each do |sp| -      @stop_points_list << { -        :id => sp.stop_area.id, -        :route_id => sp.try(:route_id), -        :object_id => sp.try(:objectid), -        :position => sp.try(:position), -        :for_boarding => sp.try(:for_boarding), -        :for_alighting => sp.try(:for_alighting), -        :name => sp.stop_area.try(:name), -        :zip_code => sp.stop_area.try(:zip_code), -        :city_name => sp.stop_area.try(:city_name), -        :comment => sp.stop_area.try(:comment), -        :area_type => sp.stop_area.try(:area_type), -        :registration_number => sp.stop_area.try(:registration_number), -        :nearest_topic_name => sp.stop_area.try(:nearest_topic_name), -        :fare_code => sp.stop_area.try(:fare_code), -        :longitude => sp.stop_area.try(:longitude), -        :latitude => sp.stop_area.try(:latitude), -        :long_lat_type => sp.stop_area.try(:long_lat_type), -        :country_code => sp.stop_area.try(:country_code), -        :street_name => sp.stop_area.try(:street_name) -      } -    end - -    @transport_mode = route.line['transport_mode'] -    @transport_submode = route.line['transport_submode'] - -    if params[:jp] -      @jp_origin  = Chouette::JourneyPattern.find_by(objectid: params[:jp]) -      @jp_origin_stop_points = @jp_origin.stop_points -    end - -    index! do +    index! do |format|        if collection.out_of_bounds?          redirect_to params.merge(:page => 1)        end +      format.json do +        @vehicle_journeys = @vehicle_journeys.includes({stop_points: :stop_area}) +      end +      format.html do +        @stop_points_list = [] +        @stop_points_list = route.stop_points.joins(:stop_area).map do |sp| +          { +            :id => sp.stop_area.id, +            :route_id => sp.try(:route_id), +            :object_id => sp.try(:objectid), +            :position => sp.try(:position), +            :for_boarding => sp.try(:for_boarding), +            :for_alighting => sp.try(:for_alighting), +            :name => sp.stop_area.try(:name), +            :zip_code => sp.stop_area.try(:zip_code), +            :city_name => sp.stop_area.try(:city_name), +            :comment => sp.stop_area.try(:comment), +            :area_type => sp.stop_area.try(:area_type), +            :registration_number => sp.stop_area.try(:registration_number), +            :nearest_topic_name => sp.stop_area.try(:nearest_topic_name), +            :fare_code => sp.stop_area.try(:fare_code), +            :longitude => sp.stop_area.try(:longitude), +            :latitude => sp.stop_area.try(:latitude), +            :long_lat_type => sp.stop_area.try(:long_lat_type), +            :country_code => sp.stop_area.try(:country_code), +            :street_name => sp.stop_area.try(:street_name) +          } +        end +        @transport_mode = route.line['transport_mode'] +        @transport_submode = route.line['transport_submode'] + +        if params[:jp] +          @jp_origin  = Chouette::JourneyPattern.find_by(objectid: params[:jp]) +          @jp_origin_stop_points = @jp_origin.stop_points +        end +      end      end    end @@ -92,13 +95,15 @@ class VehicleJourneysController < ChouetteController      scope = maybe_filter_by_departure_time(scope)      scope = maybe_filter_out_journeys_with_time_tables(scope) -    @q = scope.search filtered_ransack_params +    @vehicle_journeys ||= begin +      @q = scope.search filtered_ransack_params -    @ppage = 20 -    @vehicle_journeys = @q.result.paginate(:page => params[:page], :per_page => @ppage) -    @footnotes = route.line.footnotes.to_json -    @matrix    = resource_class.matrix(@vehicle_journeys) -    @vehicle_journeys +      @ppage = 20 +      @vehicle_journeys = @q.result.paginate(:page => params[:page], :per_page => @ppage) +      @footnotes = route.line.footnotes.to_json +      @matrix    = resource_class.matrix(@vehicle_journeys) +      @vehicle_journeys +    end    end    def maybe_filter_by_departure_time(scope) diff --git a/app/javascript/routes/form_helper.js b/app/javascript/routes/form_helper.js index 8a3277234..865722fb6 100644 --- a/app/javascript/routes/form_helper.js +++ b/app/javascript/routes/form_helper.js @@ -7,14 +7,14 @@ const formHelper = {      input.setAttribute('name', formatedName)      input.setAttribute('value', value)      form.appendChild(input) -  },  +  },    addError: (ids) => {      ids.forEach((id) => {        if (!$(id).parents('.form-group').hasClass('has-error')) {          $(id).parents('.form-group').addClass('has-error')          $(id).parent().append(`<span class='help-block small'>${'doit être rempli(e)'}</span>`)        } -    })  +    })    },    cleanInputs: (ids) => {      ids.forEach((id) =>{ @@ -28,21 +28,22 @@ const formHelper = {      ids.forEach(id => {        $(id).val() == "" ? blankInputs.push(id) : filledInputs.push(id)      }) -     +      if (filledInputs.length > 0) formHelper.cleanInputs(filledInputs) -    if (blankInputs.length > 0) formHelper.addError(blankInputs)   +    if (blankInputs.length > 0) formHelper.addError(blankInputs)    },    handleStopPoints: (event, state) => {      if (state.stopPoints.length >= 2) {        state.stopPoints.map((stopPoint, i) => {          formHelper.addInput('id', stopPoint.stoppoint_id ? stopPoint.stoppoint_id : '', i)          formHelper.addInput('stop_area_id', stopPoint.stoparea_id, i) -        formHelper.addInput('position', i, i) +        formHelper.addInput('position', stopPoint.index, i)          formHelper.addInput('for_boarding', stopPoint.for_boarding, i)          formHelper.addInput('for_alighting', stopPoint.for_alighting, i)        }) -    if ($('.alert.alert-danger').length > 0) $('.alert.alert-danger').remove() -    } else { +      if ($('.alert.alert-danger').length > 0) $('.alert.alert-danger').remove() +    } +    else {        event.preventDefault()        let msg = "L'itinéraire doit comporter au moins deux arrêts"        if ($('.alert.alert-danger').length == 0) { @@ -52,4 +53,4 @@ const formHelper = {    }  } -export default formHelper
\ No newline at end of file +export default formHelper diff --git a/app/javascript/routes/reducers/stopPoints.js b/app/javascript/routes/reducers/stopPoints.js index eeec06327..0b42b504f 100644 --- a/app/javascript/routes/reducers/stopPoints.js +++ b/app/javascript/routes/reducers/stopPoints.js @@ -38,15 +38,15 @@ const stopPoints = (state = [], action) => {      case 'MOVE_STOP_UP':        return [          ...state.slice(0, action.index - 1), -        _.assign({}, state[action.index], { stoppoint_id: state[action.index - 1].stoppoint_id }), -        _.assign({}, state[action.index - 1], { stoppoint_id: state[action.index].stoppoint_id }), +        _.assign({}, state[action.index], { index: action.index - 1 }), +        _.assign({}, state[action.index - 1], { index: action.index }),          ...state.slice(action.index + 1)        ]      case 'MOVE_STOP_DOWN':        return [          ...state.slice(0, action.index), -        _.assign({}, state[action.index + 1], { stoppoint_id: state[action.index].stoppoint_id }), -        _.assign({}, state[action.index], { stoppoint_id: state[action.index + 1].stoppoint_id }), +        _.assign({}, state[action.index + 1], { index: action.index }), +        _.assign({}, state[action.index], { index: action.index + 1 }),          ...state.slice(action.index + 2)        ]      case 'DELETE_STOP': @@ -141,4 +141,4 @@ const stopPoints = (state = [], action) => {    }  } -export default stopPoints
\ No newline at end of file +export default stopPoints diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index 247c30668..67bbbe927 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -241,11 +241,9 @@ module Chouette      end      def self.matrix(vehicle_journeys) -      {}.tap do |hash| -        vehicle_journeys.map{ |vj| -          vj.vehicle_journey_at_stops.map{ |vjas |hash[ "#{vj.id}-#{vjas.stop_point_id}"] = vjas } -        } -      end +      Hash[*VehicleJourneyAtStop.where(vehicle_journey_id: vehicle_journeys.pluck(:id)).map do |vjas| +        [ "#{vjas.vehicle_journey_id}-#{vjas.stop_point_id}", vjas] +      end.flatten]      end      def self.with_stops diff --git a/app/views/vehicle_journeys/show.rabl b/app/views/vehicle_journeys/show.rabl index 830dee8bd..a39a53b05 100644 --- a/app/views/vehicle_journeys/show.rabl +++ b/app/views/vehicle_journeys/show.rabl @@ -49,11 +49,9 @@ child(:vehicle_journey_at_stops_matrix, :object_root => false) do |vehicle_stops        vehicle_stop.stop_point.stop_area.city_name      end -    [:id, :connecting_service_id, :boarding_alighting_possibility].map do |att| -      node(att) { vehicle_stop.send(att) ? vehicle_stop.send(att) : nil  } -    end +    attributes :id, :connecting_service_id, :boarding_alighting_possibility -    [:arrival_time, :departure_time].map do |att| +    [:arrival_time, :departure_time].each do |att|        node(att) do |vs|          {            hour: vs.send(att).try(:strftime, '%H'), diff --git a/spec/javascript/routes/reducers/stop_points_spec.js b/spec/javascript/routes/reducers/stop_points_spec.js index b375cdc2c..124618f9d 100644 --- a/spec/javascript/routes/reducers/stop_points_spec.js +++ b/spec/javascript/routes/reducers/stop_points_spec.js @@ -1,7 +1,17 @@  import stopPointsReducer from '../../../../app/javascript/routes/reducers/stopPoints' +import formHelper from '../../../../app/javascript/routes/form_helper' +import _ from 'lodash' + + //  _  _ ___ _    ___ ___ ___  ___ + // | || | __| |  | _ \ __| _ \/ __| + // | __ | _|| |__|  _/ _||   /\__ \ + // |_||_|___|____|_| |___|_|_\|___/ + //  let state = [] +formHelper.addInput = (...args)=>{} +  let fakeData = {    geometry: undefined,    registration_number: 'rn_test', @@ -10,34 +20,47 @@ let fakeData = {    user_objectid: 'uoid_test'  } +let update_stop_point = (stop_point, opts) => { +  return _.assign({}, stop_point, opts) +} + +let stop_point = (opts) => { +  return _.assign({}, +    { +      text: "", +      index: 0, +      edit: false, +      for_boarding: 'normal', +      for_alighting: 'normal', +      olMap: { isOpened: false, json: {} } +    }, +    opts +  ) +} + +let stop_point_1 = stop_point({text: 'first',  index: 0, stoppoint_id: 72 }) +let stop_point_2 = stop_point({text: 'second', index: 1, stoppoint_id: 73 }) +let stop_point_3 = stop_point({text: 'third',  index: 2, stoppoint_id: 74 }) + +let it_should_handle = (action, final_state, custom_state=null) => { +  it("should handle "+ action.type, () => { +    expect( +      stopPointsReducer(custom_state || state, action) +    ).toEqual( final_state ) +  }) +} + + + //  ___ ___ ___ ___ ___ + // / __| _ \ __/ __/ __| + // \__ \  _/ _| (__\__ \ + // |___/_| |___\___|___/ + // + +  describe('stops reducer', () => {    beforeEach(()=>{ -    state = [ -      { -        text: 'first', -        index: 0, -        stoppoint_id: 72, -        edit: false, -        for_boarding: 'normal', -        for_alighting: 'normal', -        olMap: { -          isOpened: false, -          json: {} -        } -      }, -      { -        text: 'second', -        index: 1, -        stoppoint_id: 73, -        edit: false, -        for_boarding: 'normal', -        for_alighting: 'normal', -        olMap: { -          isOpened: false, -          json: {} -        } -      } -    ] +    state = [ stop_point_1, stop_point_2, stop_point_3 ]    })    it('should return the initial state', () => { @@ -46,441 +69,124 @@ describe('stops reducer', () => {      ).toEqual([])    }) -  it('should handle ADD_STOP', () => { -    expect( -      stopPointsReducer(state, { -        type: 'ADD_STOP' -      }) -    ).toEqual( -      [ -        { -          text: 'first', -          index: 0, -          stoppoint_id: 72, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        }, -        { -          text: 'second', -          index: 1, -          stoppoint_id: 73, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        }, -        { -          text: '', -          index: 2, -          edit: true, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +  it_should_handle( +    {type: "ADD_STOP"}, +    [stop_point_1, stop_point_2, stop_point_3, stop_point({index: 3, edit: true})] +  ) -  it('should handle MOVE_UP_STOP', () => { -    expect( -      stopPointsReducer(state, { -        type: 'MOVE_STOP_UP', -        index: 1 -      }) -    ).toEqual( -      [ -        { -          text: 'second', -          index: 1, -          stoppoint_id: 72, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        }, -        { -          text: 'first', -          index: 0, -          stoppoint_id: 73, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +  it_should_handle( +    {type: 'MOVE_STOP_UP', index: 1}, +    [ update_stop_point(stop_point_2, {index: 0}), update_stop_point(stop_point_1, {index: 1}), stop_point_3 ] +  ) -  it('should handle MOVE_DOWN_STOP', () => { -    expect( -      stopPointsReducer(state, { -        type: 'MOVE_STOP_DOWN', -        index: 0 -      }) -    ).toEqual( -      [ -        { -          text: 'second', -          index: 1, -          stoppoint_id: 72, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        }, -        { -          text: 'first', -          index: 0, -          stoppoint_id: 73, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +  it_should_handle( +    {type: 'MOVE_STOP_DOWN', index: 0}, +    [ update_stop_point(stop_point_2, {index: 0}), update_stop_point(stop_point_1, {index: 1}), stop_point_3 ] +  ) -  // it('should handle DELETE_STOP', () => { -  //   expect( -  //     stopPointsReducer(state, { -  //       type: 'DELETE_STOP', -  //       index: 1 -  //     }) -  //   ).toEqual( -  //     [ -  //       { -  //         text: 'first', -  //         index: 0, -  //         stoppoint_id: 72, -  //         edit: false, -  //         for_boarding: 'normal', -  //         for_alighting: 'normal', -  //         olMap: { -  //           isOpened: false, -  //           json: {} -  //         } -  //       } -  //     ] -  //   ) -  // }) +  it_should_handle( +    {type: 'DELETE_STOP', index: 1}, +    [stop_point_1, stop_point_3] +  ) -  it('should handle UPDATE_INPUT_VALUE', () => { -    expect( -      stopPointsReducer(state, { -        type: 'UPDATE_INPUT_VALUE', -        index: 0, -        edit: false, -        text: { -          text: "new value", -          name: 'new', -          stoparea_id: 1, -          user_objectid: "1234", -          longitude: 123, -          latitude: 123, -          registration_number: '0', -          city_name: 'city', -          area_type: 'area', -          short_name: 'new', -          comment: 'newcomment' -        } -      }) -    ).toEqual( -      [ -        { -          text: 'new value', -          name: 'new', -          index: 0, -          stoppoint_id: 72, -          edit: false, -          stoparea_id: 1, -          for_boarding: 'normal', -          for_alighting: 'normal', -          user_objectid: "1234", -          longitude: 123, -          latitude: 123, -          registration_number: '0', -          city_name: 'city', -          area_type: 'area', -          short_name: 'new', -          comment: 'newcomment', -          olMap: { -            isOpened: false, -            json: {} -          } -        }, -        { -          text: 'second', -          index: 1, -          stoppoint_id: 73, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +  let text = { +    text: "new value", +    name: 'new', +    stoparea_id: 1, +    user_objectid: "1234", +    longitude: 123, +    latitude: 123, +    registration_number: '0', +    city_name: 'city', +    area_type: 'area', +    short_name: 'new', +    comment: 'newcomment' +  } +  it_should_handle( +    {type: 'UPDATE_INPUT_VALUE', index: 0, text: text}, +    [ +      update_stop_point(stop_point_1, text), +      stop_point_2, +      stop_point_3 +    ] +  ) -  it('should handle UPDATE_SELECT_VALUE', () => { -    expect( -      stopPointsReducer(state, { -        type :'UPDATE_SELECT_VALUE', -        select_id: 'for_boarding', -        select_value: 'prohibited', -        index: 0 -      }) -    ).toEqual( -      [ -        { -          text: 'first', -          index: 0, -          stoppoint_id: 72, -          edit: false, -          for_boarding: 'prohibited', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        }, -        { -          text: 'second', -          index: 1, -          stoppoint_id: 73, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +  it_should_handle( +    {type: 'UPDATE_SELECT_VALUE', index: 0, select_id: 'for_boarding', select_value: 'prohibited'}, +    [ +      update_stop_point(stop_point_1, {for_boarding: 'prohibited'}), +      stop_point_2, +      stop_point_3 +    ] +  ) -  it('should handle TOGGLE_MAP', () => { -    expect( -      stopPointsReducer(state, { -        type: 'TOGGLE_MAP', -        index: 0 -      }) -    ).toEqual( -      [ -        { +  it_should_handle( +    {type: 'TOGGLE_MAP', index: 0}, +    [ +      update_stop_point(stop_point_1, {olMap: { +        isOpened: true, +        json: {            text: 'first',            index: 0,            stoppoint_id: 72,            edit: false,            for_boarding: 'normal',            for_alighting: 'normal', -          olMap: { -            isOpened: true, -            json: { -              text: 'first', -              index: 0, -              stoppoint_id: 72, -              edit: false, -              for_boarding: 'normal', -              for_alighting: 'normal', -              olMap: undefined -            } -          } -        }, -        { -          text: 'second', -          index: 1, -          stoppoint_id: 73, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } +          olMap: undefined          } -      ] -    ) -  }) +      }}), +      stop_point_2, +      stop_point_3 +    ] +  ) -  it('should handle TOGGLE_EDIT', () => { -    expect( -      stopPointsReducer(state, { -        type: 'TOGGLE_EDIT', -        index: 0 -      }) -    ).toEqual( -      [ -        { -          text: 'first', -          index: 0, -          stoppoint_id: 72, -          edit: true, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        }, -        { -          text: 'second', -          index: 1, -          stoppoint_id: 73, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +  it_should_handle( +    {type: 'TOGGLE_EDIT', index: 0}, +    [ +      update_stop_point(stop_point_1, {edit: true}), +      stop_point_2, +      stop_point_3 +    ] +  ) -  it('should handle SELECT_MARKER', () => { -    let openedMapState = [ -      { -        text: 'first', -        index: 0, -        edit: false, -        for_boarding: 'normal', -        for_alighting: 'normal', -        olMap: { -          isOpened: true, -          json: {} -        } -      }, -      { -        text: 'second', -        index: 1, -        edit: false, -        for_boarding: 'normal', -        for_alighting: 'normal', -        olMap: { -          isOpened: false, -          json: {} -        } +  let openedMapState = [ +    update_stop_point(stop_point_1, { +      olMap: { +        isOpened: true, +        json: {}        } -    ] -    expect( -      stopPointsReducer(openedMapState, { -        type: 'SELECT_MARKER', -        index: 0, -        data: fakeData -      }) -    ).toEqual( -      [ -        { -          text: 'first', -          index: 0, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: true, -            json: fakeData -          } -        }, -        { -          text: 'second', -          index: 1, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +    }), +    stop_point_2, +    stop_point_3 +  ] -  it('should handle UNSELECT_MARKER', () => { -    let openedMapState = [ -      { -        text: 'first', -        index: 0, -        edit: false, -        for_boarding: 'normal', -        for_alighting: 'normal', +  it_should_handle( +    {type: 'SELECT_MARKER', index: 0, data: fakeData}, +    [ +      update_stop_point(stop_point_1, {          olMap: {            isOpened: true, -          json: {} -        } -      }, -      { -        text: 'second', -        index: 1, -        edit: false, -        for_boarding: 'normal', -        for_alighting: 'normal', +          json: fakeData +        } +      }), +      stop_point_2, +      stop_point_3 +    ], +    openedMapState +  ) + +  it_should_handle( +    {type: 'UNSELECT_MARKER', index: 0}, +    [ +      update_stop_point(stop_point_1, {          olMap: { -          isOpened: false, +          isOpened: true,            json: {}          } -      } -    ] - -    expect( -      stopPointsReducer(openedMapState, { -        type: 'UNSELECT_MARKER', -        index: 0 -      }) -    ).toEqual( -      [ -        { -          text: 'first', -          index: 0, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: true, -            json: {} -          } -        }, -        { -          text: 'second', -          index: 1, -          edit: false, -          for_boarding: 'normal', -          for_alighting: 'normal', -          olMap: { -            isOpened: false, -            json: {} -          } -        } -      ] -    ) -  }) +      }), +      stop_point_2, +      stop_point_3 +    ], +    openedMapState +  )  }) | 
