Skip to content

Instantly share code, notes, and snippets.

@dgorodnichy
Last active June 14, 2020 19:22
Show Gist options
  • Save dgorodnichy/ca6c75ba3bc70afd931021dcceb061e7 to your computer and use it in GitHub Desktop.
Save dgorodnichy/ca6c75ba3bc70afd931021dcceb061e7 to your computer and use it in GitHub Desktop.
class ToggleLikeActionScript
Result = Struct.new(:success?, :errors, :value)
def initialize(user, post)
@user = user
@post = post
end
def perform
like = @post.likes.find_or_initialize_by(user: @user)
if like.persisted?
like.destroy!
Result.new(true, [], false)
else
like.save!
Result.new(true, [], true)
end
rescue StandardError => e
Result.new(false, [e], nil)
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment