From 328d47e506972fededfa1080967224928c36a4cf Mon Sep 17 00:00:00 2001 From: Shaun McDonald Date: Fri, 3 Oct 2008 17:09:58 +0000 Subject: [PATCH] Fixing a bug in my foreign keys. I can now upload and download nodes and ways using JOSM in the new API. --- app/controllers/node_controller.rb | 5 ++++- app/controllers/way_controller.rb | 3 +-- app/models/node.rb | 18 +++++++++--------- app/models/old_node.rb | 6 +++--- app/models/old_way.rb | 2 +- app/models/way.rb | 16 ++++++++++------ db/migrate/020_add_changesets.rb | 2 +- 7 files changed, 29 insertions(+), 23 deletions(-) diff --git a/app/controllers/node_controller.rb b/app/controllers/node_controller.rb index 70bf59734..03ae64f41 100644 --- a/app/controllers/node_controller.rb +++ b/app/controllers/node_controller.rb @@ -13,10 +13,13 @@ class NodeController < ApplicationController def create if request.put? node = Node.from_xml(request.raw_post, true) + # FIXME remove debug + logger.debug request.raw_post + logger.debug node if node node.version = 0 - node.user_id = @user.id + #node.changeset_id = node.changeset node.visible = true node.save_with_history! diff --git a/app/controllers/way_controller.rb b/app/controllers/way_controller.rb index cf1634fa5..17f166ddc 100644 --- a/app/controllers/way_controller.rb +++ b/app/controllers/way_controller.rb @@ -12,11 +12,10 @@ class WayController < ApplicationController way = Way.from_xml(request.raw_post, true) if way - if !way.preconditions_ok? + unless way.preconditions_ok? render :text => "", :status => :precondition_failed else way.version = 0 - way.user_id = @user.id way.save_with_history! render :text => way.id.to_s, :content_type => "text/plain" diff --git a/app/models/node.rb b/app/models/node.rb index 2efdb1bc9..2f3001a49 100644 --- a/app/models/node.rb +++ b/app/models/node.rb @@ -5,7 +5,7 @@ class Node < ActiveRecord::Base set_table_name 'current_nodes' - validates_presence_of :user_id, :timestamp + validates_presence_of :changeset_id, :timestamp validates_inclusion_of :visible, :in => [ true, false ] validates_numericality_of :latitude, :longitude validate :validate_position @@ -77,6 +77,7 @@ class Node < ActiveRecord::Base node.version = pt['version'] node.lat = pt['lat'].to_f node.lon = pt['lon'].to_f + node.changeset_id = pt['changeset'].to_i return nil unless node.in_world? @@ -151,7 +152,8 @@ class Node < ActiveRecord::Base raise OSM::APIVersionMismatchError.new(new_node.version, version) end - self.user_id = user.id + # FIXME logic need looked at + self.changeset_id = user.id self.latitude = new_node.latitude self.longitude = new_node.longitude self.tags = new_node.tags @@ -173,15 +175,15 @@ class Node < ActiveRecord::Base user_display_name_cache = {} if user_display_name_cache.nil? - if user_display_name_cache and user_display_name_cache.key?(self.user_id) + if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id) # use the cache if available - elsif self.user.data_public? - user_display_name_cache[self.user_id] = self.user.display_name + elsif self.changeset.user.data_public? + user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name else - user_display_name_cache[self.user_id] = nil + user_display_name_cache[self.changeset.user_id] = nil end - el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil? + el1['user'] = user_display_name_cache[self.changeset.user_id] unless user_display_name_cache[self.changeset.user_id].nil? self.tags.each do |k,v| el2 = XML::Node.new('tag') @@ -219,6 +221,4 @@ class Node < ActiveRecord::Base @tags[k] = v end - - end diff --git a/app/models/old_node.rb b/app/models/old_node.rb index 58a514bcd..d4d9f1775 100644 --- a/app/models/old_node.rb +++ b/app/models/old_node.rb @@ -3,7 +3,7 @@ class OldNode < ActiveRecord::Base set_table_name 'nodes' - validates_presence_of :user_id, :timestamp + validates_presence_of :changeset_id, :timestamp validates_inclusion_of :visible, :in => [ true, false ] validates_numericality_of :latitude, :longitude validate :validate_position @@ -21,7 +21,7 @@ class OldNode < ActiveRecord::Base old_node.visible = node.visible old_node.tags = node.tags old_node.timestamp = node.timestamp - old_node.user_id = node.user_id + old_node.changeset_id = node.changeset_id old_node.id = node.id old_node.version = node.version return old_node @@ -38,7 +38,7 @@ class OldNode < ActiveRecord::Base el1['id'] = self.id.to_s el1['lat'] = self.lat.to_s el1['lon'] = self.lon.to_s - el1['user'] = self.user.display_name if self.user.data_public? + el1['user'] = self.changeset.user.display_name if self.changeset.user.data_public? self.tags.each do |k,v| el2 = XML::Node.new('tag') diff --git a/app/models/old_way.rb b/app/models/old_way.rb index 2820d9438..f297cfc1a 100644 --- a/app/models/old_way.rb +++ b/app/models/old_way.rb @@ -6,7 +6,7 @@ class OldWay < ActiveRecord::Base def self.from_way(way) old_way = OldWay.new old_way.visible = way.visible - old_way.user_id = way.user_id + old_way.changeset_id = way.changeset_id old_way.timestamp = way.timestamp old_way.id = way.id old_way.version = way.version diff --git a/app/models/way.rb b/app/models/way.rb index 8da959853..7c6e9d06b 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -3,6 +3,9 @@ class Way < ActiveRecord::Base set_table_name 'current_ways' + validates_presence_of :changeset_id, :timestamp + validates_inclusion_of :visible, :in => [ true, false ] + belongs_to :changeset has_many :old_ways, :foreign_key => 'id', :order => 'version' @@ -37,13 +40,14 @@ class Way < ActiveRecord::Base end way.version = pt['version'] + way.changeset_id = pt['changeset'] if create way.timestamp = Time.now way.visible = true else if pt['timestamp'] - way.timestamp = Time.parse(pt['timestamp']) + way.timestamp = Time.parse(pt['timestamp']) end end @@ -84,15 +88,15 @@ class Way < ActiveRecord::Base user_display_name_cache = {} if user_display_name_cache.nil? - if user_display_name_cache and user_display_name_cache.key?(self.user_id) + if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id) # use the cache if available - elsif self.user.data_public? - user_display_name_cache[self.user_id] = self.user.display_name + elsif self.changeset.user.data_public? + user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name else - user_display_name_cache[self.user_id] = nil + user_display_name_cache[self.changeset.user_id] = nil end - el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil? + el1['user'] = user_display_name_cache[self.changeset.user_id] unless user_display_name_cache[self.changeset.user_id].nil? # make sure nodes are output in sequence_id order ordered_nodes = [] diff --git a/db/migrate/020_add_changesets.rb b/db/migrate/020_add_changesets.rb index c21b50782..75a909e80 100644 --- a/db/migrate/020_add_changesets.rb +++ b/db/migrate/020_add_changesets.rb @@ -38,7 +38,7 @@ class AddChangesets < ActiveRecord::Migration @@conv_user_tables.each { |tbl| rename_column tbl, :user_id, :changeset_id #foreign keys too - add_foreign_key tbl, [:changeset_id], :users, [:id] + add_foreign_key tbl, [:changeset_id], :changesets, [:id] } end -- 2.39.5