Coder Social home page Coder Social logo

ciihla / mongoid_follow Goto Github PK

View Code? Open in Web Editor NEW

This project forked from appsbakery/mongoid_follow

0.0 2.0 2.0 165 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 Rails4 with Mongoid2

Inspired by

appsbakery/mongoid_follow

Installation

In Gemfile:

gem 'mongoid_follow', github: "ciihla/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) # is the same like  @bonnie.follow!(@clyde, "follow")
@bonnie.unfollow!(@clyde) # is the same like  @bonnie.unfollow!(@clyde, "follow")

You can specify different relations:

@bonnie.follow!(@clyde, "friends") #  @bonnie marked @clyde as friend

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

@clyde.follows?(@bonnie) # is the same like  @clyde.follows?(@bonnie, "follow")
@bonnie.followee?(@clyde) # is the same like  @bonnie.followee?(@clyde, "follow")

@clyde.follows?(@bonnie, "friends") # @clyde marked @bonnie as his friend?
@bonnie.followee?(@clyde, "friends")

You can also be a follower of other models

@gang = Group.create
@bonnie.follow!(@group) #  @bonnie.follow!(@group, "follow")

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

Getting followers/followees

Get all followers/followees by

@gang.all_followers # is the same like @gang.all_followers("follow")
@bonnie.all_followees # is the same like @bonnie.all_followees("follow")

You can also get followers/followees by a certain model

@gang.all_followers("follow", User)
@bonnie.all_followees("follow", Gang)

Specific queries

Get all users that I’m in a relation “friends” with:

current_user.followees.where(followee_type: "User", relation: "friends") # current user acts as follower. Others are followees

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

Example making friends with callbacks

In User model:

include Mongoid::Followee
include Mongoid::Follower

def after_follow(model)
  if model.follows?(self)
    self.follow!(model, "friends")
    model.follow!(self, "friends")
  end
end

def after_unfollow(model)
  self.unfollow!(model, "friends")
  model.unfollow!(self, "friends")
end

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

Thanks

Awesome thanks to:

mongoid_followable
Tristan Peralta

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

mongoid_follow's People

Contributors

alecguintu avatar ciihla avatar tagrudev avatar deepj avatar dminchev avatar

Watchers

 avatar James Cloos 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.