move user unread message count into a scope

This commit is contained in:
joshua stein 2015-04-03 12:57:12 -05:00
parent 97c70d3efc
commit fdf892e7f9
2 changed files with 6 additions and 2 deletions

View file

@ -14,6 +14,9 @@ class Message < ActiveRecord::Base
validates_length_of :subject, :in => 1..150
validates_length_of :body, :maximum => (64 * 1024)
scope :unread, -> { where(:has_been_read => false,
:deleted_by_recipient => false) }
before_validation :assign_short_id,
:on => :create
after_create :deliver_email_notifications
@ -35,6 +38,8 @@ class Message < ActiveRecord::Base
end
def deliver_email_notifications
return if Rails.env.development?
if self.recipient.email_messages?
begin
EmailMessage.notify(self, self.recipient).deliver

View file

@ -259,8 +259,7 @@ class User < ActiveRecord::Base
def update_unread_message_count!
Keystore.put("user:#{self.id}:unread_messages",
Message.where("recipient_user_id = ? AND (has_been_read = ? AND " <<
"deleted_by_recipient = ?)", self.id, false, false).count)
self.received_messages.unread.count)
end
def votes_for_others