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
33 # Return a flattened version of the comments for a note
34 def flatten_comment(separator_char, upto_timestamp = :nil)
37 self.comments.each do |comment|
38 next if upto_timestamp != :nil and comment.created_at > upto_timestamp
39 resp += (comment_no == 1 ? "" : separator_char)
40 resp += comment.body if comment.body
42 resp += comment.author.display_name if comment.author
43 resp += " " + comment.created_at.to_s + " ]"
50 # Check if a note is visible
52 return status != "hidden"
55 # Return the author object, derived from the first comment
57 self.comments.first.author
60 # Return the author IP address, derived from the first comment
62 self.comments.first.author_ip
67 # Fill in default values for new notes
69 self.status = "open" unless self.attribute_present?(:status)