wblog/app/models/comment.rb

32 lines
996 B
Ruby
Raw Normal View History

2012-06-25 00:09:44 +08:00
class Comment
include Mongoid::Document
include Mongoid::Timestamps
2014-03-29 21:59:15 +08:00
field :name, :type => String
2012-06-25 00:09:44 +08:00
field :content, :type => String
2014-03-29 21:59:15 +08:00
field :email, :type=>String
belongs_to :post
validates :name, presence: true
2015-04-07 01:19:09 +08:00
validates :email, presence: true, format: { with: /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i, message: I18n.t('comment_attributes.email') }
2012-06-25 00:09:44 +08:00
validates :content, presence: true
2014-03-30 22:14:59 +08:00
validates_presence_of :post_id
def reply_emails
Comment.where(post_id: self.post_id).collect(&:email).uniq - [ self.email ] - Subscribe.unsubscribe_list
end
after_create do
if ENV['MAIL_SERVER'].present? && ENV['ADMIN_USER'].present? && ENV['ADMIN_USER'] =~ /@/
2014-04-03 00:23:33 +08:00
Rails.logger.info 'comment created, comment worker start'
NewCommentWorker.perform_async(self.id.to_s, ENV['ADMIN_USER'])
end
if ENV['MAIL_SERVER'].present?
Rails.logger.info 'comment created, reply worker start'
NewReplyPostWorker.perform_async(self.id.to_s)
end
end
2012-06-25 00:09:44 +08:00
end