Sha256: a62399170cf3bca64a454f987c2df3949123eebe89c979e0dd6219f468a61cf5

Contents?: true

Size: 1.19 KB

Versions: 2

Compression:

Stored size: 1.19 KB

Contents

require 'ancestry'

module ActsAsMessageable
  class Message < ::ActiveRecord::Base
    include ActsAsMessageable::Scopes

    belongs_to :received_messageable, :polymorphic => true
    belongs_to :sent_messageable,     :polymorphic => true

    attr_accessor   :removed, :restored
    cattr_accessor  :required

    ActsAsMessageable.rails_api.new(self).default_scope("created_at desc")

    def open?
      self.opened?
    end

    def open
      update_attributes!(:opened => true)
    end
    alias :mark_as_read :open
    alias :read         :open

    def close
      update_attributes!(:opened => false)
    end
    alias :mark_as_unread :close
    alias :unread         :close

    alias :from :sent_messageable
    alias :to   :received_messageable

    def real_receiver(user)
      user == from ? to : from
    end

    def participant?(user)
      user.class.group_messages || (to == user) || (from == user)
    end

    def conversation
      root.subtree
    end

    def delete
      self.removed = true
    end

    def restore
      self.restored = true
    end

    def reply(*args)
      to.reply_to(self, *args)
    end

    def people
      conversation.map{ |x| x.from }.uniq!
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
acts-as-messageable-0.4.10 lib/acts-as-messageable/message.rb
acts-as-messageable-0.4.9 lib/acts-as-messageable/message.rb