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
16 # notes_created_at_idx (created_at)
17 # notes_tile_status_idx (tile,status)
18 # notes_updated_at_idx (updated_at)
21 class Note < ApplicationRecord
24 has_many :comments, -> { left_joins(:author).where(:visible => true, :users => { :status => [nil, "active", "confirmed"] }).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id
25 has_many :all_comments, -> { left_joins(:author).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id, :inverse_of => :note
26 has_many :subscriptions, :class_name => "NoteSubscription"
27 has_many :subscribers, :through => :subscriptions, :source => :user
29 validates :id, :uniqueness => true, :presence => { :on => :update },
30 :numericality => { :on => :update, :only_integer => true }
31 validates :latitude, :longitude, :numericality => { :only_integer => true }
32 validates :closed_at, :presence => true, :if => proc { :status == "closed" }
33 validates :status, :inclusion => %w[open closed hidden]
35 validate :validate_position
37 scope :visible, -> { where.not(:status => "hidden") }
38 scope :invisible, -> { where(:status => "hidden") }
40 after_initialize :set_defaults
42 DEFAULT_FRESHLY_CLOSED_LIMIT = 7.days
44 # Sanity check the latitude and longitude and add an error if it's broken
46 errors.add(:base, "Note is not in the world") unless in_world?
51 self.status = "closed"
52 self.closed_at = Time.now.utc
63 # Check if a note is visible
68 # Check if a note is closed
74 return false unless closed?
76 Time.now.utc < freshly_closed_until
79 def freshly_closed_until
80 return nil unless closed?
82 closed_at + DEFAULT_FRESHLY_CLOSED_LIMIT
85 # Return the author object, derived from the first comment
90 # Return the author IP address, derived from the first comment
92 comments.first.author_ip
97 # Fill in default values for new notes
99 self.status = "open" unless attribute_present?(:status)