1 class Note < ActiveRecord::Base
4 has_many :comments, -> { where(:visible => true).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id
6 validates :id, :uniqueness => true, :presence => { :on => :update },
7 :numericality => { :on => :update, :integer_only => true }
8 validates :latitude, :longitude, :numericality => { :only_integer => true }
9 validates :closed_at, :presence => true, :if => proc { :status == "closed" }
10 validates :status, :inclusion => %w(open closed hidden)
12 validate :validate_position
14 scope :visible, -> { where("status != 'hidden'") }
15 scope :invisible, -> { where("status = 'hidden'") }
17 after_initialize :set_defaults
19 # Sanity check the latitude and longitude and add an error if it's broken
21 errors.add(:base, "Note is not in the world") unless in_world?
26 self.status = "closed"
27 self.closed_at = Time.now.getutc
38 # Check if a note is visible
43 # Check if a note is closed
48 # Return the author object, derived from the first comment
53 # Return the author IP address, derived from the first comment
55 comments.first.author_ip
60 # Fill in default values for new notes
62 self.status = "open" unless self.attribute_present?(:status)