Add like feature to any Active Record models through polymorphic association. Designate any models to act as a Liker or Likeable.
Installation
Add this line to your application's Gemfile:
gem 'acts_as_liked'
And then execute:
$ bundle
Run generator:
$ rails generate acts_as_liked
And don't forget to migrate your database
$ rake db:migrate
Usage
Likeable models
Add acts_as_likeable
to any models, and its instances can be liked by other models.
class Post < ActiveRecord::Base
acts_as_likeable
end
Liker models
Add acts_as_liker
to any models, and it can like instances of other models.
class User < ActiveRecord::Base
acts_as_liker
end
It is not necessary to use both acts_as_liker
and acts_as_likeable
. You can simply drop in one of them into your model and you will be good to go.
API
Following APIs will be provided to Likeable and Liker.
Likeable
# Count the number of likes of @post
@post.like_count
# Check if @post is liked by @user
@post.liked_by?(@user)
# Create a new Like record for @user, and @post
@post.liked_by(@user)
# Destroy the Like record
@post.unliked_by(@user)
Liker
# Create a new Like record for @user, and @post
@user.like(@post)
# Destroy the Like record
@user.unlike(@post)
# Check if @user has liked @post
@user.liked?(@post)
Contributing
Issues and pull reqeusts are welcomed.