]> git.openstreetmap.org Git - rails.git/blob - app/models/map_bug.rb
b07ef2941d290cfce75363e1ce957ff118ccb69b
[rails.git] / app / models / map_bug.rb
1 class MapBug < ActiveRecord::Base
2   include GeoRecord
3
4   has_many :comments, :class_name => "MapBugComment", 
5                       :foreign_key => :bug_id, 
6                       :order => :date_created, 
7                       :conditions => "visible = true and comment is not null"
8
9   validates_presence_of :id, :on => :update
10   validates_uniqueness_of :id
11   validates_numericality_of :latitude, :only_integer => true
12   validates_numericality_of :longitude, :only_integer => true
13   validates_presence_of :date_created
14   validates_presence_of :last_changed
15   validates_presence_of :date_closed if :status == "closed"
16   validates_inclusion_of :status, :in => ["open", "closed", "hidden"]
17
18   def self.create_bug(lat, lon)
19     bug = MapBug.new(:lat => lat, :lon => lon)
20     raise OSM::APIBadUserInput.new("The node is outside this world") unless bug.in_world?
21
22     bug.date_created = Time.now.getutc
23     bug.last_changed = Time.now.getutc
24     bug.status = "open"
25
26     return bug
27   end
28
29   def close_bug
30     self.status = "closed"
31     close_time = Time.now.getutc
32     self.last_changed = close_time
33     self.date_closed = close_time
34
35     self.save
36   end
37
38   def flatten_comment(separator_char, upto_timestamp = :nil)
39     resp = ""
40     comment_no = 1
41     self.comments.each do |comment|
42       next if upto_timestamp != :nil and comment.date_created > upto_timestamp
43       resp += (comment_no == 1 ? "" : separator_char)
44       resp += comment.comment if comment.comment
45       resp += " [ " 
46       resp += comment.commenter_name if comment.commenter_name
47       resp += " " + comment.date_created.to_s + " ]"
48       comment_no += 1
49     end
50
51     return resp
52   end
53
54   def visible
55     return status != "hidden"
56   end
57 end