foodsoft/lib/foodsoft_messages/app/models/message.rb

93 lines
2.6 KiB
Ruby
Raw Normal View History

2009-01-06 11:49:19 +01:00
class Message < ActiveRecord::Base
belongs_to :sender, :class_name => "User", :foreign_key => "sender_id"
serialize :recipients_ids, Array
attr_accessor :sent_to_all, :group_id, :recipient_tokens, :reply_to
2009-01-06 11:49:19 +01:00
scope :pending, -> { where(:email_state => 0) }
scope :sent, -> { where(:email_state => 1) }
2014-11-21 14:37:56 +01:00
scope :pub, -> { where(:private => false) }
2009-01-06 11:49:19 +01:00
# Values for the email_state attribute: :none, :pending, :sent, :failed
EMAIL_STATE = {
:pending => 0,
:sent => 1,
:failed => 2
2009-01-06 11:49:19 +01:00
}
validates_presence_of :recipients_ids, :subject, :body
2009-01-06 11:49:19 +01:00
validates_length_of :subject, :in => 1..255
validates_inclusion_of :email_state, :in => EMAIL_STATE.values
2011-05-11 11:17:02 +02:00
before_validation :clean_up_recipient_ids, :on => :create
def self.deliver(message_id)
find(message_id).deliver
end
2011-05-11 11:17:02 +02:00
def clean_up_recipient_ids
self.recipients_ids = recipients_ids.uniq.reject { |id| id.blank? } unless recipients_ids.nil?
self.recipients_ids = User.all.collect(&:id) if sent_to_all == "1"
end
def add_recipients(users)
self.recipients_ids = [] if recipients_ids.blank?
self.recipients_ids += users.collect(&:id) unless users.blank?
end
def group_id=(group_id)
@group_id = group_id
add_recipients Group.find(group_id).users unless group_id.blank?
2009-01-06 11:49:19 +01:00
end
def recipient_tokens=(ids)
@recipient_tokens = ids
add_recipients ids.split(",").collect { |id| User.find(id) }
2009-01-06 11:49:19 +01:00
end
def reply_to=(message_id)
@reply_to = Message.find(message_id)
add_recipients([@reply_to.sender])
self.subject = I18n.t('messages.model.reply_subject', :subject => @reply_to.subject)
self.body = I18n.t('messages.model.reply_header', :user => @reply_to.sender.display, :when => I18n.l(@reply_to.created_at, :format => :short)) + "\n"
@reply_to.body.each_line{ |l| self.body += I18n.t('messages.model.reply_indent', :line => l) }
2009-01-06 11:49:19 +01:00
end
def mail_to=(user_id)
user = User.find(user_id)
2012-08-17 19:08:02 +02:00
add_recipients([user])
end
2014-06-18 13:28:19 +02:00
# Returns true if this message is a system message, i.e. was sent automatically by Foodsoft itself.
2009-01-06 11:49:19 +01:00
def system_message?
self.sender_id.nil?
end
def sender_name
system_message? ? I18n.t('layouts.foodsoft') : sender.display rescue "?"
end
def recipients
User.find(recipients_ids)
end
2009-01-06 11:49:19 +01:00
2012-08-17 19:08:02 +02:00
def deliver
for user in recipients
if user.receive_email?
2012-08-17 19:08:02 +02:00
begin
2014-03-07 09:51:24 +01:00
MessagesMailer.foodsoft_message(self, user).deliver
rescue
Rails.logger.warn "Deliver failed for user \##{user.id}: #{user.email}"
2012-08-17 19:08:02 +02:00
end
end
end
update_attribute(:email_state, 1)
end
def is_readable_for?(user)
!private || sender == user || recipients_ids.include?(user.id)
end
2009-01-06 11:49:19 +01:00
end