PALINDROME invite form can now take comma seperated multiple emails, so you only have to click the box once

This commit is contained in:
maxwell 2010-11-23 00:08:30 -08:00
parent 7c09117230
commit da16e3ee1e
11 changed files with 414 additions and 347 deletions

View file

@ -8,12 +8,22 @@ class InvitationsController < Devise::InvitationsController
def create def create
if current_user.invites == 0
flash[:error] = I18n.t 'invitations.create.no_more'
redirect_to :back
return
end
begin begin
params[:user][:aspect_id] = params[:user].delete(:aspects) params[:user][:aspect_id] = params[:user].delete(:aspects)
message = params[:user].delete(:invite_messages) message = params[:user].delete(:invite_messages)
params[:user][:invite_message] = message unless message == "" params[:user][:invite_message] = message unless message == ""
self.resource = current_user.invite_user(params[resource_name])
flash[:notice] = I18n.t 'invitations.create.sent' emails = params[:user][:email].split(/, */)
invited_users = emails.map { |e| current_user.invite_user(params[:user].merge({:email => e}))}
good_users, rejected_users = invited_users.partition {|u| u.persisted? }
flash[:notice] = I18n.t('invitations.create.sent') + good_users.map{|x| x.email}.join(', ')
flash[:error] = I18n.t('invitations.create.REJECTED') + rejected_users.map{|x| x.email}.join(', ')
rescue RuntimeError => e rescue RuntimeError => e
if e.message == "You have no invites" if e.message == "You have no invites"
flash[:error] = I18n.t 'invitations.create.no_more' flash[:error] = I18n.t 'invitations.create.no_more'
@ -25,7 +35,7 @@ class InvitationsController < Devise::InvitationsController
raise e raise e
end end
end end
redirect_to after_sign_in_path_for(resource_name) redirect_to :back
end end
def update def update

View file

@ -24,14 +24,7 @@ class Invitation
raise "You already invited this person" raise "You already invited this person"
end end
end end
create_invitee(opts)
invited_user = create_invitee(opts)
if invited_user.persisted?
invited_user
else
false
end
end end
def self.create_invitee(opts = {}) def self.create_invitee(opts = {})
@ -43,20 +36,23 @@ class Invitation
invitee.errors.add(:email, :taken) unless invitee.invited? invitee.errors.add(:email, :taken) unless invitee.invited?
end end
if opts[:from] if invitee.errors.empty?
invitee.save(:validate => false)
Invitation.create!(:from => opts[:from],
:to => invitee,
:into => opts[:into],
:message => opts[:message])
opts[:from].invites -= 1 if opts[:from]
opts[:from].save! invitee.save(:validate => false)
invitee.reload Invitation.create!(:from => opts[:from],
:to => invitee,
:into => opts[:into],
:message => opts[:message])
opts[:from].invites -= 1 unless opts[:from].invites == 0
opts[:from].save!
invitee.reload
end
invitee.send(:generate_invitation_token)
invitee.invite!
end end
invitee.send(:generate_invitation_token)
invitee.invite! if invitee.errors.empty?
invitee invitee
end end

View file

@ -19,7 +19,7 @@ class User
key :username key :username
key :serialized_private_key, String key :serialized_private_key, String
key :invites, Integer, :default => 0 key :invites, Integer, :default => 5
key :invitation_token, String key :invitation_token, String
key :invitation_sent_at, DateTime key :invitation_sent_at, DateTime
key :pending_request_ids, Array, :typecast => 'ObjectId' key :pending_request_ids, Array, :typecast => 'ObjectId'
@ -321,23 +321,19 @@ class User
###Invitations############ ###Invitations############
def invite_user(opts = {}) def invite_user(opts = {})
if self.invites > 0 aspect_id = opts.delete(:aspect_id)
aspect_id = opts.delete(:aspect_id) if aspect_id == nil
if aspect_id == nil raise "Must invite into aspect"
raise "Must invite into aspect" end
end aspect_object = self.aspects.find_by_id(aspect_id)
aspect_object = self.aspects.find_by_id(aspect_id) if !(aspect_object)
if !(aspect_object) raise "Must invite to your aspect"
raise "Must invite to your aspect"
else
Invitation.invite(:email => opts[:email],
:from => self,
:into => aspect_object,
:message => opts[:invite_message])
end
else else
raise "You have no invites" Invitation.invite(:email => opts[:email],
:from => self,
:into => aspect_object,
:message => opts[:invite_message])
end end
end end

View file

@ -11,7 +11,5 @@
%h3= t('.existing') %h3= t('.existing')
= render 'shared/contact_list', :aspect => aspect, :contacts => @contacts, :manage => defined?(manage) = render 'shared/contact_list', :aspect => aspect, :contacts => @contacts, :manage => defined?(manage)
.span-7.last .span-7.last
= render 'shared/add_contact', :aspect => aspect = render 'shared/add_contact', :aspect => aspect

View file

@ -113,6 +113,5 @@
%br %br
%h3= t('.invites') %h3= t('.invites')
%p= t('.invite_people')
= render "shared/invitations", :invites => @invites = render "shared/invitations", :invites => @invites

