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 :changeset, :presence => true, :associated => true
20 validates :timestamp, :presence => true
21 validates :visible, :inclusion => [true, false]
23 def self.from_relation(relation)
24 old_relation = OldRelation.new
25 old_relation.visible = relation.visible
26 old_relation.changeset_id = relation.changeset_id
27 old_relation.timestamp = relation.timestamp
28 old_relation.relation_id = relation.id
29 old_relation.version = relation.version
30 old_relation.members = relation.members
31 old_relation.tags = relation.tags
35 def save_with_dependencies!
39 tag = OldRelationTag.new
42 tag.relation_id = relation_id
47 members.each_with_index do |m, i|
48 member = OldRelationMember.new
49 member.id = [relation_id, version, i]
50 member.member_type = m[0].classify
51 member.member_id = m[1]
52 member.member_role = m[2]
58 @members ||= old_members.collect do |member|
59 [member.member_type, member.member_id, member.member_role]
64 @tags ||= Hash[old_tags.collect { |t| [t.k, t.v] }]
72 doc = OSM::API.new.get_xml_doc
73 doc.root << to_xml_node
77 def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
78 el = XML::Node.new "relation"
79 el["id"] = relation_id.to_s
81 add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
83 old_members.each do |member|
84 member_el = XML::Node.new "member"
85 member_el["type"] = member.member_type.to_s.downcase
86 member_el["ref"] = member.member_id.to_s # "id" is considered uncool here as it should be unique in XML
87 member_el["role"] = member.member_role.to_s
91 add_tags_to_xml_node(el, old_tags)
96 # Temporary method to match interface to relations
101 # Pretend we're not in any relations
102 def containing_relation_members
106 # check whether this element is the latest version - that is,
107 # has the same version as its "current" counterpart.
108 def is_latest_version?
109 current_relation.version == version