]> git.openstreetmap.org Git - rails.git/blob - app/models/old_relation.rb
api06: s/delete_with_relations_and_history/delete_with_history/g
[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.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     
97     self.old_members.each do |member|
98       e = XML::Node.new 'member'
99       e['type'] = member.member_type.to_s
100       e['ref'] = member.member_id.to_s # "id" is considered uncool here as it should be unique in XML
101       e['role'] = member.member_role.to_s
102       el1 << e
103     end
104  
105     self.old_tags.each do |tag|
106       e = XML::Node.new 'tag'
107       e['k'] = tag.k
108       e['v'] = tag.v
109       el1 << e
110     end
111     return el1
112   end 
113 end