1 # == Schema Information
5 # id :bigint(8) not null, primary key
6 # latitude :integer not null
7 # longitude :integer not null
8 # tile :bigint(8) not null
9 # updated_at :datetime not null
10 # created_at :datetime not null
11 # status :enum not null
13 # description :text default(""), not null
19 # notes_created_at_idx (created_at)
20 # notes_tile_status_idx (tile,status)
21 # notes_updated_at_idx (updated_at)
25 # notes_user_id_fkey (user_id => users.id)
28 class Note < ApplicationRecord
31 has_many :comments, -> { left_joins(:author).where(:visible => true, :users => { :status => [nil, "active", "confirmed"] }).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id
32 has_many :all_comments, -> { left_joins(:author).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id, :inverse_of => :note
33 has_many :subscriptions, :class_name => "NoteSubscription"
34 has_many :subscribers, :through => :subscriptions, :source => :user
36 validates :id, :uniqueness => true, :presence => { :on => :update },
37 :numericality => { :on => :update, :only_integer => true }
38 validates :latitude, :longitude, :numericality => { :only_integer => true }
39 validates :closed_at, :presence => true, :if => proc { :status == "closed" }
40 validates :status, :inclusion => %w[open closed hidden]
42 validate :validate_position
44 scope :visible, -> { where.not(:status => "hidden") }
45 scope :invisible, -> { where(:status => "hidden") }
47 after_initialize :set_defaults
49 DEFAULT_FRESHLY_CLOSED_LIMIT = 7.days
51 # Sanity check the latitude and longitude and add an error if it's broken
53 errors.add(:base, "Note is not in the world") unless in_world?
58 self.status = "closed"
59 self.closed_at = Time.now.utc
70 # Check if a note is visible
75 # Check if a note is closed
81 return false unless closed?
83 Time.now.utc < freshly_closed_until
86 def freshly_closed_until
87 return nil unless closed?
89 closed_at + DEFAULT_FRESHLY_CLOSED_LIMIT
92 # Return the note's description, derived from the first comment
97 # Return the note's author object, derived from the first comment
102 # Return the note's author ID, derived from the first comment
104 comments.first.author_id
107 # Return the note's author IP address, derived from the first comment
109 comments.first.author_ip
114 # Fill in default values for new notes
116 self.status = "open" unless attribute_present?(:status)