Module: Devise::Models::Invitable::ClassMethods

Defined in:
lib/devise_invitable/model.rb

Instance Method Summary collapse

Instance Method Details

#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



125
126
127
128
129
130
131
132
133
# File 'lib/devise_invitable/model.rb', line 125

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

#invite!(attributes = {}) ⇒ 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. Attributes must contain the user email, other attributes will be set in the record



101
102
103
104
105
106
107
108
109
110
111
112
113
# File 'lib/devise_invitable/model.rb', line 101

def invite!(attributes={})
  invitable = find_or_initialize_with_error_by(:email, attributes.delete(:email))
  invitable.attributes = attributes

  if invitable.new_record?
    invitable.errors.clear if invitable.email.match Devise.email_regexp
  else
    invitable.errors.add(:email, :taken) unless invitable.invited?
  end

  invitable.invite! if invitable.errors.empty?
  invitable
end

#send_invitation(attributes = {}) ⇒ Object



115
116
117
118
# File 'lib/devise_invitable/model.rb', line 115

def send_invitation(attributes = {})
  ActiveSupport::Deprecation.warn('send_invitation has been renamed to invite!')
  self.invite!(attributes)
end