Sha256: 675fdd74675a2b8304eb7fbe6bf34eb29e6d18f7ac684f77ea1e99d67f1044f5

Contents?: true

Size: 1.68 KB

Versions: 4

Compression:

Stored size: 1.68 KB

Contents

module UsersMessages
  
  def send_message?(options)
    send_message(options)
    true                        
  rescue Exception => e
    false
  end 

  def send_message(options)
    unless options[:recipients].nil?
      transaction do
        recipients = options[:recipients]
        options.delete(:recipients)

        options[:subject_id] = Message.sequence_subject_id if options[:subject_id].nil?

        recipients.each do |rec|
          # => create message copies
          options[:user_id] = self.id
          options[:sender_id] = self.id
          options[:recipient_id] = rec.id
          options[:copies] = true
          Message.create(options)
          # => create message
          options[:user_id] = rec.id 
          options[:sender_id] = self.id
          options[:recipient_id] = rec.id
          options[:copies] = false
          options[:parent_id] = Message.next_parent_id(options[:parent_id]) unless options[:parent_id].nil?
          Message.create(options)
        end
      end
    else
      raise "Required recipients"
    end
  rescue Exception => e
    raise e 
  end
  
  def inbox(options = {})
    options[:deleted] = false
    options[:copies] = false
    self.messages.where(options)
  end

  def outbox(options = {})
    options[:deleted] = false
    options[:copies] = true
    self.messages.where(options)
  end

  def trash(options = {})
    options[:deleted] = true
    self.messages.where(options)
  end

  def empty_messages(options = {})
    if options.empty? or options[:inbox] or options[:outbox]
      self.inbox.update_all(:deleted => true)
      self.outbox.update_all(:deleted => true)
    elsif options[:trash]
      self.trash.delete_all
    end
  end
  
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
fetty-generators-2.0.4 lib/generators/fetty/messages/templates/lib/users_messages.rb
fetty-generators-2.0.3 lib/generators/fetty/messages/templates/lib/users_messages.rb
fetty-generators-2.0.1 lib/generators/fetty/messages/templates/lib/users_messages.rb
fetty-generators-2.0.0 lib/generators/fetty/messages/templates/lib/users_messages.rb