Skip to content

Instantly share code, notes, and snippets.

@cutalion
Last active May 8, 2019 20:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save cutalion/4b2860f3ab0f03d9ce92c09efb6ee7ad to your computer and use it in GitHub Desktop.
Save cutalion/4b2860f3ab0f03d9ce92c09efb6ee7ad to your computer and use it in GitHub Desktop.
# Пусть админы могут удалять комментарии
# и мы уведомляем кого-нибудь, что комментарий был удален
class DeleteComment
def call(comment)
transaction do
comment.delete!
user.decrement(:comments_count)
end
send_push 'Comment deleted'
send_email 'Comment deleted'
end
end
# Потом у нас появляется бан пользователей
# и нам надо удалить все их комментарии
# (опустим, что юзер получит сразу пачку уведомлений)
class BanUser
def call(user)
transaction do
user.ban!
user.comments.each do |comment|
DeleteComment.new.call(comment)
end
end
send_email 'Banned!'
end
end
# api
# DeleteComment может вызываться, как напрямую, так и опосредованно
delete '/comments/:id' do
comment = find_comment(params[:id])
DeleteComment.new.call(comment)
end
post '/users/:id/ban' do
user = find_user(params[:id])
BanUser.new.call(user)
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment