]> git.openstreetmap.org Git - rails.git/blob - app/models/diary_comment.rb
Refactor site#welcome to use abilities instead of require_user
[rails.git] / app / models / diary_comment.rb
1 # == Schema Information
2 #
3 # Table name: diary_comments
4 #
5 #  id             :integer          not null, primary key
6 #  diary_entry_id :integer          not null
7 #  user_id        :integer          not null
8 #  body           :text             not null
9 #  created_at     :datetime         not null
10 #  updated_at     :datetime         not null
11 #  visible        :boolean          default(TRUE), not null
12 #  body_format    :enum             default("markdown"), not null
13 #
14 # Indexes
15 #
16 #  diary_comment_user_id_created_at_index  (user_id,created_at)
17 #  diary_comments_entry_id_idx             (diary_entry_id,id) UNIQUE
18 #
19 # Foreign Keys
20 #
21 #  diary_comments_diary_entry_id_fkey  (diary_entry_id => diary_entries.id)
22 #  diary_comments_user_id_fkey         (user_id => users.id)
23 #
24
25 class DiaryComment < ActiveRecord::Base
26   belongs_to :user
27   belongs_to :diary_entry
28
29   scope :visible, -> { where(:visible => true) }
30
31   validates :body, :presence => true
32   validates :diary_entry, :user, :associated => true
33
34   after_save :spam_check
35
36   def body
37     RichText.new(self[:body_format], self[:body])
38   end
39
40   def digest
41     md5 = Digest::MD5.new
42     md5 << diary_entry_id.to_s
43     md5 << user_id.to_s
44     md5 << created_at.xmlschema
45     md5 << body
46     md5.hexdigest
47   end
48
49   private
50
51   def spam_check
52     user.spam_check
53   end
54 end