Skip to content

Instantly share code, notes, and snippets.

@bhalash
Created September 6, 2017 10:37
Show Gist options
  • Save bhalash/fac3282a48c6ed6a96a8db994ccec5e8 to your computer and use it in GitHub Desktop.
Save bhalash/fac3282a48c6ed6a96a8db994ccec5e8 to your computer and use it in GitHub Desktop.
Like/Dislike Model Example Code
# 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