From 9189093997610a8f289037d37b683c9fed060ae1 Mon Sep 17 00:00:00 2001 From: Steve Coast Date: Wed, 13 Dec 2006 16:05:37 +0000 Subject: [PATCH] precondition stuff --- app/controllers/segment_controller.rb | 8 ++++---- app/controllers/way_controller.rb | 9 ++++++++- app/models/segment.rb | 6 +++++- app/models/way.rb | 10 ++++++++++ 4 files changed, 27 insertions(+), 6 deletions(-) diff --git a/app/controllers/segment_controller.rb b/app/controllers/segment_controller.rb index 7e77609f3..5f2603f0b 100644 --- a/app/controllers/segment_controller.rb +++ b/app/controllers/segment_controller.rb @@ -13,11 +13,11 @@ class SegmentController < ApplicationController segment.user_id = @user.id - a = Node.find(segment.node_a.to_i) - b = Node.find(segment.node_b.to_i) + segment.from_node = Node.find(segment.node_a.to_i) + segment.to_node = Node.find(segment.node_b.to_i) - unless a and a.visible and b and b.visible - render :nothing => true, :status => 400 + unless segment.precondtions_ok? # are the nodes visible? + render :nothing => true, :status => 412 end if segment.save_with_history diff --git a/app/controllers/way_controller.rb b/app/controllers/way_controller.rb index a2d215760..ec8f31003 100644 --- a/app/controllers/way_controller.rb +++ b/app/controllers/way_controller.rb @@ -10,10 +10,17 @@ class WayController < ApplicationController if way way.user_id = @user.id + unless way.precondtions_ok? # are the segments (and their nodes) visible? + render :nothing => true, :status => 412 + return + end + if way.save_with_history render :text => way.id + return else render :nothing => true, :status => 500 + return end return else @@ -33,7 +40,7 @@ class WayController < ApplicationController way = Way.find(params[:id]) case request.method - + when :get unless way.visible render :nothing => true, :status => 410 diff --git a/app/models/segment.rb b/app/models/segment.rb index ddfc3db03..774517e2c 100644 --- a/app/models/segment.rb +++ b/app/models/segment.rb @@ -4,11 +4,12 @@ class Segment < ActiveRecord::Base validates_numericality_of :node_a validates_numericality_of :node_b - # FIXME validate a nd b exist and are visible has_many :old_segments, :foreign_key => :id belongs_to :user + has_one :from_node, :class => 'Node', :foreign_key => 'node_a' + has_one :to_node, :class => 'Node', :foreign_key => 'node_b' def self.from_xml(xml, create=false) p = XML::Parser.new @@ -102,5 +103,8 @@ class Segment < ActiveRecord::Base end end + def precondtions_ok? + return from_node and from_node.visible and to_node and to_node.visible + end end diff --git a/app/models/way.rb b/app/models/way.rb index 43bd4d8c3..55a578acd 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -139,4 +139,14 @@ class Way < ActiveRecord::Base old_way.save_with_dependencies end + def preconditions_ok? + self.segs.each do |n| + segment = Segment.find(n) + unless segment and segment.visible and segment.preconditions_ok? + return false + end + end + return true + end + end -- 2.39.5