Giter VIP home page Giter VIP logo

mongo_followable's Introduction

mongo_followable

Now works for both Mongoid and Mongo_Mapper!

Installation

In console:

gem install mongo_followable

or in Gemfile:

gem 'mongo_followable'

Notice

Please read following documentation first. Since 0.3.2, some apis have been changed. Sorry for the inconvenience.

If you want to remove ‘follow_history` and `followed_history` fields totally from your database after you decide not to use follow/followed history feature, do this:

# in the rails console, taking user as an example:
User.all.each { |u| u.unset(:follow_history) } # this will remove the follow_history field

Usage

To make model followable you need to include Mongo::Followable into your model; You also need to include Mongo::Follower in your follower model:

class User
  include Mongoid::Document  #for Mongo_Mapper users, this line of code should be include MongoMapper::Document
  include Mongo::Followable::Followed
  include Mongo::Followable::Follower
  include Mongo::Followable::History # you have to add this line to enable follow/followed history
end

class Group
  include Mongoid::Document  #for Mongo_Mapper users, this line of code should be include MongoMapper::Document
  include Mongo::Followable::Followed
  include Mongo::Followable::History # you have to add this line to enable follow/followed history
end

I’ve decided to remove authorization because it is quite inefficient to keep this field for every record in the database. However, it’s possible that I’ll add it back as a plugin in the future.

And then you can follow and unfollow:

@group = Group.new
@group.save

current_user.follow(@group)
current_user.unfollow(@group)
current_user.unfollow_all

current_user.follow(*array_of_objects_to_follow) # follow an array of objects
current_user.unfollow(*array_of_objects_to_follow) # unfollow

or,

@group.unfollowed(current_user)
@group.unfollowed_all

It’s also possible to pass a block:

current_user.follow(u1, u2, u3, u4...) { |user| user.name == 'Jeremy Lin' }
current_user.unfollow(u1, u2, u3, u4...) { |user| user.followee_of? @kobe_bryant }

@group.unfollowed(u1, u2, u3...) { |user| user.ever_follow.include? @some_user }

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

current_user.follower_of?(@group)
current_user.followee_of?(@group)

or whether a model is following some other model and vice versa:

current_user.following?
@group.followed?

Moreover, it’s easy to get a model’s follower/followee count:

current_user.followers_count
current_user.followees_count

Of course, you can get a list of followers/followees:

	
User.followers_of(@group)
User.followees_of(@group)

@group.all_followers
@user.all_followees

Getting a model’s followers/followees by type is also possible:

@group.followers_by_type("user")
@user.followees_by_type("group")

Dealing with model names:

@group.followers_by_type("user")
@group.followers_by_type("User")
@group.followers_by_type("user_post") # both are fine
@user.followees_by_type("GroupPost")

And their count:

@group.followers_by_type("user")
@group.followers_count_by_type("user")
@user.followees_by_type("group")
@user.followees_count_by_type("group")

You can also get a model’s follow/followed history:

@user.ever_follow
@group.ever_followed

or to tell if ever follow/followed by someone:

@user.ever_follow? @some_group
@group.ever_followed? @some_user

Sure you can clear the histories:

@user.clear_history!

#or more specific:

@user.clear_follow_history!
@group.clear_followed_history!

Another feature is to get a list of models which has the most followers/followees:

User.with_max_followees
User.with_min_followees
User.with_max_followees_by_type('group')
User.with_min_followees_by_type('group')
Group.with_max_followers
Group.with_min_followers
Group.with_max_followers_by_type('user')
Group.with_min_followers_by_type('user')

Now you can tell if two models have some common followers/followees by following methods:

@user.common_followees?(@another_user)
@user.common_followers?(@group)

And see what the common followers/followees are:

@user.common_followees_with(@another_user)
@user.common_followers_with(@group)
  • Any bug or issue, please send me an email: [email protected]

    include Mongo::Followable::History # you have to add this line to enable follow/followed history
    

TODO

  • inter-models followable #FINISHED#

  • divide into two parts: followable(being followed) and follower(following others) #FINISHED#

  • following history/followed history #FINISHED#

  • most/least followed/following #FINISHED

  • add authorization to followable models #FINISHED#

  • common followers/followees #FINISHED#

  • add support for mongo_mapper in next version #FINISHED#

  • implement plugins: confirmation, authorization etc.

!!If you have any advice, plese do not hesitate to tell me!!

Thanks

Thanks the author(s) of acts_as_followable, you can find this gem here

Thanks the author(s) of voteable_mongo, you can find this gem here

Copyright © Jie Fan. See LICENSE.txt for further details.

mongo_followable's People

Contributors

cinerama avatar lastomato avatar ryw avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar

mongo_followable's Issues

How to paginate?

The all_followers and all_followees methods seem very inefficient where there are lots of records, and since they return arrays they can't be paginated using standard Rails pagination gems.

Deleted documents

When A follow B, and then B is deleted.
There will be a mongoid document not found error when we call A.all_followees

stack level too deep

I'm getting a stack level too deep error when using the all_followers and followers_count methods.

Callbacks on follow

I'm using mongo_followable to handle not only following but favorites in my app. For both favorites and follow I use Mongoid::Observer to send notifications to users when each happens.

What I've found is it's kind of clunky. For example in order to send a notification to a User is being followed I do the following...

  def after_save(follow)
    if follow.f_type = "User" and follow.following_id_changed? and follow.following_id_was == nil
      followed = User.find(follow.f_id)
      followed.notify(:follow,
        actor: follow.following
      ) if followed
    end
  end

One thing that kind of threw me off was I wasn't sure why there are two follow documents? I thought it was for bidirectional following but it looks like when both users follow each other there are 4 documents.

The other thing I'm also wondering is if I could have favorites be in another collection. I feel like mixing them in one collection isn't optimal.

f_type capitalize problem

Thank you for a great gem.

I'm using your gem with models like "Post" and "ChildPost".
In this case, for inherited model name, rebuild_instances() fails for "ChildPost".

How about just call constantize() without capitalize() ?

Since scope :by_type also calls type.capitalize(), it fails for "ChildPost" again.

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.