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

Last change on this file since 3148 was 2417, checked in by steve, 13 years ago

API returns user info if public

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