In Gemfile:
gem 'mongoid_follow'
"follower" is a model who follows "followee" is a model who would be followed
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)
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)
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)
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
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.
-
Any bug or issue, please send me an email to [email protected]
gem install 'mongoid' gem install 'bson_ext' gem install 'database_cleaner' gem install 'rspec' rake spec/specs/follow_spec_rb
-
limit result of followers/followees
Awesome thanks to:
mongoid_followable Tristan Peralta
Copyright © Alec Guintu. See LICENSE.txt for further details.