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