X-Git-Url: https://git.openstreetmap.org/rails.git/blobdiff_plain/86b4d1bc2c05a68053c7485217144a7fcf8ef4e8..e1b205913c9afd6b41684add7308cef4613112e2:/app/models/way.rb diff --git a/app/models/way.rb b/app/models/way.rb index a6192e2d7..9ddb603a8 100644 --- a/app/models/way.rb +++ b/app/models/way.rb @@ -1,15 +1,22 @@ class Way < ActiveRecord::Base require 'xml/libxml' - belongs_to :user + set_table_name 'current_ways' - has_many :nodes, :through => :way_nodes, :order => 'sequence_id' - has_many :way_nodes, :foreign_key => 'id', :order => 'sequence_id' - has_many :way_tags, :foreign_key => 'id' + validates_presence_of :changeset_id, :timestamp + validates_inclusion_of :visible, :in => [ true, false ] + + belongs_to :changeset has_many :old_ways, :foreign_key => 'id', :order => 'version' - set_table_name 'current_ways' + has_many :way_nodes, :foreign_key => 'id', :order => 'sequence_id' + has_many :nodes, :through => :way_nodes, :order => 'sequence_id' + + has_many :way_tags, :foreign_key => 'id' + + has_many :containing_relation_members, :class_name => "RelationMember", :as => :member + has_many :containing_relations, :class_name => "Relation", :through => :containing_relation_members, :source => :relation, :extend => ObjectFinder def self.from_xml(xml, create=false) begin @@ -33,13 +40,14 @@ class Way < ActiveRecord::Base end way.version = pt['version'] + way.changeset_id = pt['changeset'] if create way.timestamp = Time.now way.visible = true else if pt['timestamp'] - way.timestamp = Time.parse(pt['timestamp']) + way.timestamp = Time.parse(pt['timestamp']) end end @@ -77,18 +85,19 @@ class Way < ActiveRecord::Base el1['visible'] = self.visible.to_s el1['timestamp'] = self.timestamp.xmlschema el1['version'] = self.version.to_s + el1['changeset'] = self.changeset_id.to_s user_display_name_cache = {} if user_display_name_cache.nil? - if user_display_name_cache and user_display_name_cache.key?(self.user_id) + if user_display_name_cache and user_display_name_cache.key?(self.changeset.user_id) # use the cache if available - elsif self.user.data_public? - user_display_name_cache[self.user_id] = self.user.display_name + elsif self.changeset.user.data_public? + user_display_name_cache[self.changeset.user_id] = self.changeset.user.display_name else - user_display_name_cache[self.user_id] = nil + user_display_name_cache[self.changeset.user_id] = nil end - el1['user'] = user_display_name_cache[self.user_id] unless user_display_name_cache[self.user_id].nil? + el1['user'] = user_display_name_cache[self.changeset.user_id] unless user_display_name_cache[self.changeset.user_id].nil? # make sure nodes are output in sequence_id order ordered_nodes = [] @@ -221,8 +230,7 @@ class Way < ActiveRecord::Base return true end - # Delete the way and it's relations, but don't really delete it - set its visibility to false and update the history etc to maintain wiki-like functionality. - def delete_with_relations_and_history(user) + def delete_with_history(user) if self.visible # FIXME # this should actually delete the relations, @@ -246,18 +254,11 @@ class Way < ActiveRecord::Base end # delete a way and it's nodes that aren't part of other ways, with history - def delete_with_relations_and_nodes_and_history(user) - - node_ids = self.nodes.collect {|node| node.id } - node_ids_not_to_delete = [] - way_nodes = WayNode.find(:all, :conditions => "node_id in (#{node_ids.join(',')}) and id != #{self.id}") - - node_ids_not_to_delete = way_nodes.collect {|way_node| way_node.node_id} - - node_ids_to_delete = node_ids - node_ids_not_to_delete + # FIXME: merge the potlatch code to delete the relations + def delete_with_relations_and_nodes_and_history(user) # delete the nodes not used by other ways - node_ids_to_delete.each do |node_id| + self.unshared_node_ids.each do |node_id| n = Node.find(node_id) n.user_id = user.id n.visible = false @@ -266,7 +267,23 @@ class Way < ActiveRecord::Base self.user_id = user.id - self.delete_with_relations_and_history(user) + self.delete_with_history(user) + end + + # Find nodes that belong to this way only + def unshared_node_ids + node_ids = self.nodes.collect { |node| node.id } + + unless node_ids.empty? + way_nodes = WayNode.find(:all, :conditions => "node_id in (#{node_ids.join(',')}) and id != #{self.id}") + node_ids = node_ids - way_nodes.collect { |way_node| way_node.node_id } + end + + return node_ids + end + # Temporary method to match interface to nodes + def tags_as_hash + return self.tags end end