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! if invitable.valid?
  end
  invitable
end

#send_invitation(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. Options must contain the user email



106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'lib/devise_invitable/model.rb', line 106

def send_invitation(attributes={})
  invitable = find_or_initialize_by_email(attributes[:email])

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

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