Sha256: 0b7bd8f81c58f61d8e8ca1c2d909cc7ddec6341fced2fba38139fb83f1cc0d61
Contents?: true
Size: 1.81 KB
Versions: 1
Compression:
Stored size: 1.81 KB
Contents
require 'state_machine' # Adds a generic implementation for sending messages between users module HasMessages extend ActiveSupport::Concern included do has_many :messages, :as => :sender, :class_name => 'Message', :conditions => {:hidden_at => nil}, :order => 'messages.id DESC' has_many :received_messages, :as => :receiver, :class_name => 'MessageRecipient', :include => :message, :conditions => ['message_recipients.hidden_at IS NULL AND messages.state = ?', 'sent'], :order => 'messages.id DESC' end # Composed messages that have not yet been sent. These consists of all # messages that are currently in the "unsent" state. def unsent_messages messages.with_state(:unsent) end # Composed messages that have already been sent. These consists of all # messages that are currently in the "queued" or "sent" states. def sent_messages messages.with_states(:queued, :sent) end # Returns the most recent message of each thread def last_received_message_per_thread MessageRecipient.find_all_by_receiver_id(id, :order => 'id desc', :joins => :message, :conditions => 'message_recipients.hidden_at is null', :group => 'COALESCE(original_message_id,messages.id)') end def conversations (messages + received_messages.map(&:message)).compact.uniq end def original_conversations conversations.select{ |message| message.original_message_id == nil } end def find_conversation_by_id(id) conversations.select{ |message| message.id == id.to_i }.first end def unread_messages received_messages.select(&:unread?).map(&:message) end end require 'has_messages/models/message.rb' require 'has_messages/models/message_recipient.rb'
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
has_messages_huacnlee-0.4.2 | lib/has_messages.rb |