module Devise::Models::Invitable::ClassMethods
Public Instance Methods
Attempt to find a user by its email. If a record is not found, create a new user and send an invitation to it. If the user is found, return the user with an email already exists error. If the user is found and still has a pending invitation, invitation email is resent unless resend_invitation is set to false. Attributes must contain the user’s email, other attributes will be set in the record
# File lib/devise_invitable/models.rb, line 302 def _invite(attributes = {}, invited_by = nil, options = {}, &block) invite_key_array = invite_key_fields attributes_hash = {} invite_key_array.each do |k,v| attribute = attributes.delete(k) attribute = attribute.to_s.strip if strip_whitespace_keys.include?(k) attributes_hash[k] = attribute end invitable = find_or_initialize_with_errors(invite_key_array, attributes_hash) invitable.assign_attributes(attributes) invitable.invited_by = invited_by unless invitable.password || invitable.encrypted_password.present? invitable.password = random_password end invitable.valid? if self.validate_on_invite if invitable.new_record? invitable.clear_errors_on_valid_keys if !self.validate_on_invite elsif invitable.invitation_taken? || !self.resend_invitation invite_key_array.each do |key| invitable.add_taken_error(key) end end yield invitable if block_given? mail = invitable.invite!(nil, options.merge(validate: false)) if invitable.errors.empty? [invitable, mail] end
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
# File lib/devise_invitable/models.rb, line 346 def accept_invitation!(attributes = {}) original_token = attributes.delete(:invitation_token) invitable = find_by_invitation_token(original_token, false) if invitable.errors.empty? invitable.assign_attributes(attributes) invitable.accept_invitation! end invitable end
# File lib/devise_invitable/models.rb, line 377 def after_invitation_accepted(*args, &blk) set_callback(:invitation_accepted, :after, *args, &blk) end
# File lib/devise_invitable/models.rb, line 369 def after_invitation_created(*args, &blk) set_callback(:invitation_created, :after, *args, &blk) end
# File lib/devise_invitable/models.rb, line 373 def before_invitation_accepted(*args, &blk) set_callback(:invitation_accepted, :before, *args, &blk) end
Callback convenience methods
# File lib/devise_invitable/models.rb, line 365 def before_invitation_created(*args, &blk) set_callback(:invitation_created, :before, *args, &blk) end
# File lib/devise_invitable/models.rb, line 356 def find_by_invitation_token(original_token, only_valid) invitation_token = Devise.token_generator.digest(self, :invitation_token, original_token) invitable = find_or_initialize_with_error_by(:invitation_token, invitation_token) invitable.errors.add(:invitation_token, :invalid) if invitable.invitation_token && invitable.persisted? && !invitable.valid_invitation? invitable unless only_valid && invitable.errors.present? end
# File lib/devise_invitable/models.rb, line 332 def invite!(attributes = {}, invited_by = nil, options = {}, &block) attr_hash = ActiveSupport::HashWithIndifferentAccess.new(attributes.to_h) _invite(attr_hash, invited_by, options, &block).first end
Return fields to invite
# File lib/devise_invitable/models.rb, line 291 def invite_key_fields invite_key.keys end
# File lib/devise_invitable/models.rb, line 337 def invite_mail!(attributes = {}, invited_by = nil, options = {}, &block) _invite(attributes, invited_by, options, &block).last end
Private Instance Methods
The random password, as set after an invitation, must conform to any password format validation rules of the application. This default fixes the most common scenarios: Passwords must contain lower + upper case, a digit and a symbol. For more unusual rules, this method can be overridden.
# File lib/devise_invitable/models.rb, line 400 def random_password length = respond_to?(:password_length) ? password_length : Devise.password_length prefix = 'aA1!' prefix + Devise.friendly_token(length.last - prefix.length) end