• Stars
    star
    198
  • Rank 196,898 (Top 4 %)
  • Language
    Ruby
  • Created almost 10 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

Add friendship to ActiveRecord models

(UNMAINTAINED) HasFriendship Build Status Coverage Status

Maintainer or owner wanted: #86

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

More Repositories

1

millisec

Prettify milliseconds with customizable format
JavaScript
60
star
2

vym

The code that powers Vym
JavaScript
37
star
3

react-cntdwn

A simple, customizable countdown timer for React
JavaScript
27
star
4

meteor-stat

Get a simple analysis of your Meteor app
JavaScript
20
star
5

node-producthunt

node.js wrapper for the Product Hunt API
JavaScript
19
star
6

pattern_generator

The missing Rails generator for patterns
Ruby
16
star
7

meteor-icon

a simple icon for Meteor packages
JavaScript
16
star
8

node-ufc-api

node.js wrapper for UFC API
JavaScript
13
star
9

editer

A high level multiline string manipulation library
JavaScript
12
star
10

factory-boy

A minimalistic factory for Meteor
JavaScript
11
star
11

meteor-mailman

Preview outgoing emails in a browser when developing Meteor app
JavaScript
11
star
12

iron-utils

Convenient client side utilities for Iron Router
JavaScript
10
star
13

k_means_ruby

K-means Algorithm in Ruby
Ruby
10
star
14

acts_as_liked

Add like feature to any Active Record models.
Ruby
8
star
15

compconv

Convert React components between function and class
JavaScript
7
star
16

meteor-continuous-delivery

A Meteor app with continuous delivery using TravisCI
JavaScript
6
star
17

convert-time

Convert a string between 12-hour time and 24-hour time
JavaScript
6
star
18

match-bracket

Find matching brackets in code
JavaScript
5
star
19

qparse

A simple top-down parser for advanced search queries
TypeScript
5
star
20

tset

Development Driven Test. Generate tests for your models in a Rails app.
Ruby
5
star
21

tru

An esoteric programming language with prefix-free brackets
Python
4
star
22

violet

modern, reactive forum built with Meteor
CSS
4
star
23

vym-chrome-extension

The chrome extension for Vym
JavaScript
4
star
24

pixel-explorer

Explore the screen and look for the pixels with the desired color.
JavaScript
4
star
25

vym-sky

Work in Progress. Different version of Vym.
JavaScript
3
star
26

hacker-news-hover-profile

View Hacker News user profiles by hovering over usernames
JavaScript
3
star
27

license-up

Update outdated license files from your command line
JavaScript
3
star
28

dotfiles

Personal dotfiles
Shell
2
star
29

feedy

Rails engine to collect and view user feedback
Ruby
2
star
30

rubbis

A simple Redis clone in Ruby
Ruby
1
star
31

typeform-slack-inviter

A little program to monitor TypeForm responses and send Slack invite
Go
1
star
32

ny

a programming language
C
1
star
33

bind_vs_arrow

JavaScript
1
star
34

tic-tac-toe

A game of tic tac toe versus computer.
Python
1
star
35

lotus-todo

A todo application using Lotus and AngularJS
Ruby
1
star
36

locater

Easily find locations of string or regex in a multi-line input
JavaScript
1
star
37

nomad-icon

Broadcast your current location
JavaScript
1
star