test batch_invite
This commit is contained in:
parent
e0fb8a08cb
commit
ad1b122e20
4 changed files with 47 additions and 14 deletions
|
|
@ -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)
|
||||
|
||||
|
|
|
|||
|
|
@ -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<String>] :emails
|
||||
# @return [Array<Invitation>] An array of initialized [Invitation] models.
|
||||
def self.batch_build(opts)
|
||||
emails = opts.delete(:emails)
|
||||
# @param [Array<String>] emails
|
||||
# @option opts [User] :sender
|
||||
# @option opts [Aspect] :aspect
|
||||
# @option opts [String] :service
|
||||
# @return [Array<Invitation>] 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
|
||||
|
|
|
|||
|
|
@ -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))
|
||||
|
|
|
|||
|
|
@ -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
|
||||
|
|
|
|||
Loading…
Reference in a new issue