Altered old_way stuff to be Railsy like old_node is now
[rails.git] / app / models / old_relation.rb
1 class OldRelation < ActiveRecord::Base
2   include ConsistencyValidations
3   include Redactable
4   
5   self.table_name = "relations"
6   self.primary_keys = "relation_id", "version"
7
8   belongs_to :changeset
9   belongs_to :redaction
10   belongs_to :current_relation, :class_name => "Relation", :foreign_key => "relation_id"
11
12   has_many :old_members, :class_name => 'OldRelationMember', :foreign_key => [:relation_id, :version], :order => :sequence_id
13   has_many :old_tags, :class_name => 'OldRelationTag', :foreign_key => [:relation_id, :version]
14   
15   validates_associated :changeset
16
17   def self.from_relation(relation)
18     old_relation = OldRelation.new
19     old_relation.visible = relation.visible
20     old_relation.changeset_id = relation.changeset_id
21     old_relation.timestamp = relation.timestamp
22     old_relation.relation_id = relation.id
23     old_relation.version = relation.version
24     old_relation.members = relation.members
25     old_relation.tags = relation.tags
26     return old_relation
27   end
28
29   def save_with_dependencies!
30
31     # see comment in old_way.rb ;-)
32     save!
33     clear_aggregation_cache
34     clear_association_cache
35     @attributes.update(OldRelation.where(:relation_id => self.relation_id, :timestamp => self.timestamp).order("version DESC").first.instance_variable_get('@attributes'))
36
37     # ok, you can touch from here on
38
39     self.tags.each do |k,v|
40       tag = OldRelationTag.new
41       tag.k = k
42       tag.v = v
43       tag.relation_id = self.relation_id
44       tag.version = self.version
45       tag.save!
46     end
47
48     self.members.each_with_index do |m,i|
49       member = OldRelationMember.new
50       member.id = [self.relation_id, self.version, i]
51       member.member_type = m[0].classify
52       member.member_id = m[1]
53       member.member_role = m[2]
54       member.save!
55     end
56   end
57
58   def members
59     unless @members
60       @members = Array.new
61       OldRelationMember.where(:relation_id => self.relation_id, :version => self.version).order(:sequence_id).each do |m|
62         @members += [[m.type,m.id,m.role]]
63       end
64     end
65     @members
66   end
67
68   def tags
69     unless @tags
70       @tags = Hash.new
71       OldRelationTag.where(:relation_id => self.relation_id, :version => self.version).each do |tag|
72         @tags[tag.k] = tag.v
73       end
74     end
75     @tags = Hash.new unless @tags
76     @tags
77   end
78
79   def members=(s)
80     @members = s
81   end
82
83   def tags=(t)
84     @tags = t
85   end
86
87   def to_xml
88     doc = OSM::API.new.get_xml_doc
89     doc.root << to_xml_node()
90     return doc
91   end
92
93   def to_xml_node
94     el1 = XML::Node.new 'relation'
95     el1['id'] = self.relation_id.to_s
96     el1['visible'] = self.visible.to_s
97     el1['timestamp'] = self.timestamp.xmlschema
98     if self.changeset.user.data_public?
99       el1['user'] = self.changeset.user.display_name
100       el1['uid'] = self.changeset.user.id.to_s
101     end
102     el1['version'] = self.version.to_s
103     el1['changeset'] = self.changeset_id.to_s
104     
105     self.old_members.each do |member|
106       e = XML::Node.new 'member'
107       e['type'] = member.member_type.to_s.downcase
108       e['ref'] = member.member_id.to_s # "id" is considered uncool here as it should be unique in XML
109       e['role'] = member.member_role.to_s
110       el1 << e
111     end
112  
113     self.old_tags.each do |tag|
114       e = XML::Node.new 'tag'
115       e['k'] = tag.k
116       e['v'] = tag.v
117       el1 << e
118     end
119     return el1
120   end
121
122   # Temporary method to match interface to nodes
123   def tags_as_hash
124     return self.tags
125   end
126
127   # Temporary method to match interface to relations
128   def relation_members
129     return self.old_members
130   end
131
132   # Pretend we're not in any relations
133   def containing_relation_members
134     return []
135   end
136
137   # check whether this element is the latest version - that is,
138   # has the same version as its "current" counterpart.
139   def is_latest_version?
140     current_relation.version == self.version
141   end
142 end