Ecosyste.ms: Awesome

An open API service indexing awesome lists of open source software.

Awesome Lists | Featured Topics | Projects

https://github.com/sungwoncho/acts_as_liked

Add like feature to any Active Record models.
https://github.com/sungwoncho/acts_as_liked

Last synced: about 2 months ago
JSON representation

Add like feature to any Active Record models.

Awesome Lists containing this project

README

        

# ActsAsLiked [![Build Status](https://travis-ci.org/sungwoncho/acts_as_liked.svg?branch=master)](https://travis-ci.org/sungwoncho/acts_as_liked) [![Coverage Status](https://coveralls.io/repos/sungwoncho/acts_as_liked/badge.png?branch=master)](https://coveralls.io/r/sungwoncho/acts_as_liked?branch=master)

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:

```ruby
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.

```ruby
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.

```ruby
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*

```ruby
# 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*

```ruby
# 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.