Advertisement
Guest User

Untitled

a guest
Oct 12th, 2012
443
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.56 KB | None | 0 0
  1. class Message
  2. include Mongoid::Document
  3. include Mongoid::Timestamps::Created
  4.  
  5. # Relationships
  6. belongs_to :sender, :class_name => 'User', :inverse_of => :messages_sent
  7. belongs_to :receiver, :class_name => 'User', :inverse_of => :messages_received
  8. belongs_to :thread, :class_name => 'Message' # Reference to parent message
  9. has_many :replies, :class_name => 'Message', :foreign_key => 'thread_id'
  10. scope :in_reply_to, lambda { |message| where({:thread => message}).asc('created_at') }
  11.  
  12. #validations
  13. validates_presence_of :username, :email, :subject, :body, :sender, :receiver
  14. validates_length_of :subject, :within => 10..70
  15. validates_length_of :body, :within => 10..1000
  16.  
  17. #state machine has been read message?
  18. state_machine :has_been_read, :initial => :unread do
  19. event :read_message do
  20. transition :from => :unread, :to => :read
  21. end
  22. event :mark_unread do
  23. transition :from => :read, :to => :unread
  24. end
  25. end
  26.  
  27. #state machine place_sender can be sent, draft, trash
  28. state_machine :place_sender, :initial => :draft do
  29. event :send_message do
  30. transition :from => :draft, :to => :sent
  31. end
  32. event :sender_send_to_trash do
  33. transition :from => :draft, :to => :drafts_trash
  34. end
  35. end
  36.  
  37. #state machine place_sender can be inbox, trash, spam
  38. state_machine :place_receiver, :initial => :in_box do
  39. event :receiver_send_to_trash do
  40. transition :from => :inbox, :to => :trash
  41. end
  42. event :receiver_send_to_spam do
  43. transition :from => :inbox, :to => :spam
  44. end
  45. end
  46. end
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement