2016-04-20 22:29:29 +08:00
|
|
|
class Comment < ApplicationRecord
|
2014-03-29 21:59:15 +08:00
|
|
|
belongs_to :post
|
2016-04-20 22:29:29 +08:00
|
|
|
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
|
2014-04-02 22:06:27 +08:00
|
|
|
|
2014-04-07 22:56:21 +08:00
|
|
|
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'] ]
|
2014-04-07 22:56:21 +08:00
|
|
|
end
|
|
|
|
|
2016-04-27 12:22:37 +08:00
|
|
|
after_commit on: :create do
|
2014-05-25 00:54:52 +08:00
|
|
|
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'
|
2014-05-25 00:54:52 +08:00
|
|
|
NewCommentWorker.perform_async(self.id.to_s, ENV['ADMIN_USER'])
|
2014-04-02 22:06:27 +08:00
|
|
|
end
|
2014-04-07 22:56:21 +08:00
|
|
|
|
2014-05-25 00:54:52 +08:00
|
|
|
if ENV['MAIL_SERVER'].present?
|
2014-04-07 22:56:21 +08:00
|
|
|
Rails.logger.info 'comment created, reply worker start'
|
2014-05-25 00:54:52 +08:00
|
|
|
NewReplyPostWorker.perform_async(self.id.to_s)
|
2014-04-07 22:56:21 +08:00
|
|
|
end
|
2014-04-02 22:06:27 +08:00
|
|
|
end
|
2012-06-25 00:09:44 +08:00
|
|
|
end
|