Make invitation view with no inviters sensible

This commit is contained in:
Raphael 2010-11-15 16:34:39 -08:00
parent 70bcab3abc
commit 1b39b79446
7 changed files with 63 additions and 46 deletions

View file

@ -8,7 +8,7 @@ gem "chef", :require => false
#Security
gem 'devise', '1.1.3'
gem 'devise-mongo_mapper', :git => 'git://github.com/collectiveidea/devise-mongo_mapper'
gem 'devise_invitable', '~> 0.3.4'
gem 'devise_invitable','0.3.5'
#Authentication
gem 'omniauth'

View file

@ -381,7 +381,7 @@ DEPENDENCIES
database_cleaner (= 0.5.2)
devise (= 1.1.3)
devise-mongo_mapper!
devise_invitable (~> 0.3.4)
devise_invitable (= 0.3.5)
em-http-request!
em-websocket
factory_girl_rails

View file

@ -25,15 +25,9 @@ class Invitation
end
end
invited_user = create_invitee(:email => opts[:email])
invited_user = create_invitee(opts)
if invited_user.persisted?
Invitation.create!(:from => opts[:from],
:to => invited_user,
:into => opts[:into],
:message => opts[:message])
opts[:from].invites -= 1
opts[:from].save!
invited_user
else
false
@ -41,16 +35,29 @@ class Invitation
end
def self.create_invitee(opts = {})
invitable = User.find_or_initialize_with_error_by(:email, opts[:email])
invitee = User.find_or_initialize_with_error_by(:email, opts[:email])
if invitable.new_record?
invitable.errors.clear if invitable.email.try(:match, Devise.email_regexp)
if invitee.new_record?
invitee.errors.clear if invitee.email.try(:match, Devise.email_regexp)
else
invitable.errors.add(:email, :taken) unless invitable.invited?
invitee.errors.add(:email, :taken) unless invitee.invited?
end
invitable.invite! if invitable.errors.empty?
invitable
if opts[:from]
invitee.save(:validate => false)
Invitation.create!(:from => opts[:from],
:to => invitee,
:into => opts[:into],
:message => opts[:message])
opts[:from].invites -= 1
opts[:from].save!
invitee.reload
end
invitee.send(:generate_invitation_token)
invitee.invite! if invitee.errors.empty?
invitee
end
def to_request!

View file

@ -0,0 +1,11 @@
%p
- if @invs.count == 1
= t('.has_invited_you', :name => @invs.first.from.real_name + " (#{@invs.first.from.diaspora_handle})")
- else
= t('.have_invited_you', :names => (@invs.map{|inv| inv.from.real_name + " (#{inv.from.diaspora_handle})"}.join(",")))
= t('.accept_at', :url => root_url)
- @invs.each do |inv|
- if inv.message
= "#{inv.from.real_name}:"
= "\"#{inv.message}\""
%p

View file

@ -10,17 +10,8 @@
- @invs = @resource.invitations_to_me
%p
= t('devise.mailer.welcome', :email => @resource.email)
%p
- if @invs.count == 1
= t('.has_invited_you', :name => @invs.first.real_name + " (#{@invs.first.diaspora_handle})")
- else
= t('.have_invited_you', :names => (@invs.map{|inv| inv.from.real_name + " (#{inv.from.diaspora_handle})"}.join(",")))
= t('.accept_at', :url => root_url)
- @invs.each do |inv|
- if inv.message
= "#{inv.from.real_name}:"
= "\"#{inv.message}\""
%p
-if @invs.count > 0
= render :partial => 'inviters'
%p= link_to t('.accept'), accept_invitation_url(@resource, :invitation_token => @resource.invitation_token), :class => "large_text"
%p.small
= t('.ignore')

View file

@ -63,7 +63,7 @@ en:
click_to_unlock: "Click the link below to unlock your account:"
unlock: "Unlock my account"
invitation:
subject: 'A friend wants you to join Diaspora!'
subject: "You've been invited to join Diaspora!"
has_invited_you: "%{name} has invited you to join Diaspora"
have_invited_you: "%{names} have invited you to join Diaspora"
accept_at: ", at %{url}, you can accept it through the link below."

View file

@ -79,11 +79,6 @@ describe Invitation do
new_user.invitations_to_me.first.message.should == message
end
it 'mails the optional message' do
message = "How've you been?"
new_user = Invitation.invite(:from => user, :email => @email, :into => aspect, :message => message)
Devise.mailer.deliveries.first.to_s.include?(message).should be_true
end
it 'sends a contact request to a user with that email into the aspect' do
user2
user.should_receive(:send_contact_request_to){ |a, b|
@ -95,24 +90,37 @@ describe Invitation do
end
describe '.create_invitee' do
it 'creates a user' do
lambda {
Invitation.create_invitee(:email => @email)
}.should change(User, :count).by(1)
end
it 'sends email to the invited user' do
lambda {
Invitation.create_invitee(:email => @email)
}.should change{Devise.mailer.deliveries.size}.by(1)
end
it 'sends an email that includes the right things' do
Invitation.create_invitee(:email => @email)
Devise.mailer.deliveries.first.to_s.include?("Welcome #{@email}").should == true
context 'with an inviter' do
it 'sends mail' do
message = "How've you been?"
lambda {
Invitation.create_invitee(:from => user, :email => @email, :into => aspect, :message => message)
}.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
end
end
context 'with no inviter' do
it 'sends an email that includes the right things' do
Invitation.create_invitee(:email => @email)
Devise.mailer.deliveries.first.to_s.include?("Welcome #{@email}").should == true
end
it 'creates a user' do
lambda {
Invitation.create_invitee(:email => @email)
}.should change(User, :count).by(1)
end
it 'sends email to the invited user' do
lambda {
Invitation.create_invitee(:email => @email)
}.should change{Devise.mailer.deliveries.size}.by(1)
end
it 'does not render nonsensical emails' do
Invitation.create_invitee(:email => @email)
Devise.mailer.deliveries.first.subject.match(/a friend/i).should be_false
Devise.mailer.deliveries.first.body.raw_source.match(/have invited you to join/i).should be_false
end
end
end