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