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 # Sanity check the latitude and longitude and add an error if it's broken
19 errors.add_to_base("Note is not in the world") unless in_world?
22 # Fill in default values for new notes
24 self.status = "open" unless self.attribute_present?(:status)
29 self.status = "closed"
30 self.closed_at = Time.now.getutc
34 # Return a flattened version of the comments for a note
35 def flatten_comment(separator_char, upto_timestamp = :nil)
38 self.comments.each do |comment|
39 next if upto_timestamp != :nil and comment.created_at > upto_timestamp
40 resp += (comment_no == 1 ? "" : separator_char)
41 resp += comment.body if comment.body
43 resp += comment.author_name if comment.author_name
44 resp += " " + comment.created_at.to_s + " ]"
51 # Check if a note is visible
53 return status != "hidden"
56 # Return the author object, derived from the first comment
58 self.comments.first.author
61 # Return the author IP address, derived from the first comment
63 self.comments.first.author_ip
66 # Return the author id, derived from the first comment
68 self.comments.first.author_id
71 # Return the author name, derived from the first comment
73 self.comments.first.author_name
76 # Custom JSON output routine for notes
77 def to_json(options = {})
78 super options.reverse_merge(
79 :methods => [ :lat, :lon ],
80 :only => [ :id, :status, :created_at ],
83 :only => [ :event, :author_name, :created_at, :body ]