Coder Social home page Coder Social logo

syd / has_friendship Goto Github PK

View Code? Open in Web Editor NEW

This project forked from has-friendship/has_friendship

0.0 2.0 0.0 140 KB

A Rails plugin to add friendship features to models

License: MIT License

Ruby 87.65% JavaScript 1.20% CSS 1.37% HTML 9.79%

has_friendship's Introduction

HasFriendship Build Status Coverage Status

Add social network friendship feature to your Active Record models.

HasFriendship allows objects in a model to send, accept, and decline friend requests using self-refernetial polymorphic association.

Getting started

Add HasFriendship to your Gemfile:

gem 'has_friendship'

After you install HasFriendship, you need to run the generator:

$ rails generate has_friendship

The generator will copy a migration that creates friendships table. Run the migration to finish the setup.

$ rake db:migrate

Usage

Simply drop in has_friendship to a model:

class User < ActiveRecord::Base
  has_friendship
end

Friend request

Now, User instances can send, accept, and decline friend requests:

@mac = User.create(name: "Mac")
@dee = User.create(name: "Dee")

# @mac sends a friend request to @dee
@mac.friend_request(@dee)

# @dee can accept the friend request
@dee.accept_request(@mac)

# @dee can also decline the friend request
@dee.decline_request(@mac)

A Friendship can also be removed:

# @dee removes @mac from its friends
@dee.remove_friend(@mac)

Type of friends

There are three types of friends. They can be accessed using association:

  • requested_friends

Instances that sent friend request that has not been accepted yet.

@mac.friend_request(@dee)

@dee.requested_friends # => [@mac]
  • pending_friends

Instances that received but has not accepted the friend request yet.

@mac.friend_request(@dee)

@mac.pending_friends # => [@dee]
  • friends

Instances with accepted Friendship.

@mac.friend_request(@dee)
@dee.accpet_request(@mac)

@mac.friends # => [@dee]
@dee.friends # => [@mac]

Contributing

Issues and pull reqeusts are welcomed.

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.