Skip to content

Instantly share code, notes, and snippets.

@marclove
Last active February 2, 2018 19:47
Show Gist options
  • Save marclove/9378706 to your computer and use it in GitHub Desktop.
Save marclove/9378706 to your computer and use it in GitHub Desktop.
class CommentsController
def create
comment = Comment.new(params[:comment])
@comment = CommentCreator.new(comment).create!
respond_with @comment
end
end
class CommentCreator
def initialize(comment)
@comment = comment
end
def create!
@comment.save!
CommentCreatedWorker.perform_async(@comment.id)
@comment
end
def perform
ThreadParticipantsNotifier.new(@comment).notify
CommentReplyNotifier.new(@comment).notify
Kissmetrics.record(@comment.author, 'Added Comment')
end
end
class CommentCreatedWorker
include Sidekiq::Worker
def perform(comment_id)
comment = Comment.find(comment_id)
CommentCreator.new(comment).perform
end
end
class ThreadParticipantsNotifier
def initialize(comment)
@comment = comment
end
def notify
participants.each { |participant| notify_participant(participant) }
end
def notify_participant(participant)
CommentThreadMailer.activity_notification(comment_thread, @comment, participant)
end
private
def participants
comment_thread.participants
end
def comment_thread
@comment.comment_thread
end
end
class CommentReplyNotifier
def initialize(comment)
@comment = comment
end
def notify
if previous_comment?
ReplyMailer.notification(in_reply_to_user, @comment).deliver
end
end
private
def previous_comment?
!!previous_comment
end
def previous_comment
@comment.previous_comment
end
def in_reply_to_user
previous_comment.author
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment