From 1b39b794461df8bc6b26ebb83d21606e1e232155 Mon Sep 17 00:00:00 2001 From: Raphael Date: Mon, 15 Nov 2010 16:34:39 -0800 Subject: [PATCH] Make invitation view with no inviters sensible --- Gemfile | 2 +- Gemfile.lock | 2 +- app/models/invitation.rb | 33 ++++++++------ app/views/devise/mailer/_inviters.haml | 11 +++++ app/views/devise/mailer/invitation.html.haml | 13 +----- config/locales/devise/devise.en.yml | 2 +- spec/models/invitation_spec.rb | 46 ++++++++++++-------- 7 files changed, 63 insertions(+), 46 deletions(-) create mode 100644 app/views/devise/mailer/_inviters.haml diff --git a/Gemfile b/Gemfile index 5071cec4d..ec4df5a75 100644 --- a/Gemfile +++ b/Gemfile @@ -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' diff --git a/Gemfile.lock b/Gemfile.lock index 760d087fc..cb04f9a2b 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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 diff --git a/app/models/invitation.rb b/app/models/invitation.rb index 7ee8705c7..4ee802817 100644 --- a/app/models/invitation.rb +++ b/app/models/invitation.rb @@ -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! diff --git a/app/views/devise/mailer/_inviters.haml b/app/views/devise/mailer/_inviters.haml new file mode 100644 index 000000000..b40b5807e --- /dev/null +++ b/app/views/devise/mailer/_inviters.haml @@ -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 diff --git a/app/views/devise/mailer/invitation.html.haml b/app/views/devise/mailer/invitation.html.haml index 735ee1088..c4a9a184c 100644 --- a/app/views/devise/mailer/invitation.html.haml +++ b/app/views/devise/mailer/invitation.html.haml @@ -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') diff --git a/config/locales/devise/devise.en.yml b/config/locales/devise/devise.en.yml index 42d49223a..1aad5e0b8 100644 --- a/config/locales/devise/devise.en.yml +++ b/config/locales/devise/devise.en.yml @@ -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." diff --git a/spec/models/invitation_spec.rb b/spec/models/invitation_spec.rb index e5843a7c3..cb79ec08d 100644 --- a/spec/models/invitation_spec.rb +++ b/spec/models/invitation_spec.rb @@ -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