]> git.openstreetmap.org Git - rails.git/blob - app/models/old_relation.rb
moving the conistency checks for updates and deletes to library, hopefully got the...
[rails.git] / app / models / old_relation.rb
1 class OldRelation < ActiveRecord::Base
2   set_table_name 'relations'
3
4   belongs_to :changeset
5
6   def self.from_relation(relation)
7     old_relation = OldRelation.new
8     old_relation.visible = relation.visible
9     old_relation.changeset_id = relation.changeset_id
10     old_relation.timestamp = relation.timestamp
11     old_relation.id = relation.id
12     old_relation.version = relation.version
13     old_relation.members = relation.members
14     old_relation.tags = relation.tags
15     return old_relation
16   end
17
18   def save_with_dependencies!
19
20     # see comment in old_way.rb ;-)
21     save!
22     clear_aggregation_cache
23     clear_association_cache
24     @attributes.update(OldRelation.find(:first, :conditions => ['id = ? AND timestamp = ?', self.id, self.timestamp]).instance_variable_get('@attributes'))
25
26     # ok, you can touch from here on
27
28     self.tags.each do |k,v|
29       tag = OldRelationTag.new
30       tag.k = k
31       tag.v = v
32       tag.id = self.id
33       tag.version = self.version
34       tag.save!
35     end
36
37     i = 1
38     self.members.each do |m|
39       member = OldRelationMember.new
40       member.id = self.id
41       member.member_type = m[0]
42       member.member_id = m[1]
43       member.member_role = m[2]
44       member.version = self.version
45       member.save!
46     end
47   end
48
49   def members
50     unless @members
51         @members = Array.new
52         OldRelationMember.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version]).each do |m|
53             @members += [[m.type,m.id,m.role]]
54         end
55     end
56     @members
57   end
58
59   def tags
60     unless @tags
61         @tags = Hash.new
62         OldRelationTag.find(:all, :conditions => ["id = ? AND version = ?", self.id, self.version]).each do |tag|
63             @tags[tag.k] = tag.v
64         end
65     end
66     @tags = Hash.new unless @tags
67     @tags
68   end
69
70   def members=(s)
71     @members = s
72   end
73
74   def tags=(t)
75     @tags = t
76   end
77
78 #  has_many :relation_segments, :class_name => 'OldRelationSegment', :foreign_key => 'id'
79 #  has_many :relation_tags, :class_name => 'OldRelationTag', :foreign_key => 'id'
80
81   def old_members
82     OldRelationMember.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
83   end
84
85   def old_tags
86     OldRelationTag.find(:all, :conditions => ['id = ? AND version = ?', self.id, self.version])    
87   end
88
89   def to_xml_node
90     el1 = XML::Node.new 'relation'
91     el1['id'] = self.id.to_s
92     el1['visible'] = self.visible.to_s
93     el1['timestamp'] = self.timestamp.xmlschema
94     el1['user'] = self.user.display_name if self.user.data_public?
95     el1['version'] = self.version.to_s
96     el1['changeset'] = self.changeset_id.to_s
97     
98     self.old_members.each do |member|
99       e = XML::Node.new 'member'
100       e['type'] = member.member_type.to_s
101       e['ref'] = member.member_id.to_s # "id" is considered uncool here as it should be unique in XML
102       e['role'] = member.member_role.to_s
103       el1 << e
104     end
105  
106     self.old_tags.each do |tag|
107       e = XML::Node.new 'tag'
108       e['k'] = tag.k
109       e['v'] = tag.v
110       el1 << e
111     end
112     return el1
113   end
114
115   # Temporary method to match interface to nodes
116   def tags_as_hash
117     return self.tags
118   end
119
120   # Temporary method to match interface to relations
121   def relation_members
122     return self.old_members
123   end
124
125   # Pretend we're not in any relations
126   def containing_relation_members
127     return []
128   end
129 end