Satchel
Satchel

Reputation: 16734

Is there a plugin or gem that can help me do "invite a friend" capability in rails?

I want to add the ability for users to invite a friend.

The email should be generated so that, if someone clicks on the link and register, that person is automatically a friend.

Not sure what the options are, but wanted some ideas and strategies as an alternative to building it from scratch.

Upvotes: 3

Views: 762

Answers (4)

Kyle Macey
Kyle Macey

Reputation: 8154

Start by defining the relationship:

class User < ActiveRecord::Base
 has_and_belongs_to_many :friends, :class_name => "User", :join_table => "friends_users"
end

So really, User relates to itself with a different name. Then you can use something along the lines of:

@current_user.friends << @selected_user

in your controller.

Upvotes: 0

hnprashanth
hnprashanth

Reputation: 831

I don't know gem for rails. But there's an extension for Spree, rails based e-commerce project. Check it out & probably you can refer how it's implemented.

https://github.com/spree/spree_email_to_friend

Upvotes: 1

mnelson
mnelson

Reputation: 3012

I'm not aware of any gems that handle the entire process (user >> email >> signup). If you're just looking to create the relationship when a user comes from a specific link, create a special invitation route (the separate controller isn't necessary but just to make it clear):

# routes.rb
match '/invite/:friend_id' => 'public#invite', :as => :invite

# PublicController
def invite
  session[:referring_friend] = params[:friend_id]
  redirect_to root_path
end

# UsersController
def create
  @user = User.new(params[:user])
  if @user.save
    @user.create_friendship(session[:referring_friend]) if session[:referring_friend]
    ...
  else
    ...
  end
end

If you want to track conversion metrics, I'd recommend creating a link model and using that to track clicks and signups:

class Link < ActiveRecord::Base

  belongs_to :user
  attr_accessible :user, :user_id, :clicks, :conversions

  def click!
    self.class.increment_count(:clicks, self.id)
  end

  def convert!
    self.class.increment_count(:conversions, self.id)
  end

end

# routes.rb
match '/invite/:link_id' => 'links#hit', :as => :invite

# LinksController
def hit
  link = Link.find(params[:link_id])
  link.click!
  session[:referring_link_id] = link.id
  redirect_to root_path # or whatever path (maybe provided by link...)
end

# UsersController
def create
  @user = User.new(params[:user])
  if @user.save
    if session[:referring_link_id]
      link = Link.find(session[:referring_link_id])
      link.convert!
      @user.create_friendship(link.user_id)
    end
    ...
  else
    ...
  end
end

Which method you choose depends on what you'll want to track down the road.

Upvotes: 2

BurmajaM
BurmajaM

Reputation: 724

I don't know about some gem to support this, but solution should be rather trivial. I guess you need Friendship model, you can place some status in it like 'waiting_for_approvment' and send in mail link with that Friendship model id. When user accepts either way you just change status to 'approved' or even 'rejected' if you want to track that too.

Upvotes: 0

Related Questions