Made relations ordered. Added some tests for this. Otherwise interface is unchanged.
[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   def self.from_relation(relation)
9     old_relation = OldRelation.new
10     old_relation.visible = relation.visible
11     old_relation.changeset_id = relation.changeset_id
12     old_relation.timestamp = relation.timestamp
13     old_relation.id = relation.id
14     old_relation.version = relation.version
15     old_relation.members = relation.members
16     old_relation.tags = relation.tags
17     return old_relation
18   end
19
20   def save_with_dependencies!
21
22     # see comment in old_way.rb ;-)
23     save!
24     clear_aggregation_cache
25     clear_association_cache
26     @attributes.update(OldRelation.find(:first, :conditions => ['id = ? AND timestamp = ?', self.id, self.timestamp]).instance_variable_get('@attributes'))
27
28     # ok, you can touch from here on
29
30     self.tags.each do |k,v|
31       tag = OldRelationTag.new
32       tag.k = k
33       tag.v = v
34       tag.id = self.id
35       tag.version = self.version
36       tag.save!
37     end
38
39     self.members.each_with_index do |m,i|
40       member = OldRelationMember.new
41       member.id = [self.id, self.version, i]
42       member.member_type = m[0]
43       member.member_id = m[1]
44       member.member_role = m[2]
45       member.save!
46     end
47   end
48
49   def members
50     unless @members
51         @members = Array.new
52         OldRelationMember.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version], :order => "sequence_id").each do |m|
53             @members += [[m.type,m.id,m.role]]
54         end
55     end
56     @members
57   end
58
59   def tags
60     unless @tags
61         @tags = Hash.new
62         OldRelationTag.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version]).each do |tag|
63             @tags[tag.k] = tag.v
64         end
65     end
66     @tags = Hash.new unless @tags
67     @tags
68   end
69
70   def members=(s)
71     @members = s
72   end
73
74   def tags=(t)
75     @tags = t
76   end
77
78 #  has_many :relation_segments, :class_name => 'OldRelationSegment', :foreign_key => 'id'
79 #  has_many :relation_tags, :class_name => 'OldRelationTag', :foreign_key => 'id'
80
81   def old_members
82     OldRelationMember.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
83   end
84
85   def old_tags
86     OldRelationTag.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
87   end
88
89   def to_xml
90     doc = OSM::API.new.get_xml_doc
91     doc.root << to_xml_node()
92     return doc
93   end
94
95   def to_xml_node
96     el1 = XML::Node.new 'relation'
97     el1['id'] = self.id.to_s
98     el1['visible'] = self.visible.to_s
99     el1['timestamp'] = self.timestamp.xmlschema
100     if self.changeset.user.data_public?
101       el1['user'] = self.changeset.user.display_name
102       el1['uid'] = self.changeset.user.id.to_s
103     end
104     el1['version'] = self.version.to_s
105     el1['changeset'] = self.changeset_id.to_s
106     
107     self.old_members.each do |member|
108       e = XML::Node.new 'member'
109       e['type'] = member.member_type.to_s
110       e['ref'] = member.member_id.to_s # "id" is considered uncool here as it should be unique in XML
111       e['role'] = member.member_role.to_s
112       el1 << e
113     end
114  
115     self.old_tags.each do |tag|
116       e = XML::Node.new 'tag'
117       e['k'] = tag.k
118       e['v'] = tag.v
119       el1 << e
120     end
121     return el1
122   end
123
124   # Temporary method to match interface to nodes
125   def tags_as_hash
126     return self.tags
127   end
128
129   # Temporary method to match interface to relations
130   def relation_members
131     return self.old_members
132   end
133
134   # Pretend we're not in any relations
135   def containing_relation_members
136     return []
137   end
138 end