From ad1b122e20f2fea7116617dcc38d0794a8df9076 Mon Sep 17 00:00:00 2001 From: Maxwell Salzberg Date: Wed, 17 Aug 2011 16:27:36 -0700 Subject: [PATCH] test batch_invite --- app/controllers/invitations_controller.rb | 3 +- app/models/invitation.rb | 31 ++++++++++++------- .../invitations_controller_spec.rb | 2 +- spec/models/invitation_spec.rb | 25 ++++++++++++++- 4 files changed, 47 insertions(+), 14 deletions(-) diff --git a/app/controllers/invitations_controller.rb b/app/controllers/invitations_controller.rb index bd1b03da9..e29490341 100644 --- a/app/controllers/invitations_controller.rb +++ b/app/controllers/invitations_controller.rb @@ -22,7 +22,8 @@ class InvitationsController < Devise::InvitationsController emails = params[:user][:email].to_s.gsub(/\s/, '').split(/, */) #NOTE should we try and find users by email here? probs aspect = current_user.aspects.find(aspect_id) - invites = Invitation.batch_build(:sender => current_user, :aspect => aspect, :emails => emails, :service => 'email') + + invites = Invitation.batch_invite(emails, :sender => current_user, :aspect => aspect, :service => 'email') flash[:notice] = extract_messages(invites) diff --git a/app/models/invitation.rb b/app/models/invitation.rb index 4ccbc3322..344bc7d04 100644 --- a/app/models/invitation.rb +++ b/app/models/invitation.rb @@ -8,15 +8,14 @@ class Invitation < ActiveRecord::Base belongs_to :recipient, :class_name => 'User' belongs_to :aspect - validates_presence_of :identifier, :service - - validates_presence_of :sender, :aspect, :unless => :admin? attr_accessible :sender, :recipient, :aspect, :service, :identifier, :admin before_validation :set_email_as_default_service - validate :ensure_not_inviting_self, :on => :create, :unless => :admin? + validates_presence_of :identifier, :service validate :valid_identifier? + validates_presence_of :sender, :aspect, :unless => :admin? + validate :ensure_not_inviting_self, :on => :create, :unless => :admin? validate :sender_owns_aspect?, :unless => :admin? validates_uniqueness_of :sender_id, :scope => [:identifier, :service], :unless => :admin? @@ -26,10 +25,20 @@ class Invitation < ActiveRecord::Base # @note options hash is passed through to [Invitation.new] # @see [Invitation.new] # - # @option opts [Array] :emails - # @return [Array] An array of initialized [Invitation] models. - def self.batch_build(opts) - emails = opts.delete(:emails) + # @param [Array] emails + # @option opts [User] :sender + # @option opts [Aspect] :aspect + # @option opts [String] :service + # @return [Array] An array of [Invitation] models + # the valid ones are saved, and the invalid ones are not. + def self.batch_invite(emails, opts) + + users_on_pod = User.where(:email => emails, :invitation_token => nil) + + #share with anyone whose email you entered who is on the pod + emails = emails - users_on_pod.map{|u| u.email} + users_on_pod.each{|u| opts[:sender].share_with(u.person, opts[:aspect])} + emails.map! do |e| Invitation.create(opts.merge(:identifier => e)) end @@ -71,7 +80,6 @@ class Invitation < ActiveRecord::Base # @return [Invitation] self def send! self.attach_recipient! - puts self.recipient.inspect # Sets an instance variable in User (set by devise invitable) # This determines whether an email should be sent to the recipient. @@ -94,9 +102,10 @@ class Invitation < ActiveRecord::Base # @return [String] def recipient_identifier - if self.service == 'email' + case self.service + when 'email' self.identifier - elsif self.service == 'facebook' + when'facebook' if su = ServiceUser.where(:uid => self.identifier).first su.name else diff --git a/spec/controllers/invitations_controller_spec.rb b/spec/controllers/invitations_controller_spec.rb index a2256366e..dc1b3fdad 100644 --- a/spec/controllers/invitations_controller_spec.rb +++ b/spec/controllers/invitations_controller_spec.rb @@ -11,7 +11,7 @@ describe InvitationsController do AppConfig[:open_invitations] = true @user = alice @aspect = @user.aspects.first - @invite = {:invite_message=>"test", :aspect_id=> @aspect.id.to_s, :email=>"abc@example.com"} + @invite = {:invite_message=>"test", :aspects=> @aspect.id.to_s, :email=>"abc@example.com"} request.env["devise.mapping"] = Devise.mappings[:user] Webfinger.stub_chain(:new, :fetch).and_return(Factory(:person)) diff --git a/spec/models/invitation_spec.rb b/spec/models/invitation_spec.rb index a4e76e2e3..329df2971 100644 --- a/spec/models/invitation_spec.rb +++ b/spec/models/invitation_spec.rb @@ -77,7 +77,30 @@ describe Invitation do end end - describe '.resend' do + describe '.batch_invite' do + before do + @emails = ['max@foo.com', 'bob@mom.com'] + @opts = {:aspect => eve.aspects.first, :sender => eve, :service => 'email'} + end + + it 'returns an array of invites based on the emails passed in' do + invites = Invitation.batch_invite(@emails, @opts) + invites.count.should be 2 + invites.all?{|x| x.persisted?}.should be_true + end + + it 'shares with people who are already on the pod and does not create an invite for them' do + Factory(:user, :email => @emails.first) + invites = nil + expect{ + invites = Invitation.batch_invite(@emails, @opts) + }.to change(eve.contacts, :count).by(1) + invites.count.should be 1 + + end + end + + describe '#resend' do before do @invitation = Factory(:invitation, :sender => alice, :aspect => alice.aspects.first, :service => 'email', :identifier => 'a@a.com') end