Popular
Popular is a friendship gem designed for Rails/ActiveRecord models.
Installation
Add this line to your application's Gemfile:
gem 'popular'
And then execute:
$ bundle
Or install it yourself as:
$ gem install popular
Database Migration
Popular uses a friendships table to store friendship relationships. To get up and running, use the following command:
rails g popular:migration
rake db:migrate
Usage
Model
To get started using Popular, simply add popular
to your model, (ie: app/models/user.rb
)
class User < ActiveRecord::Base
popular
end
@sam = User.create name: "Samuel"
@jackson = User.create name: "Jackson"
# Adding and removing friends
@sam.friends_with? @jackson #=> false
@sam.friends.length #=> 0
@sam.befriend @jackson
@sam.friends_with? @jackson #=> true
@sam.friends.length #=> 1
@sam.unfriend @jackson
@sam.friends_with? @jackson #=> false
Callbacks
Popular provides callbacks that are fired around friendship creation. Available callbacks are:
- after_befriend
- before_befriend
- after_unfriend
- before_unfriend
class User < ActiveRecord::Base
popular
after_befriend :notify_friendship_created
after_unfriend :notify_unfriended
def notify_friendship_created
puts "Friendship created successfully"
end
def notify_unfriended
puts ":("
end
end
@justin = User.create name: "Justin"
@jenny = User.create name: "Jenny"
@justin.befriend @jenny #=> "Friendship created successfully"
@justin.unfriend @jenny #=> ":("
Contributing
- Fork it ( http://github.com/thejchap/popular/fork )
- Create your feature branch (
git checkout -b my-new-feature
) - Commit your changes (
git commit -am 'Add some feature'
) - Push to the branch (
git push origin my-new-feature
) - Create new Pull Request