Files
growstuff/app/models/notification.rb
Cjay Billones bb88041fef [Refactoring] Replace .count and .length with .size for collections
Improved program efficiency (in querying in particular) by replacing .count and .length with .size for collections.

Fix error in refactoring

Reverted some .size into .count
2015-06-25 12:09:03 +08:00

29 lines
615 B
Ruby

class Notification < ActiveRecord::Base
belongs_to :sender, :class_name => 'Member'
belongs_to :recipient, :class_name => 'Member'
belongs_to :post
default_scope { order('created_at DESC') }
scope :unread, -> { where(:read => false) }
before_create :replace_blank_subject
after_create :send_email
def self.unread_count
self.unread.size
end
def replace_blank_subject
if self.subject.nil? or self.subject =~ /^\s*$/
self.subject = "(no subject)"
end
end
def send_email
if self.recipient.send_notification_email
Notifier.notify(self).deliver
end
end
end