wblog/app/models/comment.rb

25 lines
932 B
Ruby
Raw Permalink Normal View History

class Comment < ApplicationRecord
2014-03-29 21:59:15 +08:00
belongs_to :post
validates_presence_of :post_id
2014-03-29 21:59:15 +08:00
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
def reply_emails
2016-04-27 12:22:37 +08:00
Comment.where(post_id: self.post_id).collect(&:email).uniq - [ self.email ] - Subscribe.unsubscribe_list - [ ENV['ADMIN_USER'] ]
end
2016-04-27 12:22:37 +08:00
after_commit on: :create do
2016-04-27 14:47:04 +08:00
if ENV['MAIL_SERVER'].present? && ENV['ADMIN_USER'].present? && ENV['ADMIN_USER'] =~ /@/ && ENV['ADMIN_USER'] != self.email
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