swemoney.com

  • Random
  • Archive
  • RSS

HowTo: has_many_friends

I’ve had a lot of requests for some sort of howto for my has_many_friends plugin for rails. I’m not wondrous at making howto’s, but I’ve been trying to rack my brain thinking of things I could demonstrate in a new post about how to use the has_many_friends plugin. So we’ll see what we can do. There won’t be any large code examples. I’ll just include a few snippets here and there, mainly because I wrote has_many_friends to have enough wiggle room so people can incorporate it in a few different ways. So let’s get started…

If you’ve followed the README, you should have the plugin installed and you will have already added the has_many_friends line to your User model and generated the friendship model with the included generator. If you haven’t done this yet, look at the README where I’ve included lots of information on installing and using the plugin there.

Now that we have the plugin installed, we need to use it. So let’s first think about a few things we might want to do with friends. Off the top of my head, I can think of requesting friendship, accepting friend requests, listing all my friends, checking to see if I’m friends with someone, and deleting friends. I think that’s a decent enough list as demonstrating how to do these actions with my plugin should give you a pretty nice jump on using any of the methods included. I want to make sure everyone understands that this plugin is just an extension to your User model. It provides the database associations and extra helper methods to your User model. So you use it just like any other model. Let’s request friends. For our examples, I’ll have 2 users stored in variables called fred and wilma.

First thing we need to do, Fred needs to send a friend request to Wilma. This is going to go in a controller method that will be handling friend requests.

fred, wilma = User.find(1), User.find(2)
fred.request_friendship_with(wilma)

That’s it. You can set up an observer or do whatever you wish to notify Wilma she has a friend request, but I’m going to leave that up to you. Now Wilma is going to log in and on her dashboard, it’s going to see if she has any pending friends.

wilma = current_user
flash[:notice] = "You have a new friend request!" unless wilma.pending_friends_for_me.blank?

Now that we’ve alerted Wilma of her new friend request, she clicks on the ‘friend request’ link and gets to a list of all pending friends from other users.

def list
  wilma = current_user
  @pending_friends = wilma.pending_friends_for_me
end

You should be able to figure out what to do in your view to display these. The only thing you need to think about is that you need 2 links for each pending friend. One will link to an action to accept the friend request and the other will deny the friend request. Let’s create the accept action first.

def accept_friend
  fred, wilma = User.find(params[:id]), current_user
  wilma.accept_friendship_with(fred)
  flash[:notice] = "Friendship has been accepted with Fred"
end

Now Wilma and Fred are friends. Keep in mind that this code is very basic and just to show those who are having trouble understanding how to use this plugin. Your code WILL differ from this but this should give you a general idea of what to do. Now that we have a friendship, Fred logs on because he got the email from your uber cool observer that said that Wilma has accepted his friendship. He’s a happy boy. He heads straight for his friend list to see his new addition. As an example, I usually have a separate controller called friends for this with a list action.

def list
  fred = current_user
  @friends = fred.friends
end

Again, you know what to do with @friends in the view. Fred now sees Wilma in that list of friends. Let’s say when 2 people are friends, they get to see more from each other. We’ll need to check for this each time a user visits a page. Fred is going to visit a page of Wilma’s right now.

def show
  fred, wilma = current_user, User.find(params[:id])
  flash[:notice] = "Your friends with this person!" if fred.is_friends_with?(wilma)
end

As just an example, this will show a nice little message if your friends with the person who owns this page. Use your imagination and you should be able to come up with some fancy ways of using this. Now Fred and Wilma have a really big fight. Punches are thrown and Fred is left with a black eye. He’s angry and he’s hell bent on revenge. He’s going to remove Wilma from his friends list! I intentionally left out the ‘deny friendship’ action above because it’s the same as deleting a friendship.

def delete_friendship
  fred, wilma = current_user, User.find(params[:id])
  fred.delete_friendship_with(wilma)
  flash[:notice] = "You and Wilma are no longer friends"
end

That’ll teach her! And there you have it. A very basic guide to using has_many_friends in your rails application. You will definitely have to elaborate on these examples for your application, but I hope it helps a few of you. Please remember to check out the README for ALL the methods available to use with this plugin. They are all used just like the methods I used in this example and most of them do similar things but you should be able to figure out how they differ by their names. Comment away!

    • #plugins
    • #has_many_friends
    • #ruby on rails
    • #My Stuff
  • 4 years ago
  • 4
  • Comments
  • Permalink
  • Share
    Tweet

4 Notes/ Hide

  1. swemoney posted this

Recent comments

Blog comments powered by Disqus
← Previous • Next →
iOS application development, video games, random thoughts, apple. These are a few of the wonderful things you'll catch on my blog.

Pages

  • Projects
  • Apps
  • Secret Browser

Elsewhere

  • @swemoney on Twitter
  • Facebook Profile
  • tehdnite on Youtube
  • dnite on Digg
  • swemoney on Rdio
  • swemoney on github

Twitter

loading tweets…

  • RSS
  • Random
  • Archive
  • Mobile

Effector Theme by Carlo Franco.

Powered by Tumblr