All Projects → sungwoncho → Has_friendship

sungwoncho / Has_friendship

Licence: mit
Add friendship to ActiveRecord models

Programming Languages

ruby
36898 projects - #4 most used programming language

Projects that are alternatives of or similar to Has friendship

Annict
The platform for anime addicts built with Rails and Stimulus.js.
Stars: ✭ 542 (+189.84%)
Mutual labels:  rails, social-network
Diaspora
A privacy-aware, distributed, open source social network.
Stars: ✭ 12,937 (+6818.18%)
Mutual labels:  rails, social-network
Brevidy
A video social network built with Ruby on Rails, HAML, Bootstrap, and jQuery.
Stars: ✭ 220 (+17.65%)
Mutual labels:  rails, social-network
Social Share Button
Helper for add social share feature in your Rails app. Twitter, Facebook, Weibo, Douban ...
Stars: ✭ 567 (+203.21%)
Mutual labels:  rails, social-network
Socify
🚀 Socify is an open source social networking platform written in Ruby on Rails
Stars: ✭ 363 (+94.12%)
Mutual labels:  rails, social-network
Stories
Medium clone built with Ruby on Rails
Stars: ✭ 688 (+267.91%)
Mutual labels:  rails, social-network
Bootcamp
An enterprise social network
Stars: ✭ 2,110 (+1028.34%)
Mutual labels:  social-network
Honeybadger Ruby
Ruby gem for reporting errors to honeybadger.io
Stars: ✭ 182 (-2.67%)
Mutual labels:  rails
Form core
A Rails engine providing ability to generate dynamic form.
Stars: ✭ 175 (-6.42%)
Mutual labels:  rails
Pump.io
Social server with an ActivityStreams API
Stars: ✭ 2,091 (+1018.18%)
Mutual labels:  social-network
Activitystreams
Activity Streams 2.0
Stars: ✭ 185 (-1.07%)
Mutual labels:  social-network
Ruby2 Rails4 Bootstrap Heroku
A starter application based on Ruby 2.4, Rails 4.2 and Bootstrap for Sass 3, deployable on Heroku
Stars: ✭ 181 (-3.21%)
Mutual labels:  rails
Wei
微信服务号裂变引擎,提供一套简单明了的DSL,快速配置和接入服务号裂变。 wechat fission platform, for technological dimensionality reduction.
Stars: ✭ 177 (-5.35%)
Mutual labels:  rails
The construct
A Modern Rails Template
Stars: ✭ 183 (-2.14%)
Mutual labels:  rails
Rspec Rails Examples
RSpec cheatsheet & Rails app: Learn how to expertly test Rails apps from a model codebase
Stars: ✭ 2,089 (+1017.11%)
Mutual labels:  rails
Saml idp
Ruby SAML Identity Provider, best used with Rails (though not required)
Stars: ✭ 184 (-1.6%)
Mutual labels:  rails
Graphrec Www19
Graph Neural Networks for Social Recommendation, WWW'19
Stars: ✭ 173 (-7.49%)
Mutual labels:  social-network
Eshop Prices
Nintendo Switch - Full game list with prices
Stars: ✭ 179 (-4.28%)
Mutual labels:  rails
Ooor
Odoo Ruby JSON client. Emulates ActiveRecord enough (as much as Mongoid; Implements ActiveModel) to make Rails development with an Odoo datastore straightforward
Stars: ✭ 184 (-1.6%)
Mutual labels:  rails
Awesome Blogs
한국에 있는 좋은 개발자들의 블로그들을 편리하게 구독할 수 있도록 하나의 주소로 묶어서 RSS 피드로 제공해줍니다.
Stars: ✭ 178 (-4.81%)
Mutual labels:  rails

HasFriendship Build Status Coverage Status

Add friendship features to your ActiveRecord models.

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

Getting started

Add HasFriendship to your Gemfile:

gem 'has_friendship'

After you bundle HasFriendship, you need to copy migrations and migrate:

$ rails has_friendship_engine:install:migrations
$ rake db:migrate

Gem upgrades

After gem updates, it may be necessary to run subsequent migrations.

$ rails has_friendship_engine:install:migrations

Will install new migrations if they're necessary.

Usage

Simply drop in has_friendship to a model:

class User < ActiveRecord::Base
  has_friendship
end

Managing friendship

Now, instances of User 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)

Blocking a friendable

A friendable can be blocked. When blocked, the friendable cannot request or remove friendship to the one that initially blocked it.

@dee.request_friend(@mac)

# @mac blocks @dee from making any more friendship actions
@mac.block_friend(@dee)

# @mac unblocks @dee
# Only @mac can perform this action
@mac.unblock_friend(@dee)

Checking friendship

# Check if there is an accepted friendship between @mac and @dee
@mac.friends_with?(@dee)

Type of friends

There are four types of friends:

  • requested_friends
  • pending_friends
  • blocked_friends
  • friends

Each type returns an array of friends, which should be looped through to access specific friends. They can be accessed using association.

requested_friends

Instances that sent friend request that has not been accepted.

@mac.friend_request(@dee)

@dee.requested_friends # => [@mac]

pending_friends

Instances that received but has not accepted the friend request.

@mac.friend_request(@dee)

@mac.pending_friends # => [@dee]

blocked_friends

Instances that are blocked from taking any friendship actions

@dee.friend_request(@mac)
@mac.block_friend(@dee)

@mac.blocked_friends # => [@dee]

friends

Instances with accepted Friendship.

@mac.friend_request(@dee)
@dee.accept_request(@mac)

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

Custom validations

You can provide custom validations for the friendship by implementing friendship_errors method on your Friendable model.

Returning an array with any elements will result in the friendship not being established.

def friendship_errors(wannabe_friend)
  return if can_become_friends_with?(wannabe_friend)

  [
    "Cannot become friends with #{wannabe_friend.email}",
  ]
end

Callbacks

To use callbacks you can add methods described below to your Friendable model.

def on_friendship_created(friendship)
  ...
end

def on_friendship_accepted(friendship)
  ...
end

def on_friendship_blocked(friendship)
  ...
end

def on_friendship_destroyed(friendship)
  ...
end
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].