1 class OldRelation < ActiveRecord::Base
2 include ConsistencyValidations
4 self.table_name = "relations"
5 self.primary_keys = "relation_id", "version"
7 # note this needs to be included after the table name changes, or
8 # the queries generated by Redactable will use the wrong table name.
13 belongs_to :current_relation, :class_name => "Relation", :foreign_key => "relation_id"
15 has_many :old_members, :class_name => 'OldRelationMember', :foreign_key => [:relation_id, :version], :order => :sequence_id
16 has_many :old_tags, :class_name => 'OldRelationTag', :foreign_key => [:relation_id, :version]
18 validates_associated :changeset
20 def self.from_relation(relation)
21 old_relation = OldRelation.new
22 old_relation.visible = relation.visible
23 old_relation.changeset_id = relation.changeset_id
24 old_relation.timestamp = relation.timestamp
25 old_relation.relation_id = relation.id
26 old_relation.version = relation.version
27 old_relation.members = relation.members
28 old_relation.tags = relation.tags
32 def save_with_dependencies!
34 # see comment in old_way.rb ;-)
36 clear_aggregation_cache
37 clear_association_cache
38 @attributes.update(OldRelation.where(:relation_id => self.relation_id, :timestamp => self.timestamp).order("version DESC").first.instance_variable_get('@attributes'))
40 # ok, you can touch from here on
42 self.tags.each do |k,v|
43 tag = OldRelationTag.new
46 tag.relation_id = self.relation_id
47 tag.version = self.version
51 self.members.each_with_index do |m,i|
52 member = OldRelationMember.new
53 member.id = [self.relation_id, self.version, i]
54 member.member_type = m[0].classify
55 member.member_id = m[1]
56 member.member_role = m[2]
64 OldRelationMember.where(:relation_id => self.relation_id, :version => self.version).order(:sequence_id).each do |m|
65 @members += [[m.type,m.id,m.role]]
74 OldRelationTag.where(:relation_id => self.relation_id, :version => self.version).each do |tag|
78 @tags = Hash.new unless @tags
91 doc = OSM::API.new.get_xml_doc
92 doc.root << to_xml_node()
97 el1 = XML::Node.new 'relation'
98 el1['id'] = self.relation_id.to_s
99 el1['visible'] = self.visible.to_s
100 el1['timestamp'] = self.timestamp.xmlschema
101 if self.changeset.user.data_public?
102 el1['user'] = self.changeset.user.display_name
103 el1['uid'] = self.changeset.user.id.to_s
105 el1['version'] = self.version.to_s
106 el1['changeset'] = self.changeset_id.to_s
108 self.old_members.each do |member|
109 e = XML::Node.new 'member'
110 e['type'] = member.member_type.to_s.downcase
111 e['ref'] = member.member_id.to_s # "id" is considered uncool here as it should be unique in XML
112 e['role'] = member.member_role.to_s
116 self.old_tags.each do |tag|
117 e = XML::Node.new 'tag'
125 # Temporary method to match interface to nodes
130 # Temporary method to match interface to relations
132 return self.old_members
135 # Pretend we're not in any relations
136 def containing_relation_members
140 # check whether this element is the latest version - that is,
141 # has the same version as its "current" counterpart.
142 def is_latest_version?
143 current_relation.version == self.version