GithubHelp home page GithubHelp logo

jcheng77 / mongoid_follow Goto Github PK

View Code? Open in Web Editor NEW

This project forked from alecguintu/mongoid_follow

0.0 2.0 0.0 23 KB

Add basic "follow" features to rails3 + mongoid

Home Page: https://github.com/alecguintu/mongoid_follow

License: MIT License

Ruby 100.00%

mongoid_follow's Introduction

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

  • limit result of followers/followees

Thanks

Awesome thanks to:

mongoid_followable
Tristan Peralta

Copyright © Alec Guintu. See LICENSE.txt for further details.

mongoid_follow's People

Contributors

alecguintu avatar tagrudev avatar jcheng77 avatar

Watchers

James Cloos avatar  avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.