aboutsummaryrefslogtreecommitdiffstats
path: root/app/controllers
diff options
context:
space:
mode:
authorXinhui2017-01-09 15:53:50 +0100
committerXinhui2017-01-09 16:03:14 +0100
commitddf2c5e2ffe57afc7c60d2e9048de86d7d93f684 (patch)
tree95b55906e0dd1acad94b5b65f33662a0d679bd97 /app/controllers
parenta3bda653a15b2f2c5790fd7ad0cd3c58025c4619 (diff)
downloadchouette-core-ddf2c5e2ffe57afc7c60d2e9048de86d7d93f684.tar.bz2
Wip save new journey_patterns
Refs #2213
Diffstat (limited to 'app/controllers')
-rw-r--r--app/controllers/journey_patterns_collections_controller.rb43
1 files changed, 33 insertions, 10 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