]> git.openstreetmap.org Git - rails.git/blob - app/models/note.rb
Use correct locale for note comment notification emails
[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   # Check if a note is visible
34   def visible?
35     status != "hidden"
36   end
37
38   # Check if a note is closed
39   def closed?
40     not closed_at.nil?
41   end
42
43   # Return the author object, derived from the first comment
44   def author
45     self.comments.first.author
46   end
47
48   # Return the author IP address, derived from the first comment
49   def author_ip
50     self.comments.first.author_ip
51   end
52
53 private
54
55   # Fill in default values for new notes
56   def set_defaults
57     self.status = "open" unless self.attribute_present?(:status)
58   end
59 end