]> git.openstreetmap.org Git - rails.git/blob - app/models/old_relation.rb
Merged 14009:14059 from trunk.
[rails.git] / app / models / old_relation.rb
1 class OldRelation < ActiveRecord::Base
2   include ConsistencyValidations
3   
4   set_table_name 'relations'
5
6   belongs_to :changeset
7   
8   validates_associated :changeset
9
10   def self.from_relation(relation)
11     old_relation = OldRelation.new
12     old_relation.visible = relation.visible
13     old_relation.changeset_id = relation.changeset_id
14     old_relation.timestamp = relation.timestamp
15     old_relation.id = relation.id
16     old_relation.version = relation.version
17     old_relation.members = relation.members
18     old_relation.tags = relation.tags
19     return old_relation
20   end
21
22   def save_with_dependencies!
23
24     # see comment in old_way.rb ;-)
25     save!
26     clear_aggregation_cache
27     clear_association_cache
28     @attributes.update(OldRelation.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 = OldRelationTag.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     self.members.each_with_index do |m,i|
42       member = OldRelationMember.new
43       member.id = [self.id, self.version, i]
44       member.member_type = m[0]
45       member.member_id = m[1]
46       member.member_role = m[2]
47       member.save!
48     end
49   end
50
51   def members
52     unless @members
53         @members = Array.new
54         OldRelationMember.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version], :order => "sequence_id").each do |m|
55             @members += [[m.type,m.id,m.role]]
56         end
57     end
58     @members
59   end
60
61   def tags
62     unless @tags
63         @tags = Hash.new
64         OldRelationTag.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 members=(s)
73     @members = s
74   end
75
76   def tags=(t)
77     @tags = t
78   end
79
80 #  has_many :relation_segments, :class_name => 'OldRelationSegment', :foreign_key => 'id'
81 #  has_many :relation_tags, :class_name => 'OldRelationTag', :foreign_key => 'id'
82
83   def old_members
84     OldRelationMember.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
85   end
86
87   def old_tags
88     OldRelationTag.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
89   end
90
91   def to_xml
92     doc = OSM::API.new.get_xml_doc
93     doc.root << to_xml_node()
94     return doc
95   end
96
97   def to_xml_node
98     el1 = XML::Node.new 'relation'
99     el1['id'] = self.id.to_s
100     el1['visible'] = self.visible.to_s
101     el1['timestamp'] = self.timestamp.xmlschema
102     if self.changeset.user.data_public?
103       el1['user'] = self.changeset.user.display_name
104       el1['uid'] = self.changeset.user.id.to_s
105     end
106     el1['version'] = self.version.to_s
107     el1['changeset'] = self.changeset_id.to_s
108     
109     self.old_members.each do |member|
110       e = XML::Node.new 'member'
111       e['type'] = member.member_type.to_s
112       e['ref'] = member.member_id.to_s # "id" is considered uncool here as it should be unique in XML
113       e['role'] = member.member_role.to_s
114       el1 << e
115     end
116  
117     self.old_tags.each do |tag|
118       e = XML::Node.new 'tag'
119       e['k'] = tag.k
120       e['v'] = tag.v
121       el1 << e
122     end
123     return el1
124   end
125
126   # Temporary method to match interface to nodes
127   def tags_as_hash
128     return self.tags
129   end
130
131   # Temporary method to match interface to relations
132   def relation_members
133     return self.old_members
134   end
135
136   # Pretend we're not in any relations
137   def containing_relation_members
138     return []
139   end
140 end