diff options
Diffstat (limited to 'app')
| -rw-r--r-- | app/controllers/journey_patterns_collections_controller.rb | 43 | ||||
| -rw-r--r-- | app/models/chouette/journey_pattern.rb | 1 | ||||
| -rw-r--r-- | app/views/api/v1/journey_patterns/show.rabl | 6 |
3 files changed, 37 insertions, 13 deletions
diff --git a/app/controllers/journey_patterns_collections_controller.rb b/app/controllers/journey_patterns_collections_controller.rb index 39355932e..b4c23ff8d 100644 --- a/app/controllers/journey_patterns_collections_controller.rb +++ b/app/controllers/journey_patterns_collections_controller.rb @@ -16,28 +16,51 @@ class JourneyPatternsCollectionsController < ChouetteController def update state = JSON.parse request.raw_post state.each do |item| - journey_pattern = journey_pattern_by_objectid(item['object_id']) - journey_pattern_update_stop_points(journey_pattern, item['stop_points']) if journey_pattern + jp = journey_pattern_by_objectid(item['object_id']) || create_journey_pattern(item) + raise "Unable to find or create journey_pattern !!" unless jp + update_journey_pattern(jp, item) end - journey_patterns_state + respond_to do |format| + format.json { render json: state } + end end protected + def update_jp jp, item + jp.update_attributes(fetch_jp_attributes(item)) + end + + def fetch_jp_attributes item + { + name: item['name'], + published_name: item['published_name'], + registration_number: item['registration_number'] + } + end + + def create_journey_pattern item + jp = route.journey_patterns.create(fetch_jp_attributes(item)) + item['object_id'] = jp.objectid + jp + end - def journey_pattern_update_stop_points journey_pattern, stop_points - stop_points.each do |sp| - stop_id = sp['id'] - exist = journey_pattern.stop_area_ids.include?(stop_id) + def update_journey_pattern jp, item + item['stop_points'].each do |sp| + exist = jp.stop_area_ids.include?(sp['id']) + ap "exist value #{exist}" next if exist && sp['checked'] + stop_point = route.stop_points.find_by(stop_area_id: sp['id']) - stop_point = route.stop_points.find_by(stop_area_id: stop_id) if sp['checked'] && !exist - journey_pattern.stop_points << stop_point + jp.stop_points << stop_point + ap "adding stop_points to jp #{jp.objectid}" else - journey_pattern.stop_points.delete(stop_point) + jp.stop_points.delete(stop_point) + ap "deleting stop_points from jp #{jp.objectid}" end end + update_jp(jp, item) end def journey_patterns_state diff --git a/app/models/chouette/journey_pattern.rb b/app/models/chouette/journey_pattern.rb index 75e1a4a14..945ee4f70 100644 --- a/app/models/chouette/journey_pattern.rb +++ b/app/models/chouette/journey_pattern.rb @@ -12,6 +12,7 @@ class Chouette::JourneyPattern < Chouette::TridentActiveRecord has_many :route_sections, through: :journey_pattern_sections, dependent: :destroy validates_presence_of :route + validates_presence_of :name enum section_status: { todo: 0, completed: 1, control: 2 } diff --git a/app/views/api/v1/journey_patterns/show.rabl b/app/views/api/v1/journey_patterns/show.rabl index 882660360..808808462 100644 --- a/app/views/api/v1/journey_patterns/show.rabl +++ b/app/views/api/v1/journey_patterns/show.rabl @@ -15,7 +15,7 @@ end unless root_object.vehicle_journeys.empty? child :stop_points => :stop_area_short_descriptions do |stop_points| node do |stop_point| - partial("api/v1/stop_areas/short_description", :object => stop_point.stop_area) - end -end unless root_object.stop_points.empty? + partial("api/v1/stop_areas/short_description", :object => stop_point.stop_area) + end +end |
