]> git.openstreetmap.org Git - rails.git/blob - app/models/old_relation.rb
Remove some (hopefully) redundant code
[rails.git] / app / models / old_relation.rb
1 class OldRelation < ActiveRecord::Base
2   include ConsistencyValidations
3   include ObjectMetadata
4   
5   self.table_name = "relations"
6   self.primary_keys = "relation_id", "version"
7
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.
10   include Redactable
11
12   belongs_to :changeset
13   belongs_to :redaction
14   belongs_to :current_relation, :class_name => "Relation", :foreign_key => "relation_id"
15
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]
18   
19   validates_associated :changeset
20
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
30     return old_relation
31   end
32
33   def save_with_dependencies!
34     save!
35
36     self.tags.each do |k,v|
37       tag = OldRelationTag.new
38       tag.k = k
39       tag.v = v
40       tag.relation_id = self.relation_id
41       tag.version = self.version
42       tag.save!
43     end
44
45     self.members.each_with_index do |m,i|
46       member = OldRelationMember.new
47       member.id = [self.relation_id, self.version, i]
48       member.member_type = m[0].classify
49       member.member_id = m[1]
50       member.member_role = m[2]
51       member.save!
52     end
53   end
54
55   def members
56     @members ||= self.old_members.collect do |member|
57       [member.member_type, member.member_id, member.member_role]
58     end
59   end
60
61   def tags
62     @tags ||= Hash[self.old_tags.collect { |t| [t.k, t.v] }]
63   end
64
65   def members=(s)
66     @members = s
67   end
68
69   def tags=(t)
70     @tags = t
71   end
72
73   def to_xml
74     doc = OSM::API.new.get_xml_doc
75     doc.root << to_xml_node()
76     return doc
77   end
78
79   def to_xml_node(changeset_cache = {}, user_display_name_cache = {})
80     el = XML::Node.new 'relation'
81     el['id'] = self.relation_id.to_s
82
83     add_metadata_to_xml_node(el, self, changeset_cache, user_display_name_cache)
84
85     self.old_members.each do |member|
86       member_el = XML::Node.new 'member'
87       member_el['type'] = member.member_type.to_s.downcase
88       member_el['ref'] = member.member_id.to_s # "id" is considered uncool here as it should be unique in XML
89       member_el['role'] = member.member_role.to_s
90       el << member_el
91     end
92     
93     add_tags_to_xml_node(el, self.old_tags)
94
95     return el
96   end
97
98   # Temporary method to match interface to nodes
99   def tags_as_hash
100     return self.tags
101   end
102
103   # Temporary method to match interface to relations
104   def relation_members
105     return self.old_members
106   end
107
108   # Pretend we're not in any relations
109   def containing_relation_members
110     return []
111   end
112
113   # check whether this element is the latest version - that is,
114   # has the same version as its "current" counterpart.
115   def is_latest_version?
116     current_relation.version == self.version
117   end
118 end