diff options
5 files changed, 35 insertions, 4 deletions
diff --git a/app/javascript/vehicle_journeys/components/tools/EditVehicleJourney.js b/app/javascript/vehicle_journeys/components/tools/EditVehicleJourney.js index 08d74baba..b46857d19 100644 --- a/app/javascript/vehicle_journeys/components/tools/EditVehicleJourney.js +++ b/app/javascript/vehicle_journeys/components/tools/EditVehicleJourney.js @@ -2,10 +2,12 @@ import React, { Component } from 'react' import PropTypes from 'prop-types' import actions from '../../actions' import CompanySelect2 from './select2s/CompanySelect2' +import Select2 from 'react-select2-wrapper' export default class EditVehicleJourney extends Component { constructor(props) { super(props) + this.custom_fields = {} } handleSubmit() { @@ -15,8 +17,8 @@ export default class EditVehicleJourney extends Component { company = this.props.modal.modalProps.selectedCompany } else if (typeof this.props.modal.modalProps.vehicleJourney.company === "object") { company = this.props.modal.modalProps.vehicleJourney.company - } - this.props.onEditVehicleJourney(this.refs, company) + } + this.props.onEditVehicleJourney(_.assign({}, this.refs, {custom_fields: this.custom_fields}), company) this.props.onModalClose() $('#EditVehicleJourneyModal').modal('hide') } @@ -140,6 +142,22 @@ export default class EditVehicleJourney extends Component { defaultValue={this.props.modal.modalProps.vehicleJourney.checksum} /> </div> + {_.map(this.props.modal.modalProps.vehicleJourney.custom_fields, (cf, code) => + <div className='form-group' key={code}> + <label className='control-label'>{cf.name}</label> + <Select2 + data={_.map(cf.options.list_values, (v, k) => { + return {id: k, text: v} + })} + ref={'custom_fields.' + code} + className='form-control' + value={cf.value} + disabled={!this.props.editMode} + options={{theme: 'bootstrap'}} + onSelect={(e) => this.custom_fields[code] = e.params.data.id } + /> + </div> + )} </div> { diff --git a/app/javascript/vehicle_journeys/reducers/vehicleJourneys.js b/app/javascript/vehicle_journeys/reducers/vehicleJourneys.js index 0549c7adc..4ed85316f 100644 --- a/app/javascript/vehicle_journeys/reducers/vehicleJourneys.js +++ b/app/javascript/vehicle_journeys/reducers/vehicleJourneys.js @@ -144,10 +144,17 @@ export default function vehicleJourneys(state = [], action) { case 'EDIT_VEHICLEJOURNEY': return state.map((vj, i) => { if (vj.selected){ + let custom_fields = _.assign({}, vj.custom_fields) + _.each(custom_fields, (cf, code) => { + console.log(action.data.custom_fields) + let value = action.data.custom_fields[code] + custom_fields[code] = _.assign({}, custom_fields[code], {value}) + }) return _.assign({}, vj, { company: action.selectedCompany, published_journey_name: action.data.published_journey_name.value, published_journey_identifier: action.data.published_journey_identifier.value, + custom_fields: custom_fields, }) }else{ return vj diff --git a/app/models/chouette/vehicle_journey.rb b/app/models/chouette/vehicle_journey.rb index 32f3cb731..3bd83f83e 100644 --- a/app/models/chouette/vehicle_journey.rb +++ b/app/models/chouette/vehicle_journey.rb @@ -218,6 +218,7 @@ module Chouette ['company', 'journey_pattern'].map do |association| attrs["#{association}_id"] = item[association]['id'] if item[association] end + attrs["custom_field_values"] = Hash[*(item["custom_fields"] || {}).map{|k, v| [k, v["value"]]}.flatten] attrs end diff --git a/app/views/vehicle_journeys/show.rabl b/app/views/vehicle_journeys/show.rabl index eeed79b34..487dd8670 100644 --- a/app/views/vehicle_journeys/show.rabl +++ b/app/views/vehicle_journeys/show.rabl @@ -1,6 +1,6 @@ object @vehicle_journey -[:objectid, :published_journey_name, :published_journey_identifier, :company_id, :comment, :checksum].each do |attr| +[:objectid, :published_journey_name, :published_journey_identifier, :company_id, :comment, :checksum, :custom_fields].each do |attr| attributes attr, :unless => lambda { |m| m.send( attr).nil?} end diff --git a/spec/models/chouette/vehicle_journey_spec.rb b/spec/models/chouette/vehicle_journey_spec.rb index 3ec2387e5..3d0055f95 100644 --- a/spec/models/chouette/vehicle_journey_spec.rb +++ b/spec/models/chouette/vehicle_journey_spec.rb @@ -80,6 +80,7 @@ describe Chouette::VehicleJourney, :type => :model do item['purchase_windows'] = [] item['footnotes'] = [] item['purchase_windows'] = [] + item['custom_fields'] = vj.custom_fields vj.vehicle_journey_at_stops.each do |vjas| item['vehicle_journey_at_stops'] << vehicle_journey_at_stop_to_state(vjas) @@ -94,7 +95,8 @@ describe Chouette::VehicleJourney, :type => :model do let(:collection) { [state] } it 'should create new vj from state' do - new_vj = build(:vehicle_journey, objectid: nil, published_journey_name: 'dummy', route: route, journey_pattern: journey_pattern) + create(:custom_field, code: :energy) + new_vj = build(:vehicle_journey, objectid: nil, published_journey_name: 'dummy', route: route, journey_pattern: journey_pattern, custom_field_values: {energy: 99}) collection << vehicle_journey_to_state(new_vj) expect { Chouette::VehicleJourney.state_update(route, collection) @@ -106,6 +108,7 @@ describe Chouette::VehicleJourney, :type => :model do obj.run_callbacks(:commit) expect(obj.published_journey_name).to eq 'dummy' + expect(obj.custom_fields["energy"]["value"]).to eq 99 end it 'should save vehicle_journey_at_stops of newly created vj' do @@ -205,11 +208,13 @@ describe Chouette::VehicleJourney, :type => :model do it 'should update vj attributes from state' do state['published_journey_name'] = 'edited_name' state['published_journey_identifier'] = 'edited_identifier' + state['custom_fields'] = {energy: {value: 99}} Chouette::VehicleJourney.state_update(route, collection) expect(state['errors']).to be_nil expect(vehicle_journey.reload.published_journey_name).to eq state['published_journey_name'] expect(vehicle_journey.reload.published_journey_identifier).to eq state['published_journey_identifier'] + expect(vehicle_journey.reload.custom_field_value("energy")).to eq 99 end it 'should return errors when validation failed' do |
