]> git.openstreetmap.org Git - rails.git/blob - app/models/message.rb
Add an HTML version of the diary comment notification mail
[rails.git] / app / models / message.rb
1 require 'validators'
2
3 class Message < ActiveRecord::Base
4   belongs_to :sender, :class_name => "User", :foreign_key => :from_user_id
5   belongs_to :recipient, :class_name => "User", :foreign_key => :to_user_id
6
7   validates_presence_of :title, :body, :sent_on, :sender, :recipient
8   validates_length_of :title, :within => 1..255
9   validates_inclusion_of :message_read, :in => [ true, false ]
10   validates_as_utf8 :title
11
12   attr_accessible :title, :body
13
14   after_initialize :set_defaults
15
16   def body
17     RichText.new(read_attribute(:body_format), read_attribute(:body))
18   end
19
20   def digest
21     md5 = Digest::MD5.new
22     md5 << from_user_id.to_s
23     md5 << to_user_id.to_s
24     md5 << sent_on.xmlschema
25     md5 << title
26     md5 << body
27     md5.hexdigest
28   end
29
30 private
31
32   def set_defaults
33     self.body_format = "markdown" unless self.attribute_present?(:body_format)
34   end
35 end