1 class OldWay < ActiveRecord::Base
 
   2   include ConsistencyValidations
 
   8   validates_associated :changeset
 
  10   def self.from_way(way)
 
  12     old_way.visible = way.visible
 
  13     old_way.changeset_id = way.changeset_id
 
  14     old_way.timestamp = way.timestamp
 
  16     old_way.version = way.version
 
  18     old_way.tags = way.tags
 
  22   def save_with_dependencies!
 
  24     # dont touch this unless you really have figured out why it's called
 
  25     # (Rails doesn't deal well with the old ways table (called 'ways') because
 
  26     # it doesn't have a unique key. It knows how to insert and auto_increment
 
  27     # id and get it back but we have that and we want to get the 'version' back
 
  28     # we could add another column but thats a lot of data. No, set_primary_key
 
  29     # doesn't work either.
 
  31     clear_aggregation_cache
 
  32     clear_association_cache
 
  33     @attributes.update(OldWay.find(:first, :conditions => ['id = ? AND timestamp = ?', self.id, self.timestamp], :order => "version desc").instance_variable_get('@attributes'))
 
  35     # ok, you can touch from here on
 
  37     self.tags.each do |k,v|
 
  42       tag.version = self.version
 
  49       nd.id = [self.id, self.version, sequence]
 
  59         OldWayNode.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version], :order => "sequence_id").each do |nd|
 
  69         OldWayTag.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version]).each do |tag|
 
  73     @tags = Hash.new unless @tags
 
  85 #  has_many :way_nodes, :class_name => 'OldWayNode', :foreign_key => 'id'
 
  86 #  has_many :way_tags, :class_name => 'OldWayTag', :foreign_key => 'id'
 
  89     OldWayNode.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
 
  93     OldWayTag.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
 
  97     el1 = XML::Node.new 'way'
 
  98     el1['id'] = self.id.to_s
 
  99     el1['visible'] = self.visible.to_s
 
 100     el1['timestamp'] = self.timestamp.xmlschema
 
 101     if self.changeset.user.data_public?
 
 102       el1['user'] = self.changeset.user.display_name
 
 103       el1['uid'] = self.changeset.user.id.to_s
 
 105     el1['version'] = self.version.to_s
 
 106     el1['changeset'] = self.changeset.id.to_s
 
 108     self.old_nodes.each do |nd| # FIXME need to make sure they come back in the right order
 
 109       e = XML::Node.new 'nd'
 
 110       e['ref'] = nd.node_id.to_s
 
 114     self.old_tags.each do |tag|
 
 115       e = XML::Node.new 'tag'
 
 123   # Read full version of old way
 
 124   # For get_nodes_undelete, uses same nodes, even if they've moved since
 
 125   # For get_nodes_revert,   allocates new ids 
 
 126   # Currently returns Potlatch-style array
 
 127   # where [5] indicates whether latest version is usable as is (boolean)
 
 128   # (i.e. is it visible? are we actually reverting to an earlier version?)
 
 130   def get_nodes_undelete
 
 134           points << [node.lon, node.lat, n, node.version, node.tags_as_hash, node.visible]
 
 139   def get_nodes_revert(timestamp)
 
 142       oldnode=OldNode.find(:first, :conditions=>['id=? AND timestamp<=?',n,timestamp], :order=>"timestamp DESC")
 
 144       id=n; reuse=curnode.visible
 
 145       if oldnode.lat!=curnode.lat or oldnode.lon!=curnode.lon or oldnode.tags!=curnode.tags then
 
 146         # node has changed: if it's in other ways, give it a new id
 
 147         if curnode.ways-[self.id] then id=-1; reuse=false end
 
 149       points << [oldnode.lon, oldnode.lat, id, curnode.version, oldnode.tags_as_hash, reuse]
 
 154   # Temporary method to match interface to nodes
 
 159   # Temporary method to match interface to ways
 
 161     return self.old_nodes
 
 164   # Pretend we're not in any relations
 
 165   def containing_relation_members