View file

@ -1,6 +1,9 @@
= link_to t('.invite_someone'), "#invite_user_pane", :class => "invite_user_button", :title => t('.invite_someone') -if invites > 0
= t('.invitations_left', :count => invites) = link_to t('.invite_someone'), "#invite_user_pane", :class => "invite_user_button", :title => t('.invite_someone')
%br = t('.invitations_left', :count => invites)
.yo{ :style => "display:none;"} %br
#invite_user_pane .yo{ :style => "display:none;"}
= render "invitations/new" #invite_user_pane
= render "invitations/new"
- else
You don't have any right now, but more invites are coming soon!

View file

@ -260,7 +260,8 @@ en:
closed: "Signups are closed on this Diaspora pod." closed: "Signups are closed on this Diaspora pod."
invitations: invitations:
create: create:
sent: "Your invitation has been sent." sent: "Invitations have been sent to: "
REJECTED: "The following email addresses had problems: "
no_more: "You have no more invitations." no_more: "You have no more invitations."
already_sent: "You already invited this person." already_sent: "You already invited this person."
already_contacts: "You are already connected with this person" already_contacts: "You are already connected with this person"

View file

@ -14,33 +14,63 @@ describe InvitationsController do
before do before do
request.env["devise.mapping"] = Devise.mappings[:user] request.env["devise.mapping"] = Devise.mappings[:user]
user.invites = 3 user.invites = 5
sign_in :user, user sign_in :user, user
@invite = {:invite_messages=>"test", :aspects=> aspect.id.to_s, :email=>"abc@example.com"}
@controller.stub!(:current_user).and_return(user) @controller.stub!(:current_user).and_return(user)
request.env["HTTP_REFERER"]= 'http://test.host/cats/foo'
end end
describe "#create" do describe "#create" do
it 'invites the requested user' do it 'invites the requested user' do
user.should_receive(:invite_user).once user.should_receive(:invite_user).and_return(make_user)
post :create, :user=>{:invite_messages=>"test", :aspects=> aspect.id.to_s, :email=>"abc@example.com"} post :create, :user => @invite
end end
it 'creates an invitation' do it 'creates an invitation' do
lambda{ lambda{
post :create, :user=>{:invite_messages=>"test", :aspects=> aspect.id.to_s, :email=>"abc@example.com"} post :create, :user => @invite
}.should change(Invitation, :count).by(1) }.should change(Invitation, :count).by(1)
end end
it 'creates an invited user with zero invites' do it 'creates an invited user with five invites' do
lambda{ lambda{
post :create, :user=>{:invite_messages=>"test", :aspects=> aspect.id.to_s, :email=>"abc@example.com"} post :create, :user => @invite
}.should change(User, :count).by(1) }.should change(User, :count).by(1)
User.find_by_email("abc@example.com").invites.should == 0 User.find_by_email("abc@example.com").invites.should == 5
end
it 'can handle a comma seperated list of emails' do
lambda {
post :create, :user => @invite.merge(:email => "foofoofoofoo@example.com, mbs@gmail.com")
}.should change(User, :count).by(2)
end
it 'displays a message that tells you how many invites were sent, and which REJECTED' do
post :create, :user => @invite.merge(:email => "mbs@gmail.com, foo@bar.com, foo.com, lala@foo, cool@bar.com")
flash[:notice].should_not be_empty
flash[:notice].should =~ /mbs@gmail\.com/
flash[:notice].should =~ /foo@bar\.com/
flash[:notice].should =~ /cool@bar\.com/
flash[:error].should_not be_empty
flash[:error].should =~ /foo\.com/
flash[:error].should =~ /lala@foo/
end
it "doesn't invite anyone if you have 0 invites" do
user.invites = 0
user.save!
lambda {
post :create, :user => @invite.merge(:email => "mbs@gmail.com, foo@bar.com, foo.com, lala@foo, cool@bar.com")
}.should_not change(User, :count)
end
it 'returns to the previous page on success' do
post :create, :user => @invite
response.should redirect_to("http://test.host/cats/foo")
end end
end end
end end

File diff suppressed because it is too large Load diff

View file

@ -49,25 +49,30 @@ describe Invitation do
Invitation.invite(:email => @email, :from => user, :into => aspect) Invitation.invite(:email => @email, :from => user, :into => aspect)
}.should change(Invitation, :count).by(1) }.should change(Invitation, :count).by(1)
end end
it 'associates the invitation with the inviter' do it 'associates the invitation with the inviter' do
lambda { lambda {
Invitation.invite(:email => @email, :from => user, :into => aspect) Invitation.invite(:email => @email, :from => user, :into => aspect)
}.should change{user.reload.invitations_from_me.count}.by(1) }.should change{user.reload.invitations_from_me.count}.by(1)
end end
it 'associates the invitation with the invitee' do it 'associates the invitation with the invitee' do
new_user = Invitation.invite(:email => @email, :from => user, :into => aspect) new_user = Invitation.invite(:email => @email, :from => user, :into => aspect)
new_user.invitations_to_me.count.should == 1 new_user.invitations_to_me.count.should == 1
end end
it 'creates a user' do it 'creates a user' do
lambda { lambda {
Invitation.invite(:from => user, :email => @email, :into => aspect) Invitation.invite(:from => user, :email => @email, :into => aspect)
}.should change(User, :count).by(1) }.should change(User, :count).by(1)
end end
it 'returns the new user' do it 'returns the new user' do
new_user = Invitation.invite(:from => user, :email => @email, :into => aspect) new_user = Invitation.invite(:from => user, :email => @email, :into => aspect)
new_user.is_a?(User).should be_true new_user.is_a?(User).should be_true
new_user.email.should == @email new_user.email.should == @email
end end
it 'adds the inviter to the invited_user' do it 'adds the inviter to the invited_user' do
new_user = Invitation.invite(:from => user, :email => @email, :into => aspect) new_user = Invitation.invite(:from => user, :email => @email, :into => aspect)
new_user.invitations_to_me.first.from.should == user new_user.invitations_to_me.first.from.should == user
@ -87,27 +92,63 @@ describe Invitation do
} }
Invitation.invite(:from => user, :email => user2.email, :into => aspect) Invitation.invite(:from => user, :email => user2.email, :into => aspect)
end end
it 'decrements the invite count of the from user' do
message = "How've you been?"
lambda{
new_user = Invitation.invite(:from => user, :email => @email, :into => aspect, :message => message)
}.should change(user, :invites).by(-1)
end
it "doesn't decrement counter past zero" do
user.invites = 0
user.save!
message = "How've you been?"
lambda {
new_user = Invitation.invite(:from => user, :email => @email, :into => aspect, :message => message)
}.should_not change(user, :invites)
end
context 'invalid email' do
it 'return a user with errors' do
new_user = Invitation.invite(:email => "fkjlsdf", :from => user, :into => aspect)
new_user.should have(1).errors_on(:email)
new_user.should_not be_persisted
end
end
end end
describe '.create_invitee' do describe '.create_invitee' do
context 'with an inviter' do context 'with an inviter' do
before do
@message = "whatever"
@valid_params = {:from => user, :email => @email, :into => aspect, :message => @message}
end
it 'sends mail' do it 'sends mail' do
message = "How've you been?"
lambda { lambda {
Invitation.create_invitee(:from => user, :email => @email, :into => aspect, :message => message) Invitation.create_invitee(@valid_params)
}.should change{Devise.mailer.deliveries.size}.by(1) }.should change{Devise.mailer.deliveries.size}.by(1)
end end
it 'mails the optional message' do it 'mails the optional message' do
message = "How've you been?" new_user = Invitation.create_invitee(@valid_params)
new_user = Invitation.create_invitee(:from => user, :email => @email, :into => aspect, :message => message) Devise.mailer.deliveries.first.to_s.include?(@message).should be_true
Devise.mailer.deliveries.first.to_s.include?(message).should be_true
end end
it 'has no translation missing' do it 'has no translation missing' do
message = "How've you been?" new_user = Invitation.create_invitee(@valid_params)
new_user = Invitation.create_invitee(:from => user, :email => @email, :into => aspect, :message => message)
Devise.mailer.deliveries.first.body.raw_source.match(/(translation_missing.+)/).should be_nil Devise.mailer.deliveries.first.body.raw_source.match(/(translation_missing.+)/).should be_nil
end end
it "doesn't create an invitation if the email is invalid" do
new_user = Invitation.create_invitee(@valid_params.merge(:email => 'fdfdfdfdf'))
new_user.should_not be_persisted
new_user.should have(1).error_on(:email)
end
end end
context 'with no inviter' do context 'with no inviter' do
it 'sends an email that includes the right things' do it 'sends an email that includes the right things' do
Invitation.create_invitee(:email => @email) Invitation.create_invitee(:email => @email)

View file

@ -49,13 +49,6 @@ describe User do
context "limit on invites" do context "limit on invites" do
it 'does not invite users after 3 invites' do
inviter_with_3_invites.invite_user(:email => "email1@example.com", :aspect_id => aspect2.id)
inviter_with_3_invites.invite_user(:email => "email2@example.com", :aspect_id => aspect2.id)
inviter_with_3_invites.invite_user(:email => "email3@example.com", :aspect_id => aspect2.id)
proc{inviter_with_3_invites.invite_user(:email => "email4@example.com", :aspect_id => aspect2.id)}.should raise_error /You have no invites/
end
it 'does not invite people I already invited' do it 'does not invite people I already invited' do
inviter_with_3_invites.invite_user(:email => "email1@example.com", :aspect_id => aspect2.id) inviter_with_3_invites.invite_user(:email => "email1@example.com", :aspect_id => aspect2.id)
proc{inviter_with_3_invites.invite_user(:email => "email1@example.com", :aspect_id => aspect2.id)}.should raise_error /You already invited this person/ proc{inviter_with_3_invites.invite_user(:email => "email1@example.com", :aspect_id => aspect2.id)}.should raise_error /You already invited this person/