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

Defined in:
lib/devise_invitable/model.rb

Instance Method Summary collapse

Instance Method Details

#_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



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

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

  invitable.valid? if self.validate_on_invite
  if invitable.new_record?
    invitable.errors.clear if !self.validate_on_invite and 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?
    mail = invitable.invite!
  end
  [invitable, mail]
end

#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



158
159
160
161
162
163
164
165
166
# File 'lib/devise_invitable/model.rb', line 158

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.



169
170
171
# File 'lib/devise_invitable/model.rb', line 169

def invitation_token
  generate_token(:invitation_token)
end

#invite!(attributes = {}, invited_by = nil, &block) ⇒ Object



143
144
145
146
# File 'lib/devise_invitable/model.rb', line 143

def invite!(attributes={}, invited_by=nil, &block)
  invitable, mail = _invite(attributes, invited_by, &block)
  invitable
end

#invite_mail!(attributes = {}, invited_by = nil, &block) ⇒ Object



148
149
150
151
# File 'lib/devise_invitable/model.rb', line 148

def invite_mail!(attributes={}, invited_by=nil, &block)
  invitable, mail = _invite(attributes, invited_by, &block)
  mail
end