alecguintu / mongoid_follow

Add basic "follow" features to rails3 + mongoid
https://github.com/alecguintu/mongoid_follow
MIT License
19 stars 16 forks source link

= Follow feature for Rails3 with Mongoid

== Installation

In Gemfile: gem 'mongoid_follow'

== Legend (to ease complications)

"follower" is a model who follows "followee" is a model who would be followed

== To use

To make mongoid_follow usable you need to include Mongoid::Followee into your document who would be followed then you also need to include Mongoid::Follower in your follower model:

class User include Mongoid::Document

include Mongoid::Followee
include Mongoid::Follower

end

class Group include Mongoid::Document

include Mongoid::Followee

end

You can then follow a model using:

@bonnie = User.create @clyde = User.create

@bonnie.follow(@clyde) @bonnie.unfollow(@clyde)

You can also see whether a model is a follower of another model or if a model is a followee of another model:

@clyde.follower?(@bonnie) @bonnie.followee?(@clyde)

You can also be a follower of other models

@gang = Group.create @bonnie.follow(@group)

@gang.follower?(@bonnie) @bonnie.follows?(@gang)

=== Getting followers/followees

Get all followers/followees by

@gang.all_followers @bonnie.all_followees

You can also get followers/followees by a certain model

@gang.all_followers_by_model(User) @bonnie.all_followees_by_model(Gang)

=== Counting followers/followees

Get the count of followers/followees using

@gang.followers_count @bonnie.followees_count

Or by a certain model by

@gang.followers_count_by_model(User) @bonnie.followees_count_by_model(User)

=== Dynamic methods

You can use dynamic methods to shorten code for getting followers/followees or their count of a certain model by

@gang.all_user_followers @bonnie.all_gang_followees

@gang.user_followers_count @bonnie.user_followees_count

== Callbacks

You can attach callbacks to the follower/followee models before or after the follow.

Follower model

def before_follow(followee) puts 'Notify me' end

Other follower callbacks

after_follow before_unfollow after_unfollow

Followee model

def before_followed_by(follower) puts 'Something here' end

Other followee callbacks

after_followed_by before_unfollowed_by after_unfollowed_by

Note: careful with using callbacks, we have no transaction so if it breaks on your callbacks, what gets saved is saved.

== For development

gem install 'mongoid' gem install 'bson_ext' gem install 'database_cleaner' gem install 'rspec'

rake spec/specs/follow_spec_rb

== TODO

== Thanks

Awesome thanks to: mongoid_followable Tristan Peralta

== Copyright

Copyright (c) Alec Guintu. See LICENSE.txt for further details.