]> git.openstreetmap.org Git - rails.git/blob - app/models/map_bug.rb
Allow any valid (per RFC 3986) scheme name in OAuth callback URLs
[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 => :created_at,
7                       :conditions => "visible = true AND body 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 :closed_at if :status == "closed"
14   validates_inclusion_of :status, :in => ["open", "closed", "hidden"]
15
16   def self.create_bug(lat, lon)
17     bug = MapBug.new(:lat => lat, :lon => lon, :status => "open")
18     raise OSM::APIBadUserInput.new("The node is outside this world") unless bug.in_world?
19
20     return bug
21   end
22
23   def close_bug
24     self.status = "closed"
25     self.closed_at = Time.now.getutc
26
27     self.save
28   end
29
30   def flatten_comment(separator_char, upto_timestamp = :nil)
31     resp = ""
32     comment_no = 1
33     self.comments.each do |comment|
34       next if upto_timestamp != :nil and comment.created_at > upto_timestamp
35       resp += (comment_no == 1 ? "" : separator_char)
36       resp += comment.body if comment.body
37       resp += " [ " 
38       resp += comment.author_name if comment.author_name
39       resp += " " + comment.created_at.to_s + " ]"
40       comment_no += 1
41     end
42
43     return resp
44   end
45
46   def visible
47     return status != "hidden"
48   end
49
50   def author
51     self.comments.first.author
52   end
53
54   def author_ip
55     self.comments.first.author_ip
56   end
57
58   def author_id
59     self.comments.first.author_id
60   end
61
62   def author_name
63     self.comments.first.author_name
64   end
65 end