-
Notifications
You must be signed in to change notification settings - Fork 10
Creating relationships between users
There are two types of relationships In social networks: uni-directorional (followings/followers) and bi-directional (friends). tog supports both. You can follow another user, or you can become friend (previous confirmation). In fact, if you follow an user that is already following you, you become friends directly.
So, we have three collections for relationships:
- followers: people that is following you. Friends are not included.
- followings: people you are following. Friends are not included.
- friends: people that you follow and are following you too.
This way you can decide if which kind of relationships you social network support. If you support only unidirectional relationships, to show all your followers you will need followers + friends. If you are developing a network with some kind of privacy or trust, you will probably differentiate between followers and friends, or just have support for friends only.
The controller Member::FriendshipsController
has four methods to manage this relationships:
namespace(:member) do |member| member.with_options(:controller => 'friendships') do |friendship| friendship.add_friend '/friend/:friend_id/add', :action => 'add_friend' friendship.remove_friend '/friend/:friend_id/remove', :action => 'remove_friend' friendship.follow_user '/follow/:friend_id', :action => 'follow' friendship.unfollow_user '/unfollow/:friend_id', :action => 'unfollow' end end
But yet easier, you can use the following helper methods in your views to write those links:
<%= following_options(profile) %> <%= friendship_options(profile) %>
Those will write the links for you, including the check to decide if you already are a friend/follower of the given user to choose between add and remove actions.