Last active
June 14, 2020 19:22
-
-
Save dgorodnichy/ca6c75ba3bc70afd931021dcceb061e7 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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