]> git.openstreetmap.org Git - rails.git/blob - app/models/note.rb
Use correct title in og:title for pages with no title
[rails.git] / app / models / note.rb
1 class Note < ActiveRecord::Base
2   include GeoRecord
3
4   has_many :comments, -> { where(:visible => true).order(:created_at) }, :class_name => "NoteComment", :foreign_key => :note_id
5
6   validates :id, :uniqueness => true, :presence => { :on => :update },
7                  :numericality => { :on => :update, :integer_only => true }
8   validates :latitude, :longitude, :numericality => { :only_integer => true }
9   validates :closed_at, :presence => true, :if => proc { :status == "closed" }
10   validates :status, :inclusion => %w(open closed hidden)
11
12   validate :validate_position
13
14   scope :visible, -> { where("status != 'hidden'") }
15   scope :invisible, -> { where("status = 'hidden'") }
16
17   after_initialize :set_defaults
18
19   # Sanity check the latitude and longitude and add an error if it's broken
20   def validate_position
21     errors.add(:base, "Note is not in the world") unless in_world?
22   end
23
24   # Close a note
25   def close
26     self.status = "closed"
27     self.closed_at = Time.now.getutc
28     save
29   end
30
31   # Reopen a note
32   def reopen
33     self.status = "open"
34     self.closed_at = nil
35     save
36   end
37
38   # Check if a note is visible
39   def visible?
40     status != "hidden"
41   end
42
43   # Check if a note is closed
44   def closed?
45     !closed_at.nil?
46   end
47
48   # Return the author object, derived from the first comment
49   def author
50     comments.first.author
51   end
52
53   # Return the author IP address, derived from the first comment
54   def author_ip
55     comments.first.author_ip
56   end
57
58   private
59
60   # Fill in default values for new notes
61   def set_defaults
62     self.status = "open" unless self.attribute_present?(:status)
63   end
64 end