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



164
165
166
167
168
169
170
171
172
# File 'lib/devise_invitable/model.rb', line 164

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.



175
176
177
# File 'lib/devise_invitable/model.rb', line 175

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. Attributes must contain the user email, other attributes will be set in the record



141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
# File 'lib/devise_invitable/model.rb', line 141

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?
  end

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