]> git.openstreetmap.org Git - rails.git/blob - app/models/note.rb
Merge pull request #3691 from AntonKhorev/wikipedia-secondary-links
[rails.git] / app / models / note.rb
1 # == Schema Information
2 #
3 # Table name: notes
4 #
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
12 #  closed_at  :datetime
13 #
14 # Indexes
15 #
16 #  notes_created_at_idx   (created_at)
17 #  notes_tile_status_idx  (tile,status)
18 #  notes_updated_at_idx   (updated_at)
19 #
20
21 class Note < ApplicationRecord
22   include GeoRecord
23
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
27   validates :id, :uniqueness => true, :presence => { :on => :update },
28                  :numericality => { :on => :update, :only_integer => true }
29   validates :latitude, :longitude, :numericality => { :only_integer => true }
30   validates :closed_at, :presence => true, :if => proc { :status == "closed" }
31   validates :status, :inclusion => %w[open closed hidden]
32
33   validate :validate_position
34
35   scope :visible, -> { where.not(:status => "hidden") }
36   scope :invisible, -> { where(:status => "hidden") }
37
38   after_initialize :set_defaults
39
40   DEFAULT_FRESHLY_CLOSED_LIMIT = 7.days
41
42   # Sanity check the latitude and longitude and add an error if it's broken
43   def validate_position
44     errors.add(:base, "Note is not in the world") unless in_world?
45   end
46
47   # Close a note
48   def close
49     self.status = "closed"
50     self.closed_at = Time.now.utc
51     save
52   end
53
54   # Reopen a note
55   def reopen
56     self.status = "open"
57     self.closed_at = nil
58     save
59   end
60
61   # Check if a note is visible
62   def visible?
63     status != "hidden"
64   end
65
66   # Check if a note is closed
67   def closed?
68     !closed_at.nil?
69   end
70
71   def freshly_closed?
72     return false unless closed?
73
74     Time.now.utc < freshly_closed_until
75   end
76
77   def freshly_closed_until
78     return nil unless closed?
79
80     closed_at + DEFAULT_FRESHLY_CLOSED_LIMIT
81   end
82
83   # Return the author object, derived from the first comment
84   def author
85     comments.first.author
86   end
87
88   # Return the author IP address, derived from the first comment
89   def author_ip
90     comments.first.author_ip
91   end
92
93   private
94
95   # Fill in default values for new notes
96   def set_defaults
97     self.status = "open" unless attribute_present?(:status)
98   end
99 end