Commit da16e3ee authored by maxwell's avatar maxwell

PALINDROME invite form can now take comma seperated multiple emails, so you...

PALINDROME invite form can now take comma seperated multiple emails, so you only have to click the box once
parent 7c091172
......@@ -8,12 +8,22 @@ class InvitationsController < Devise::InvitationsController
def create
if current_user.invites == 0
flash[:error] = I18n.t 'invitations.create.no_more'
redirect_to :back
return
end
begin
params[:user][:aspect_id] = params[:user].delete(:aspects)
message = params[:user].delete(:invite_messages)
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
if e.message == "You have no invites"
flash[:error] = I18n.t 'invitations.create.no_more'
......@@ -25,7 +35,7 @@ class InvitationsController < Devise::InvitationsController
raise e
end
end
redirect_to after_sign_in_path_for(resource_name)
redirect_to :back
end
def update
......
......@@ -24,14 +24,7 @@ class Invitation
raise "You already invited this person"
end
end
invited_user = create_invitee(opts)
if invited_user.persisted?
invited_user
else
false
end
create_invitee(opts)
end
def self.create_invitee(opts = {})
......@@ -43,20 +36,23 @@ class Invitation
invitee.errors.add(:email, :taken) unless invitee.invited?
end
if opts[:from]
invitee.save(:validate => false)
Invitation.create!(:from => opts[:from],
:to => invitee,
:into => opts[:into],
:message => opts[:message])
if invitee.errors.empty?
opts[:from].invites -= 1
opts[:from].save!
invitee.reload
end
if opts[:from]
invitee.save(:validate => false)
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! if invitee.errors.empty?
invitee.send(:generate_invitation_token)
invitee.invite!
end
invitee
end
......
......@@ -19,7 +19,7 @@ class User
key :username
key :serialized_private_key, String
key :invites, Integer, :default => 0
key :invites, Integer, :default => 5
key :invitation_token, String
key :invitation_sent_at, DateTime
key :pending_request_ids, Array, :typecast => 'ObjectId'
......@@ -321,23 +321,19 @@ class User
###Invitations############
def invite_user(opts = {})
if self.invites > 0
aspect_id = opts.delete(:aspect_id)
if aspect_id == nil
raise "Must invite into aspect"
end
aspect_object = self.aspects.find_by_id(aspect_id)
if !(aspect_object)
raise "Must invite to your aspect"
else
Invitation.invite(:email => opts[:email],
:from => self,
:into => aspect_object,
:message => opts[:invite_message])
end
aspect_id = opts.delete(:aspect_id)
if aspect_id == nil
raise "Must invite into aspect"
end
aspect_object = self.aspects.find_by_id(aspect_id)
if !(aspect_object)
raise "Must invite to your aspect"
else
raise "You have no invites"
Invitation.invite(:email => opts[:email],
:from => self,
:into => aspect_object,
:message => opts[:invite_message])
end
end
......
......@@ -11,7 +11,5 @@
%h3= t('.existing')
= render 'shared/contact_list', :aspect => aspect, :contacts => @contacts, :manage => defined?(manage)
.span-7.last
= render 'shared/add_contact', :aspect => aspect
......@@ -113,6 +113,5 @@
%br
%h3= t('.invites')
%p= t('.invite_people')
= render "shared/invitations", :invites => @invites
= link_to t('.invite_someone'), "#invite_user_pane", :class => "invite_user_button", :title => t('.invite_someone')
= t('.invitations_left', :count => invites)
%br
.yo{ :style => "display:none;"}
#invite_user_pane
= render "invitations/new"
-if invites > 0
= link_to t('.invite_someone'), "#invite_user_pane", :class => "invite_user_button", :title => t('.invite_someone')
= t('.invitations_left', :count => invites)
%br
.yo{ :style => "display:none;"}
#invite_user_pane
= render "invitations/new"
- else
You don't have any right now, but more invites are coming soon!
......@@ -260,7 +260,8 @@ en:
closed: "Signups are closed on this Diaspora pod."
invitations:
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."
already_sent: "You already invited this person."
already_contacts: "You are already connected with this person"
......
......@@ -14,33 +14,63 @@ describe InvitationsController do
before do
request.env["devise.mapping"] = Devise.mappings[:user]
user.invites = 3
user.invites = 5
sign_in :user, user
@invite = {:invite_messages=>"test", :aspects=> aspect.id.to_s, :email=>"abc@example.com"}
@controller.stub!(:current_user).and_return(user)
request.env["HTTP_REFERER"]= 'http://test.host/cats/foo'
end
describe "#create" do
it 'invites the requested user' do
user.should_receive(:invite_user).once
post :create, :user=>{:invite_messages=>"test", :aspects=> aspect.id.to_s, :email=>"abc@example.com"}
user.should_receive(:invite_user).and_return(make_user)
post :create, :user => @invite
end
it 'creates an invitation' do
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)
end
it 'creates an invited user with zero invites' do
it 'creates an invited user with five invites' do
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)
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
This diff is collapsed.
......@@ -49,25 +49,30 @@ describe Invitation do
Invitation.invite(:email => @email, :from => user, :into => aspect)
}.should change(Invitation, :count).by(1)
end
it 'associates the invitation with the inviter' do
lambda {
Invitation.invite(:email => @email, :from => user, :into => aspect)
}.should change{user.reload.invitations_from_me.count}.by(1)
end
it 'associates the invitation with the invitee' do
new_user = Invitation.invite(:email => @email, :from => user, :into => aspect)
new_user.invitations_to_me.count.should == 1
end
it 'creates a user' do
lambda {
Invitation.invite(:from => user, :email => @email, :into => aspect)
}.should change(User, :count).by(1)
end
it 'returns the new user' do
new_user = Invitation.invite(:from => user, :email => @email, :into => aspect)
new_user.is_a?(User).should be_true
new_user.email.should == @email
end
it 'adds the inviter to the invited_user' do
new_user = Invitation.invite(:from => user, :email => @email, :into => aspect)
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)
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
describe '.create_invitee' 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
message = "How've you been?"
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)
end
it 'mails the optional message' do
message = "How've you been?"
new_user = Invitation.create_invitee(:from => user, :email => @email, :into => aspect, :message => message)
Devise.mailer.deliveries.first.to_s.include?(message).should be_true
new_user = Invitation.create_invitee(@valid_params)
Devise.mailer.deliveries.first.to_s.include?(@message).should be_true
end
it 'has no translation missing' do
message = "How've you been?"
new_user = Invitation.create_invitee(:from => user, :email => @email, :into => aspect, :message => message)
new_user = Invitation.create_invitee(@valid_params)
Devise.mailer.deliveries.first.body.raw_source.match(/(translation_missing.+)/).should be_nil
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
context 'with no inviter' do
it 'sends an email that includes the right things' do
Invitation.create_invitee(:email => @email)
......
......@@ -49,13 +49,6 @@ describe User 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
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/
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment