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



146
147
148
149
150
151
152
153
154
# File 'lib/devise_invitable/model.rb', line 146

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.



157
158
159
# File 'lib/devise_invitable/model.rb', line 157

def invitation_token
  generate_token(:invitation_token)
end

#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



123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
# File 'lib/devise_invitable/model.rb', line 123

def invite!(attributes={}, invited_by=nil, &block)
  invitable = find_or_initialize_with_error_by(invite_key, attributes.delete(invite_key))
  invitable.attributes = attributes
  invitable.invited_by = invited_by

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

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