]> git.openstreetmap.org Git - rails.git/blob - app/models/note.rb
Merge remote-tracking branch 'upstream/pull/5611'
[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 #  description :text             default(""), not null
14 #  user_id     :bigint(8)
15 #  user_ip     :inet
16 #
17 # Indexes
18 #
19 #  notes_created_at_idx   (created_at)
20 #  notes_tile_status_idx  (tile,status)
21 #  notes_updated_at_idx   (updated_at)
22 #
23 # Foreign Keys
24 #
25 #  notes_user_id_fkey  (user_id => users.id)
26 #
27
28 class Note < ApplicationRecord
29   include GeoRecord
30
31   belongs_to :author, :class_name => "User", :foreign_key => "user_id", :optional => true
32
33   has_many :comments, -> { left_joins(:author).where(:visible => true, :users => { :status => [nil, "active", "confirmed"] }).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id
34   has_many :all_comments, -> { left_joins(:author).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id, :inverse_of => :note
35   has_many :subscriptions, :class_name => "NoteSubscription"
36   has_many :subscribers, :through => :subscriptions, :source => :user
37
38   validates :id, :uniqueness => true, :presence => { :on => :update },
39                  :numericality => { :on => :update, :only_integer => true }
40   validates :latitude, :longitude, :numericality => { :only_integer => true }
41   validates :closed_at, :presence => true, :if => proc { :status == "closed" }
42   validates :status, :inclusion => %w[open closed hidden]
43
44   validate :validate_position
45
46   scope :visible, -> { where.not(:status => "hidden") }
47   scope :invisible, -> { where(:status => "hidden") }
48
49   after_initialize :set_defaults
50
51   DEFAULT_FRESHLY_CLOSED_LIMIT = 7.days
52
53   # Sanity check the latitude and longitude and add an error if it's broken
54   def validate_position
55     errors.add(:base, "Note is not in the world") unless in_world?
56   end
57
58   # Close a note
59   def close
60     self.status = "closed"
61     self.closed_at = Time.now.utc
62     save
63   end
64
65   # Reopen a note
66   def reopen
67     self.status = "open"
68     self.closed_at = nil
69     save
70   end
71
72   # Check if a note is visible
73   def visible?
74     status != "hidden"
75   end
76
77   # Check if a note is closed
78   def closed?
79     !closed_at.nil?
80   end
81
82   def freshly_closed?
83     return false unless closed?
84
85     Time.now.utc < freshly_closed_until
86   end
87
88   def freshly_closed_until
89     return nil unless closed?
90
91     closed_at + DEFAULT_FRESHLY_CLOSED_LIMIT
92   end
93
94   # Return the note's description, derived from the first comment
95   def description
96     if user_ip.nil? && user_id.nil?
97       all_comments.first.body
98     else
99       RichText.new("text", super)
100     end
101   end
102
103   # Return the note's author object, derived from the first comment
104   def author
105     if user_ip.nil? && user_id.nil?
106       all_comments.first.author
107     else
108       super
109     end
110   end
111
112   private
113
114   # Fill in default values for new notes
115   def set_defaults
116     self.status = "open" unless attribute_present?(:status)
117   end
118 end