source: subversion/sites/rails_port/app/models/old_way.rb @ 4144

Last change on this file since 4144 was 3418, checked in by tomhughes, 13 years ago

Bring some sort of sanity to sequence numbers for segments in a
way - now everything starts counting at one again and nothing starts
at zero.

File size: 2.9 KB
Line 
1class OldWay < ActiveRecord::Base
2  set_table_name 'ways'
3
4  belongs_to :user
5
6  def self.from_way(way)
7    old_way = OldWay.new
8    old_way.visible = way.visible
9    old_way.user_id = way.user_id
10    old_way.timestamp = way.timestamp
11    old_way.id = way.id
12    old_way.segs = way.segs
13    old_way.tags = way.tags
14    return old_way
15  end
16
17  def save_with_dependencies!
18
19    # dont touch this unless you really have figured out why it's called
20    # (Rails doesn't deal well with the old ways table (called 'ways') because
21    # it doesn't have a unique key. It knows how to insert and auto_increment
22    # id and get it back but we have that and we want to get the 'version' back
23    # we could add another column but thats a lot of data. No, set_primary_key
24    # doesn't work either.
25    save!
26    clear_aggregation_cache
27    clear_association_cache
28    @attributes.update(OldWay.find(:first, :conditions => ['id = ? AND timestamp = ?', self.id, self.timestamp]).instance_variable_get('@attributes'))
29
30    # ok, you can touch from here on
31
32    self.tags.each do |k,v|
33      tag = OldWayTag.new
34      tag.k = k
35      tag.v = v
36      tag.id = self.id
37      tag.version = self.version
38      tag.save!
39    end
40
41    i = 1
42    self.segs.each do |n|
43      seg = OldWaySegment.new
44      seg.id = self.id
45      seg.segment_id = n
46      seg.version = self.version
47      seg.save!
48    end
49  end
50
51  def segs
52    unless @segs
53        @segs = Array.new
54        OldWaySegment.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version], :order => "sequence_id").each do |seg|
55            @segs += [seg.segment_id]
56        end
57    end
58    @segs
59  end
60
61  def tags
62    unless @tags
63        @tags = Hash.new
64        OldWayTag.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version]).each do |tag|
65            @tags[tag.k] = tag.v
66        end
67    end
68    @tags = Hash.new unless @tags
69    @tags
70  end
71
72  def segs=(s)
73    @segs = s
74  end
75
76  def tags=(t)
77    @tags = t
78  end
79
80#  has_many :way_segments, :class_name => 'OldWaySegment', :foreign_key => 'id'
81#  has_many :way_tags, :class_name => 'OldWayTag', :foreign_key => 'id'
82
83  def old_segments
84    OldWaySegment.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])   
85  end
86
87  def old_tags
88    OldWayTag.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])   
89  end
90
91  def to_xml_node
92    el1 = XML::Node.new 'way'
93    el1['id'] = self.id.to_s
94    el1['visible'] = self.visible.to_s
95    el1['timestamp'] = self.timestamp.xmlschema
96    el1['user'] = self.user.display_name if self.user.data_public?
97   
98    self.old_segments.each do |seg| # FIXME need to make sure they come back in the right order
99      e = XML::Node.new 'seg'
100      e['id'] = seg.segment_id.to_s
101      el1 << e
102    end
103 
104    self.old_tags.each do |tag|
105      e = XML::Node.new 'tag'
106      e['k'] = tag.k
107      e['v'] = tag.v
108      el1 << e
109    end
110    return el1
111  end 
112end
Note: See TracBrowser for help on using the repository browser.