Class: Invitation

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/invitation.rb

Overview

TODO: kill me

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.batch_invite(emails, opts) ⇒ Array<Invitation>

Note:

options hash is passed through to [Invitation.new]

Returns An array of [Invitation] models the valid optsnes are saved, and the invalid ones are not.

Options Hash (opts):

See Also:

  • [Invitation[Invitation.new]

34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'app/models/invitation.rb', line 34

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
  users_on_pod.each{|u| opts[:sender].share_with(u.person, opts[:aspect])}

  emails.map! do |e|
    user = users_on_pod.find{|u| u.email == e}
    Invitation.create(opts.merge(:identifier => e, :recipient => user))
  end
  emails
end

Instance Method Details

#convert_to_admin!Invitation

converts a personal invitation to an admin invite used in account deletion


83
84
85
86
87
88
89
# File 'app/models/invitation.rb', line 83

def convert_to_admin!
  self.admin = true
  self.sender = nil
  self.aspect = nil
  self.save
  self
end

#email_like_identiferString


106
107
108
109
110
111
112
113
# File 'app/models/invitation.rb', line 106

def email_like_identifer
  case self.service
  when 'email'
    self.identifier
  when 'facebook'
    false
  end
end

#ensure_not_inviting_selfObject

Note:

Validation


121
122
123
124
125
# File 'app/models/invitation.rb', line 121

def ensure_not_inviting_self
  if self.identifier == self.sender.email
    errors[:base] << 'You can not invite yourself.'
  end
end

#identifier=(ident) ⇒ Object

Downcases the incoming service identifier and assigns it

See Also:

  • super

53
54
55
56
# File 'app/models/invitation.rb', line 53

def identifier=(ident)
  ident.downcase! if ident
  super
end

#recipient_identifierString


96
97
98
99
100
101
102
103
# File 'app/models/invitation.rb', line 96

def recipient_identifier
  case self.service
  when 'email'
    self.identifier
  when'facebook'
    I18n.t('invitations.a_facebook_user')
  end
end

#recipient_not_on_pod?Boolean


135
136
137
138
139
140
# File 'app/models/invitation.rb', line 135

def recipient_not_on_pod?
  return true if self.recipient.nil?
  if self.recipient.username?
    errors[:recipient] << "The user '#{self.identifier}' (#{self.recipient.diaspora_handle}) is already on this pod, so we sent them a share request"
  end
end

#resendInvitation


91
92
93
# File 'app/models/invitation.rb', line 91

def resend
  self.send!
end

#send!Invitation

Find or create user, and send that resultant User an invitation.


70
71
72
73
74
75
76
77
# File 'app/models/invitation.rb', line 70

def send!
  if email_like_identifer
    EmailInviter.new(self.identifier, sender).send! 
  else
    puts "broken facebook invitation_token"
  end
  self
end

#sender_owns_aspect?Boolean

Note:

Validation


128
129
130
131
132
# File 'app/models/invitation.rb', line 128

def sender_owns_aspect?
  if self.sender_id != self.aspect.user_id
    errors[:base] << 'You do not own that aspect.'
  end
end

#set_email_as_default_serviceObject

Note:

before_save


116
117
118
# File 'app/models/invitation.rb', line 116

def set_email_as_default_service
  self.service ||= 'email'
end

#skip_email?Boolean, void

Determine if we want to skip emailing the recipient.


62
63
64
# File 'app/models/invitation.rb', line 62

def skip_email?
  !email_like_identifer
end

#valid_identifier?Boolean

Note:

Validation


143
144
145
146
147
148
149
150
# File 'app/models/invitation.rb', line 143

def valid_identifier?
  return false unless self.identifier
  if self.service == 'email'
    unless self.identifier.match(Devise.email_regexp)
      errors[:base] << 'invalid email'
    end
  end
end