Module: Revise::Models::Invitable::ClassMethods

Defined in:
lib/revise/models/invitable.rb

Instance Method Summary collapse

Instance Method Details

#_invite(attributes = {}, invited_by = nil, &block) ⇒ Object

Attempt to find a user by it’s email. If a record is not found, create a new user and send invitation to it. If user is found, returns the user with an email already exists error. If user is found and still have pending invitation, email is resend unless resend_invitation is set to false Attributes must contain the user email, other attributes will be set in the record



171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
# File 'lib/revise/models/invitable.rb', line 171

def _invite(attributes={}, invited_by=nil, &block)
  attributes.symbolize_keys!
  invite_key_array = invite_key_fields
  attributes_hash = {}
  invite_key_array.each do |k,v|
    attributes_hash[k] = attributes.delete(k)
  end

  invitable = find_or_initialize_with_errors(invite_key_array, attributes_hash)
  invitable.invited_by = invited_by

  invitable.skip_password = true
  invitable.valid? if self.validate_on_invite
  if invitable.new_record?
    invitable.errors.clear if !self.validate_on_invite and invitable.invite_key_valid?
  elsif !invitable.invited_to_sign_up? || !self.resend_invitation
    invite_key_array.each do |key|
      invitable.errors.add(key, :taken)
    end
  end

  if invitable.errors.empty?
    yield invitable if block_given?
    mail = invitable.invite!
  end
  [invitable, mail]
end

#accept_invitation!(attributes = {}) ⇒ Object

Attempt to find a user by it’s invitation_token to set it’s password. If a user is found, reset it’s password and automatically try saving the record. If not user is found, returns a new user containing an error in invitation_token attribute. Attributes must contain invitation_token, password and confirmation



214
215
216
217
218
219
220
221
222
# File 'lib/revise/models/invitable.rb', line 214

def accept_invitation!(attributes={})
  invitable = find_or_initialize_with_error_by(:invitation_token, attributes.delete(:invitation_token))
  invitable.errors.add(:invitation_token, :invalid) if invitable.invitation_token && invitable.persisted? && !invitable.valid_invitation?
  if invitable.errors.empty?
    invitable.attributes = attributes
    invitable.accept_invitation!
  end
  invitable
end

#invitation_tokenObject

Generate a token checking if one does not already exist in the database.



225
226
227
# File 'lib/revise/models/invitable.rb', line 225

def invitation_token
  generate_token(:invitation_token)
end

#invite!(attributes = {}, invited_by = nil, &block) ⇒ Object



199
200
201
202
# File 'lib/revise/models/invitable.rb', line 199

def invite!(attributes={}, invited_by=nil, &block)
  invitable, mail = _invite(attributes, invited_by, &block)
  invitable
end

#invite_key_fieldsObject

Return fields to invite



161
162
163
# File 'lib/revise/models/invitable.rb', line 161

def invite_key_fields
  invite_key.keys
end

#invite_mail!(attributes = {}, invited_by = nil, &block) ⇒ Object



204
205
206
207
# File 'lib/revise/models/invitable.rb', line 204

def invite_mail!(attributes={}, invited_by=nil, &block)
  invitable, mail = _invite(attributes, invited_by, &block)
  mail
end