1 class Note < ActiveRecord::Base
4 has_many :comments, :class_name => "NoteComment",
5 :foreign_key => :note_id,
7 :conditions => { :visible => true }
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
17 attr_accessible :lat, :lon
19 after_initialize :set_defaults
21 # Sanity check the latitude and longitude and add an error if it's broken
23 errors.add(:base, "Note is not in the world") unless in_world?
28 self.status = "closed"
29 self.closed_at = Time.now.getutc
40 # Check if a note is visible
45 # Check if a note is closed
50 # Return the author object, derived from the first comment
52 self.comments.first.author
55 # Return the author IP address, derived from the first comment
57 self.comments.first.author_ip
62 # Fill in default values for new notes
64 self.status = "open" unless self.attribute_present?(:status)