Sha256: 361aae01d6a1ec455c3d0d80cddd45ef7e597a4849721fb7fe5b8dfdaa3b6d5d

Contents?: true

Size: 2 KB

Versions: 12

Compression:

Stored size: 2 KB

Contents

# -*- encoding: utf-8 -*-
class Message < ActiveRecord::Base
  attr_accessible :subject, :body, :sender, :recipient
  scope :unread, where(:state => 'unread')
  belongs_to :message_request
  belongs_to :sender, :class_name => 'User'
  belongs_to :receiver, :class_name => 'User'
  validates_presence_of :subject, :body #, :sender
  validates_presence_of :recipient, :on => :create
  validates_presence_of :receiver, :on => :update
  before_save :set_receiver
  after_save :index
  after_destroy :remove_from_index
  after_create :send_notification

  acts_as_nested_set
  attr_accessor :recipient

  state_machine :initial => :unread do
    before_transition any => :read, :do => :read
    before_transition :read => :unread, :do => :unread

    event :sm_read do
      transition any => :read
    end

    event :sm_unread do
      transition :read => :unread
    end
  end

  searchable do
    text :body, :subject
    string :subject
    integer :receiver_id
    integer :sender_id
    time :created_at
    boolean :is_read do
      self.read?
    end
  end

  paginates_per 10

  def set_receiver
    if self.recipient
      self.receiver = User.find(self.recipient)
    end
  end

  def send_notification
    Notifier.message_notification(id).deliver if receiver.try(:email).present?
  end

  def read
    self.read_at = Time.zone.now unless self.read_at
    self.save(:validate => false)
  end

  def read?
    return true if state == 'read'
    false
  end
end

# == Schema Information
#
# Table name: messages
#
#  id                 :integer          not null, primary key
#  read_at            :datetime
#  receiver_id        :integer
#  sender_id          :integer
#  subject            :string(255)      not null
#  body               :text
#  message_request_id :integer
#  state              :string(255)
#  parent_id          :integer
#  created_at         :datetime         not null
#  updated_at         :datetime         not null
#  lft                :integer
#  rgt                :integer
#  depth              :integer
#

Version data entries

12 entries across 12 versions & 1 rubygems

Version Path
enju_message-0.1.14.pre14 app/models/message.rb
enju_message-0.1.14.pre13 app/models/message.rb
enju_message-0.1.14.pre12 app/models/message.rb
enju_message-0.1.14.pre11 app/models/message.rb
enju_message-0.1.14.pre10 app/models/message.rb
enju_message-0.1.14.pre9 app/models/message.rb
enju_message-0.1.14.pre8 app/models/message.rb
enju_message-0.1.14.pre7 app/models/message.rb
enju_message-0.1.14.pre6 app/models/message.rb
enju_message-0.1.14.pre5 app/models/message.rb
enju_message-0.1.14.pre4 app/models/message.rb
enju_message-0.1.14.pre3 app/models/message.rb