Created
September 6, 2017 10:37
-
-
Save bhalash/fac3282a48c6ed6a96a8db994ccec5e8 to your computer and use it in GitHub Desktop.
Like/Dislike Model Example Code
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
# Add given object to self's liked objects. | |
# | |
# @example | |
# | |
# @user.like Post.first | |
# @user.public_send('liked_posts') << Post.first | |
# @uuse.liked_posts << Post.first | |
# | |
# @param obj [ApplicationRecord::Likeable] Any likeable record. | |
def like(obj) | |
public_send("liked_#{obj.class.name.tabelize}") << obj | |
end | |
# Remove object from self's liked objects. | |
# | |
# @example | |
# | |
# @user.unlike Post.first | |
# @user.public_send('liked_posts').delete Post.first | |
# @uuse.liked_posts.delete Post.first | |
# | |
# @param obj [ApplicationRecord::Likeable] Any likeable record. | |
def unlike(obj) | |
public_send("liked_#{obj.class.name.tabelize}").delete obj | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment