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