1 class OldRelation < ActiveRecord::Base
2 include ConsistencyValidations
5 self.table_name = "relations"
6 self.primary_keys = "relation_id", "version"
8 # note this needs to be included after the table name changes, or
9 # the queries generated by Redactable will use the wrong table name.
14 belongs_to :current_relation, :class_name => "Relation", :foreign_key => "relation_id"
16 has_many :old_members, -> { order(:sequence_id) }, :class_name => 'OldRelationMember', :foreign_key => [:relation_id, :version]
17 has_many :old_tags, :class_name => 'OldRelationTag', :foreign_key => [:relation_id, :version]
19 validates_associated :changeset
21 def self.from_relation(relation)
22 old_relation = OldRelation.new
23 old_relation.visible = relation.visible
24 old_relation.changeset_id = relation.changeset_id
25 old_relation.timestamp = relation.timestamp
26 old_relation.relation_id = relation.id
27 old_relation.version = relation.version
28 old_relation.members = relation.members
29 old_relation.tags = relation.tags
33 def save_with_dependencies!
35 # see comment in old_way.rb ;-)
37 clear_aggregation_cache
38 clear_association_cache
39 @attributes.update(OldRelation.where(:relation_id => self.relation_id, :timestamp => self.timestamp).order("version DESC").first.instance_variable_get('@attributes'))
41 # ok, you can touch from here on
43 self.tags.each do |k,v|
44 tag = OldRelationTag.new
47 tag.relation_id = self.relation_id
48 tag.version = self.version
52 self.members.each_with_index do |m,i|
53 member = OldRelationMember.new
54 member.id = [self.relation_id, self.version, i]
55 member.member_type = m[0].classify
56 member.member_id = m[1]
57 member.member_role = m[2]
63 @members ||= self.old_members.collect do |member|
64 [member.member_type, member.member_id, member.member_role]
69 @tags ||= Hash[self.old_tags.collect { |t| [t.k, t.v] }]
81 doc = OSM::API.new.get_xml_doc
82 doc.root << to_xml_node()
86 def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
87 el = XML::Node.new 'relation'
88 el['id'] = self.relation_id.to_s
90 add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
92 self.old_members.each do |member|
93 member_el = XML::Node.new 'member'
94 member_el['type'] = member.member_type.to_s.downcase
95 member_el['ref'] = member.member_id.to_s # "id" is considered uncool here as it should be unique in XML
96 member_el['role'] = member.member_role.to_s
100 add_tags_to_xml_node(el, self.old_tags)
105 # Temporary method to match interface to nodes
110 # Temporary method to match interface to relations
112 return self.old_members
115 # Pretend we're not in any relations
116 def containing_relation_members
120 # check whether this element is the latest version - that is,
121 # has the same version as its "current" counterpart.
122 def is_latest_version?
123 current_relation.version == self.version