Move code for sending the messages from model to MessageNotifier

This commit is contained in:
Patrick Gansterer 2017-08-18 00:45:17 +02:00
parent add281c738
commit 3f7efbc4b9
2 changed files with 13 additions and 18 deletions

View file

@ -28,10 +28,6 @@ class Message < ActiveRecord::Base
before_create :create_salt before_create :create_salt
before_validation :clean_up_recipient_ids, :on => :create before_validation :clean_up_recipient_ids, :on => :create
def self.deliver(message_id)
find(message_id).deliver
end
def clean_up_recipient_ids def clean_up_recipient_ids
add_recipients Group.find(group_id).users unless group_id.blank? add_recipients Group.find(group_id).users unless group_id.blank?
self.recipients_ids = recipients_ids.uniq.reject { |id| id.blank? } unless recipients_ids.nil? self.recipients_ids = recipients_ids.uniq.reject { |id| id.blank? } unless recipients_ids.nil?
@ -91,19 +87,6 @@ class Message < ActiveRecord::Base
Message.where(reply_to: self.id).order(:created_at).last Message.where(reply_to: self.id).order(:created_at).last
end end
def deliver
for user in recipients
if user.receive_email?
begin
MessagesMailer.foodsoft_message(self, user).deliver
rescue
Rails.logger.warn "Deliver failed for user \##{user.id}: #{user.email}"
end
end
end
update_attribute(:email_state, 1)
end
def is_readable_for?(user) def is_readable_for?(user)
!private || sender == user || recipients_ids.include?(user.id) !private || sender == user || recipients_ids.include?(user.id)
end end

View file

@ -3,6 +3,18 @@ class MessageNotifier < UserNotifier
def self.message_deliver(args) def self.message_deliver(args)
message_id = args.first message_id = args.first
Message.find(message_id).deliver message = Message.find(message_id)
message.recipients.each do |recipient|
if recipient.receive_email?
begin
MessagesMailer.foodsoft_message(message, recipient).deliver
rescue
Rails.logger.warn "Deliver failed for user \##{recipient.id}: #{recipient.email}"
end
end
end
message.update_attribute(:email_state, 1)
end end
end end