Module: Devise::Models::Invitable::ClassMethods
- Defined in:
- lib/devise_invitable/models.rb
Instance Method Summary collapse
-
#_invite(attributes = {}, invited_by = nil, options = {}) {|invitable| ... } ⇒ Object
Attempt to find a user by its email.
-
#accept_invitation!(attributes = {}) ⇒ Object
Attempt to find a user by it’s invitation_token to set it’s password.
- #after_invitation_accepted(*args, &blk) ⇒ Object
- #after_invitation_created(*args, &blk) ⇒ Object
- #before_invitation_accepted(*args, &blk) ⇒ Object
-
#before_invitation_created(*args, &blk) ⇒ Object
Callback convenience methods.
- #find_by_invitation_token(original_token, only_valid) ⇒ Object
- #invite!(attributes = {}, invited_by = nil, options = {}, &block) ⇒ Object
-
#invite_key_fields ⇒ Object
Return fields to invite.
- #invite_mail!(attributes = {}, invited_by = nil, options = {}, &block) ⇒ Object
Instance Method Details
#_invite(attributes = {}, invited_by = nil, options = {}) {|invitable| ... } ⇒ Object
Attempt to find a user by its email. If a record is not found, create a new user and send an invitation to it. If the user is found, return the user with an email already exists error. If the user is found and still has a pending invitation, invitation email is resent unless resend_invitation is set to false. Attributes must contain the user’s email, other attributes will be set in the record
284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 |
# File 'lib/devise_invitable/models.rb', line 284 def _invite(attributes={}, invited_by=nil, = {}, &block) invite_key_array = invite_key_fields attributes_hash = {} invite_key_array.each do |k,v| attribute = attributes.delete(k) attribute = attribute.to_s.strip if strip_whitespace_keys.include?(k) attributes_hash[k] = attribute end invitable = find_or_initialize_with_errors(invite_key_array, attributes_hash) invitable.assign_attributes(attributes) invitable.invited_by = invited_by unless invitable.password || invitable.encrypted_password.present? invitable.password = random_password end invitable.valid? if self.validate_on_invite if invitable.new_record? invitable.clear_errors_on_valid_keys if !self.validate_on_invite elsif !invitable.invited_to_sign_up? || !self.resend_invitation invite_key_array.each do |key| invitable.errors.add(key, :taken) end end yield invitable if block_given? mail = invitable.invite!(nil, ) if invitable.errors.empty? [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
328 329 330 331 332 333 334 335 336 |
# File 'lib/devise_invitable/models.rb', line 328 def accept_invitation!(attributes={}) original_token = attributes.delete(:invitation_token) invitable = find_by_invitation_token(original_token, false) if invitable.errors.empty? invitable.assign_attributes(attributes) invitable.accept_invitation! end invitable end |
#after_invitation_accepted(*args, &blk) ⇒ Object
359 360 361 |
# File 'lib/devise_invitable/models.rb', line 359 def after_invitation_accepted(*args, &blk) set_callback(:invitation_accepted, :after, *args, &blk) end |
#after_invitation_created(*args, &blk) ⇒ Object
351 352 353 |
# File 'lib/devise_invitable/models.rb', line 351 def after_invitation_created(*args, &blk) set_callback(:invitation_created, :after, *args, &blk) end |
#before_invitation_accepted(*args, &blk) ⇒ Object
355 356 357 |
# File 'lib/devise_invitable/models.rb', line 355 def before_invitation_accepted(*args, &blk) set_callback(:invitation_accepted, :before, *args, &blk) end |
#before_invitation_created(*args, &blk) ⇒ Object
Callback convenience methods
347 348 349 |
# File 'lib/devise_invitable/models.rb', line 347 def before_invitation_created(*args, &blk) set_callback(:invitation_created, :before, *args, &blk) end |
#find_by_invitation_token(original_token, only_valid) ⇒ Object
338 339 340 341 342 343 344 |
# File 'lib/devise_invitable/models.rb', line 338 def find_by_invitation_token(original_token, only_valid) invitation_token = Devise.token_generator.digest(self, :invitation_token, original_token) invitable = find_or_initialize_with_error_by(:invitation_token, invitation_token) invitable.errors.add(:invitation_token, :invalid) if invitable.invitation_token && invitable.persisted? && !invitable.valid_invitation? invitable unless only_valid && invitable.errors.present? end |
#invite!(attributes = {}, invited_by = nil, options = {}, &block) ⇒ Object
314 315 316 317 |
# File 'lib/devise_invitable/models.rb', line 314 def invite!(attributes={}, invited_by=nil, = {}, &block) attr_hash = ActiveSupport::HashWithIndifferentAccess.new(attributes.to_h) _invite(attr_hash, invited_by, , &block).first end |
#invite_key_fields ⇒ Object
Return fields to invite
273 274 275 |
# File 'lib/devise_invitable/models.rb', line 273 def invite_key_fields invite_key.keys end |
#invite_mail!(attributes = {}, invited_by = nil, options = {}, &block) ⇒ Object
319 320 321 |
# File 'lib/devise_invitable/models.rb', line 319 def invite_mail!(attributes={}, invited_by=nil, = {}, &block) _invite(attributes, invited_by, , &block).last end |