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