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