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, -> { order(:sequence_id) }, :class_name => 'OldRelationMember', :foreign_key => [:relation_id, :version]
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()
96 def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
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 el1['version'] = self.version.to_s
102 el1['changeset'] = self.changeset_id.to_s
104 if changeset_cache.key?(self.changeset_id)
105 # use the cache if available
107 changeset_cache[self.changeset_id] = self.changeset.user_id
110 user_id = changeset_cache[self.changeset_id]
112 if user_display_name_cache.key?(user_id)
113 # use the cache if available
114 elsif self.changeset.user.data_public?
115 user_display_name_cache[user_id] = self.changeset.user.display_name
117 user_display_name_cache[user_id] = nil
120 if not user_display_name_cache[user_id].nil?
121 el1['user'] = user_display_name_cache[user_id]
122 el1['uid'] = user_id.to_s
125 el1['redacted'] = self.redaction.id.to_s if self.redacted?
127 self.old_members.each do |member|
128 e = XML::Node.new 'member'
129 e['type'] = member.member_type.to_s.downcase
130 e['ref'] = member.member_id.to_s # "id" is considered uncool here as it should be unique in XML
131 e['role'] = member.member_role.to_s
135 self.old_tags.each do |tag|
136 e = XML::Node.new 'tag'
145 # Temporary method to match interface to nodes
150 # Temporary method to match interface to relations
152 return self.old_members
155 # Pretend we're not in any relations
156 def containing_relation_members
160 # check whether this element is the latest version - that is,
161 # has the same version as its "current" counterpart.
162 def is_latest_version?
163 current_relation.version == self.version