]> git.openstreetmap.org Git - rails.git/blob - app/models/note.rb
Remove debugging code
[rails.git] / app / models / note.rb
1 class Note < ActiveRecord::Base
2   include GeoRecord
3
4   has_many :comments, -> { where(:visible => true).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id
5
6   validates_presence_of :id, :on => :update
7   validates_uniqueness_of :id
8   validates_numericality_of :latitude, :only_integer => true
9   validates_numericality_of :longitude, :only_integer => true
10   validates_presence_of :closed_at if :status == "closed"
11   validates_inclusion_of :status, :in => ["open", "closed", "hidden"]
12   validate :validate_position
13
14   after_initialize :set_defaults
15
16   # Sanity check the latitude and longitude and add an error if it's broken
17   def validate_position
18     errors.add(:base, "Note is not in the world") unless in_world?
19   end
20
21   # Close a note
22   def close
23     self.status = "closed"
24     self.closed_at = Time.now.getutc
25     self.save
26   end
27
28   # Reopen a note
29   def reopen
30     self.status = "open"
31     self.closed_at = nil
32     self.save
33   end
34
35   # Check if a note is visible
36   def visible?
37     status != "hidden"
38   end
39
40   # Check if a note is closed
41   def closed?
42     not closed_at.nil?
43   end
44
45   # Return the author object, derived from the first comment
46   def author
47     self.comments.first.author
48   end
49
50   # Return the author IP address, derived from the first comment
51   def author_ip
52     self.comments.first.author_ip
53   end
54
55 private
56
57   # Fill in default values for new notes
58   def set_defaults
59     self.status = "open" unless self.attribute_present?(:status)
60   end
61 end