]> git.openstreetmap.org Git - rails.git/blob - app/models/note.rb
Merge remote-tracking branch 'osmlab/iD-1.6.0'
[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   scope :visible, -> { where("status != 'hidden'") }
15   scope :invisible, -> { where("status = 'hidden'") }
16
17   after_initialize :set_defaults
18
19   # Sanity check the latitude and longitude and add an error if it's broken
20   def validate_position
21     errors.add(:base, "Note is not in the world") unless in_world?
22   end
23
24   # Close a note
25   def close
26     self.status = "closed"
27     self.closed_at = Time.now.getutc
28     self.save
29   end
30
31   # Reopen a note
32   def reopen
33     self.status = "open"
34     self.closed_at = nil
35     self.save
36   end
37
38   # Check if a note is visible
39   def visible?
40     status != "hidden"
41   end
42
43   # Check if a note is closed
44   def closed?
45     not closed_at.nil?
46   end
47
48   # Return the author object, derived from the first comment
49   def author
50     self.comments.first.author
51   end
52
53   # Return the author IP address, derived from the first comment
54   def author_ip
55     self.comments.first.author_ip
56   end
57
58 private
59
60   # Fill in default values for new notes
61   def set_defaults
62     self.status = "open" unless self.attribute_present?(:status)
63   end
64 end