1 # == Schema Information
3 # Table name: relations
5 # relation_id :bigint(8) default(0), not null, primary key
6 # changeset_id :bigint(8) not null
7 # timestamp :datetime not null
8 # version :bigint(8) not null, primary key
9 # visible :boolean default(TRUE), not null
10 # redaction_id :integer
14 # relations_changeset_id_idx (changeset_id)
15 # relations_timestamp_idx (timestamp)
19 # relations_changeset_id_fkey (changeset_id => changesets.id)
20 # relations_redaction_id_fkey (redaction_id => redactions.id)
23 class OldRelation < ActiveRecord::Base
24 include ConsistencyValidations
25 include ObjectMetadata
27 self.table_name = "relations"
28 self.primary_keys = "relation_id", "version"
30 # note this needs to be included after the table name changes, or
31 # the queries generated by Redactable will use the wrong table name.
36 belongs_to :current_relation, :class_name => "Relation", :foreign_key => "relation_id"
38 has_many :old_members, -> { order(:sequence_id) }, :class_name => "OldRelationMember", :foreign_key => [:relation_id, :version]
39 has_many :old_tags, :class_name => "OldRelationTag", :foreign_key => [:relation_id, :version]
41 validates :changeset, :presence => true, :associated => true
42 validates :timestamp, :presence => true
43 validates :visible, :inclusion => [true, false]
45 def self.from_relation(relation)
46 old_relation = OldRelation.new
47 old_relation.visible = relation.visible
48 old_relation.changeset_id = relation.changeset_id
49 old_relation.timestamp = relation.timestamp
50 old_relation.relation_id = relation.id
51 old_relation.version = relation.version
52 old_relation.members = relation.members
53 old_relation.tags = relation.tags
57 def save_with_dependencies!
61 tag = OldRelationTag.new
64 tag.relation_id = relation_id
69 members.each_with_index do |m, i|
70 member = OldRelationMember.new
71 member.id = [relation_id, version, i]
72 member.member_type = m[0].classify
73 member.member_id = m[1]
74 member.member_role = m[2]
80 @members ||= old_members.collect do |member|
81 [member.member_type, member.member_id, member.member_role]
86 @tags ||= Hash[old_tags.collect { |t| [t.k, t.v] }]
94 doc = OSM::API.new.get_xml_doc
95 doc.root << to_xml_node
99 def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
100 el = XML::Node.new "relation"
101 el["id"] = relation_id.to_s
103 add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
105 old_members.each do |member|
106 member_el = XML::Node.new "member"
107 member_el["type"] = member.member_type.to_s.downcase
108 member_el["ref"] = member.member_id.to_s # "id" is considered uncool here as it should be unique in XML
109 member_el["role"] = member.member_role.to_s
113 add_tags_to_xml_node(el, old_tags)
118 # Temporary method to match interface to relations
123 # Pretend we're not in any relations
124 def containing_relation_members
128 # check whether this element is the latest version - that is,
129 # has the same version as its "current" counterpart.
130 def is_latest_version?
131 current_relation.version == version