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

View file

@ -4,15 +4,20 @@
class InvitationsController < Devise::InvitationsController class InvitationsController < Devise::InvitationsController
def create def create
begin
self.resource = current_user.invite_user(params[resource_name]) self.resource = current_user.invite_user(params[resource_name])
flash[:notice] = I18n.t 'invitations.create.sent'
if resource.errors.empty? rescue RuntimeError => e
set_flash_message :notice, :send_instructions#, :email => self.resource.email if e.message == "You have no invites"
redirect_to after_sign_in_path_for(resource_name) 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 else
render_with_scope :new raise e
end end
end end
redirect_to after_sign_in_path_for(resource_name)
end
def update def update
begin begin

View file

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

View file

@ -26,6 +26,7 @@
%li.grey Drag to ignore/remove %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= 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;"} .yo{ :style => "display:none;"}
#invite_user_pane #invite_user_pane

View file

@ -1,7 +1,7 @@
%p %p
Hello #{@resource.email}! Hello #{@resource.email}!
%p %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= link_to 'Accept invitation', accept_invitation_url(@resource, :invitation_token => @resource.invitation_token)
%p %p
If you don't want to accept the invitation, please ignore this email. If you don't want to accept the invitation, please ignore this email.

View file

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

View file

@ -154,6 +154,14 @@ en:
sign_up: "Sign up" sign_up: "Sign up"
create: create:
success: "You've joined Diaspora!" 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: status_messages:
new_status_message: new_status_message:
tell_me_something_good: "tell me something good" 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 describe User do
let(:inviter) {Factory.create :user} let(:inviter) {Factory.create :user}
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_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 context "creating invites" do
it 'should invite the user' do it 'should invite the user' do
@ -23,6 +27,23 @@ describe User do
end end
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 context "the acceptance of an invitation" do
it "should create the person with the passed in params" do it "should create the person with the passed in params" do
person_count = Person.count person_count = Person.count