the invited user keeps track of who invited them, limit on invites,

removed the invites controller test
This commit is contained in:
ilya 2010-10-13 16:52:18 -07:00
parent 57bf3aac33
commit 36b9f66f60
9 changed files with 67 additions and 39 deletions

View file

@ -9,6 +9,7 @@ class ApplicationController < ActionController::Base
before_filter :set_friends_and_status, :except => [:create, :update]
before_filter :count_requests
before_filter :fb_user_info
before_filter :set_invites
layout :layout_by_resource
@ -37,6 +38,12 @@ class ApplicationController < ActionController::Base
@request_count = Request.for_user(current_user).size if current_user
end
def set_invites
if current_user
@invites = current_user.invites
end
end
def fb_user_info
if current_user
@access_token = warden.session[:access_token]

View file

@ -4,14 +4,19 @@
class InvitationsController < Devise::InvitationsController
def create
self.resource = current_user.invite_user(params[resource_name])
if resource.errors.empty?
set_flash_message :notice, :send_instructions#, :email => self.resource.email
redirect_to after_sign_in_path_for(resource_name)
else
render_with_scope :new
begin
self.resource = current_user.invite_user(params[resource_name])
flash[:notice] = I18n.t 'invitations.create.sent'
rescue RuntimeError => e
if e.message == "You have no invites"
flash[:error] = I18n.t 'invitations.create.no_more'
elsif e.message == "You already invited this person"
flash[:error] = I18n.t 'invitations.create.already_sent'
else
raise e
end
end
redirect_to after_sign_in_path_for(resource_name)
end
def update

View file

@ -27,6 +27,7 @@ class User
key :username, :unique => true
key :serialized_private_key, String
key :invites, Integer, :default => 5
key :invitation_token, String
key :invitation_sent_at, DateTime
key :inviter_ids, Array
@ -266,17 +267,25 @@ class User
###Invitations############
def invite_user( opts = {} )
invited_user = User.invite!(:email => opts[:email], :inviter => self)
#invited_user.inviters << self
#invited_user.save!
invited_user
if self.invites > 0
invited_user = User.invite!(:email => opts[:email], :inviter => self)
self.invites = self.invites - 1
self.save!
invited_user
else
raise "You have no invites"
end
end
def self.invite!(attributes={})
inviter = attributes.delete(:inviter)
invitable = find_or_initialize_with_error_by(:email, attributes.delete(:email))
invitable.attributes = attributes
invitable.inviters << inviter
if invitable.inviters.include?(inviter)
raise "You already invited this person"
else
invitable.inviters << inviter
end
if invitable.new_record?
invitable.errors.clear if invitable.email.try(:match, Devise.email_regexp)

View file

@ -26,6 +26,7 @@
%li.grey Drag to ignore/remove
%h3= link_to "Invite a friend!", "#invite_user_pane", :id => "invite_user_button", :class => "invite_user_button", :title => "Invite a friend"
%h3= "You have #{@invites} invites."
.yo{ :style => "display:none;"}
#invite_user_pane

View file

@ -1,7 +1,7 @@
%p
Hello #{@resource.email}!
%p
#{(@resource.inviters.count == 1)? ( @resource.inviters.first.real_name " has") : (@resource.inviters.map{|inv| inv.real_name}.join(",") + " have")} invited you to #{root_url}, you can accept it through the link below.
#{(@resource.inviters.count == 1)? ( @resource.inviters.first.real_name + " has") : (@resource.inviters.map{|inv| inv.real_name}.join(",") + " have")} invited you to #{root_url}, you can accept it through the link below.
%p= link_to 'Accept invitation', accept_invitation_url(@resource, :invitation_token => @resource.invitation_token)
%p
If you don't want to accept the invitation, please ignore this email.

View file

@ -24,6 +24,8 @@
%br
= link_to "Invite a friend!", "#invite_user_pane", :id => "invite_user_button", :class => "invite_user_button", :title => "Invite a friend"
%br
= "You have #{@invites} invites."
.yo{ :style => "display:none;"}
#invite_user_pane
= render "invitations/new"

View file

@ -154,6 +154,14 @@ en:
sign_up: "Sign up"
create:
success: "You've joined Diaspora!"
invitations:
create:
sent: 'Your invitation has been sent.'
no_more: 'You have no more invitations.'
already_sent: 'You already invited this person.'
invitation_token_invalid: 'The invitation token provided is not valid!'
updated: 'Your password was set successfully. You are now signed in.'
status_messages:
new_status_message:
tell_me_something_good: "tell me something good"

View file

@ -1,25 +0,0 @@
# Copyright (c) 2010, Diaspora Inc. This file is
# licensed under the Affero General Public License version 3 or later. See
# the COPYRIGHT file.
require 'spec_helper'
describe InvitationsController do
render_views
let(:user) {Factory.create :user}
before do
sign_in :user, user
end
context 'inviting another user' do
it 'should create an invited user and add keep track of an invitor' do
debugger
params = {"user" => {"email" => "test@example.com"}}
post :create, params
#invitee = inviter.invite_user(:email => "test@example.com")
#invitee.inviters.includes?(inviter).should be true
end
end
end

View file

@ -6,7 +6,11 @@ require 'spec_helper'
describe User do
let(:inviter) {Factory.create :user}
let!(:invited_user) { create_user_with_invitation("abc", :email => "email@example.com", :inviter => inviter)}
let(:inviter_with_3_invites) {Factory.create :user, :invites => 3}
let!(:invited_user) { create_user_with_invitation("abc", :email => "email@example.com", :inviter => inviter)}
let(:invited_user1) { create_user_with_invitation("abc", :email => "email@example.com", :inviter => inviter_with_3_invites)}
let(:invited_user2) { create_user_with_invitation("abc", :email => "email@example.com", :inviter => inviter_with_3_invites)}
let(:invited_user3) { create_user_with_invitation("abc", :email => "email@example.com", :inviter => inviter_with_3_invites)}
context "creating invites" do
it 'should invite the user' do
@ -23,6 +27,23 @@ describe User do
end
end
context "limit on invites" do
it 'does not invite users after 3 invites' do
User.stub!(:invite!).and_return(invited_user1,invited_user2,invited_user3)
inviter_with_3_invites.invite_user(:email => "email1@example.com")
inviter_with_3_invites.invite_user(:email => "email2@example.com")
inviter_with_3_invites.invite_user(:email => "email3@example.com")
proc{inviter_with_3_invites.invite_user(:email => "email4@example.com")}.should raise_error /You have no invites/
end
it 'does not invite people I already invited' do
pending "this is really weird to test without the actual method working"
User.stub!(:invite!).and_return(invited_user1,invited_user1)
inviter_with_3_invites.invite_user(:email => "email1@example.com")
proc{inviter_with_3_invites.invite_user(:email => "email1@example.com")}.should raise_error /You already invited that person/
end
end
context "the acceptance of an invitation" do
it "should create the person with the passed in params" do
person_count = Person.count