1 class OldRelation < ActiveRecord::Base
2 include ConsistencyValidations
5 self.table_name = "relations"
6 self.primary_keys = "relation_id", "version"
10 belongs_to :current_relation, :class_name => "Relation", :foreign_key => "relation_id"
12 has_many :old_members, :class_name => 'OldRelationMember', :foreign_key => [:relation_id, :version], :order => :sequence_id
13 has_many :old_tags, :class_name => 'OldRelationTag', :foreign_key => [:relation_id, :version]
15 validates_associated :changeset
17 def self.from_relation(relation)
18 old_relation = OldRelation.new
19 old_relation.visible = relation.visible
20 old_relation.changeset_id = relation.changeset_id
21 old_relation.timestamp = relation.timestamp
22 old_relation.relation_id = relation.id
23 old_relation.version = relation.version
24 old_relation.members = relation.members
25 old_relation.tags = relation.tags
29 def save_with_dependencies!
31 # see comment in old_way.rb ;-)
33 clear_aggregation_cache
34 clear_association_cache
35 @attributes.update(OldRelation.where(:relation_id => self.relation_id, :timestamp => self.timestamp).order("version DESC").first.instance_variable_get('@attributes'))
37 # ok, you can touch from here on
39 self.tags.each do |k,v|
40 tag = OldRelationTag.new
43 tag.relation_id = self.relation_id
44 tag.version = self.version
48 self.members.each_with_index do |m,i|
49 member = OldRelationMember.new
50 member.id = [self.relation_id, self.version, i]
51 member.member_type = m[0].classify
52 member.member_id = m[1]
53 member.member_role = m[2]
61 OldRelationMember.where(:relation_id => self.relation_id, :version => self.version).order(:sequence_id).each do |m|
62 @members += [[m.type,m.id,m.role]]
71 OldRelationTag.where(:relation_id => self.relation_id, :version => self.version).each do |tag|
75 @tags = Hash.new unless @tags
88 doc = OSM::API.new.get_xml_doc
89 doc.root << to_xml_node()
94 el1 = XML::Node.new 'relation'
95 el1['id'] = self.relation_id.to_s
96 el1['visible'] = self.visible.to_s
97 el1['timestamp'] = self.timestamp.xmlschema
98 if self.changeset.user.data_public?
99 el1['user'] = self.changeset.user.display_name
100 el1['uid'] = self.changeset.user.id.to_s
102 el1['version'] = self.version.to_s
103 el1['changeset'] = self.changeset_id.to_s
105 self.old_members.each do |member|
106 e = XML::Node.new 'member'
107 e['type'] = member.member_type.to_s.downcase
108 e['ref'] = member.member_id.to_s # "id" is considered uncool here as it should be unique in XML
109 e['role'] = member.member_role.to_s
113 self.old_tags.each do |tag|
114 e = XML::Node.new 'tag'
122 # Temporary method to match interface to nodes
127 # Temporary method to match interface to relations
129 return self.old_members
132 # Pretend we're not in any relations
133 def containing_relation_members
137 # check whether this element is the latest version - that is,
138 # has the same version as its "current" counterpart.
139 def is_latest_version?
140 current_relation.version == self.version