Sha256: 94d72d3ba0ac4486a0eea2be31077b917015b1c05c8ad38b03a08961df24465c

Contents?: true

Size: 1.89 KB

Versions: 18

Compression:

Stored size: 1.89 KB

Contents

# -*- encoding: utf-8 -*-
class Message < ActiveRecord::Base
  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

  def self.per_page
    10
  end

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

  def send_notification
    Notifier.delay.message_notification(self) 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
#  created_at         :datetime
#  updated_at         :datetime
#  message_request_id :integer
#  state              :string(255)
#  parent_id          :integer
#  lft                :integer
#  rgt                :integer
#

Version data entries

18 entries across 18 versions & 1 rubygems

Version Path
enju_message-0.1.3 app/models/message.rb
enju_message-0.1.2 app/models/message.rb
enju_message-0.0.19 app/models/message.rb
enju_message-0.1.1 app/models/message.rb
enju_message-0.1.0 app/models/message.rb
enju_message-0.0.18 app/models/message.rb
enju_message-0.0.17 app/models/message.rb
enju_message-0.0.16 app/models/message.rb
enju_message-0.0.15 app/models/message.rb
enju_message-0.0.14 app/models/message.rb
enju_message-0.0.13 app/models/message.rb
enju_message-0.0.12 app/models/message.rb
enju_message-0.0.11 app/models/message.rb
enju_message-0.0.10 app/models/message.rb
enju_message-0.0.9 app/models/message.rb
enju_message-0.0.5 app/models/message.rb
enju_message-0.0.4 app/models/message.rb
enju_message-0.0.3 app/models/message.rb