Module: Devise::Models::Invitable
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/devise_invitable/model.rb
Overview
Invitable is responsible for sending invitation emails. When an invitation is sent to an email address, an account is created for it. Invitation email contains a link allowing the user to accept the invitation by setting a password (as reset password from Devise’s recoverable module).
Configuration:
invite_for: The period the generated invitation token is valid, after
this period, the invited resource won't be able to accept the invitation.
When invite_for is 0 (the default), the invitation won't expire.
Examples:
User.find(1).invited_to_sign_up? # => true/false
User.invite!(:email => '[email protected]') # => send invitation
User.accept_invitation!(:invitation_token => '...') # => accept invitation with a token
User.find(1).accept_invitation! # => accept invitation
User.find(1).invite! # => reset invitation status and send invitation again
Defined Under Namespace
Modules: ClassMethods
Instance Attribute Summary collapse
-
#completing_invite ⇒ Object
Returns the value of attribute completing_invite.
-
#skip_invitation ⇒ Object
Returns the value of attribute skip_invitation.
Class Method Summary collapse
Instance Method Summary collapse
-
#accept_invitation! ⇒ Object
Accept an invitation by clearing invitation token and and setting invitation_accepted_at Confirms it if model is confirmable.
-
#accepted_or_not_invited? ⇒ Boolean
Verifies whether a user has accepted an invitation (or is accepting it), or was never invited.
-
#accepting_or_not_invited? ⇒ Boolean
Verifies whether a user has accepted an invitation (or is accepting it), or was never invited.
-
#invitation_accepted? ⇒ Boolean
Verifies whether a user accepted an invitation (or is accepting it).
- #invitation_fields ⇒ Object
-
#invite!(invited_by = nil) ⇒ Object
Reset invitation token and send invitation again.
- #invite_key_valid? ⇒ Boolean
- #invited? ⇒ Boolean
-
#invited_to_sign_up? ⇒ Boolean
Verifies whether a user has been invited or not.
- #reset_password!(new_password, new_password_confirmation) ⇒ Object
-
#valid_invitation? ⇒ Boolean
Verify whether a invitation is active or not.
-
#valid_password?(password) ⇒ Boolean
Only verify password when is not invited.
Instance Attribute Details
#completing_invite ⇒ Object
Returns the value of attribute completing_invite.
27 28 29 |
# File 'lib/devise_invitable/model.rb', line 27 def completing_invite @completing_invite end |
#skip_invitation ⇒ Object
Returns the value of attribute skip_invitation.
26 27 28 |
# File 'lib/devise_invitable/model.rb', line 26 def skip_invitation @skip_invitation end |
Class Method Details
.required_fields(klass) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/devise_invitable/model.rb', line 56 def self.required_fields(klass) fields = [:invitation_token, :invitation_sent_at, :invitation_accepted_at, :invitation_limit, :invited_by_id, :invited_by_type] if Devise.invited_by_class_name fields -= :invited_by_type end fields end |
Instance Method Details
#accept_invitation! ⇒ Object
Accept an invitation by clearing invitation token and and setting invitation_accepted_at Confirms it if model is confirmable
75 76 77 78 79 80 81 82 83 |
# File 'lib/devise_invitable/model.rb', line 75 def accept_invitation! self.invitation_accepted_at = Time.now.utc if self.invited_to_sign_up? && self.valid? run_callbacks :invitation_accepted do self.invitation_token = nil self.save(:validate => false) end end end |
#accepted_or_not_invited? ⇒ Boolean
Verifies whether a user has accepted an invitation (or is accepting it), or was never invited
102 103 104 |
# File 'lib/devise_invitable/model.rb', line 102 def accepted_or_not_invited? invitation_accepted? || !invited_to_sign_up? end |
#accepting_or_not_invited? ⇒ Boolean
Verifies whether a user has accepted an invitation (or is accepting it), or was never invited
86 87 88 89 |
# File 'lib/devise_invitable/model.rb', line 86 def accepting_or_not_invited? ActiveSupport::Deprecation.warn "accepting_or_not_invited? is deprecated and will be removed from DeviseInvitable 1.1.0 (use accepted_or_not_invited? instead)" accepted_or_not_invited? end |
#invitation_accepted? ⇒ Boolean
Verifies whether a user accepted an invitation (or is accepting it)
97 98 99 |
# File 'lib/devise_invitable/model.rb', line 97 def invitation_accepted? invitation_accepted_at.present? end |
#invitation_fields ⇒ Object
65 66 67 68 69 70 71 |
# File 'lib/devise_invitable/model.rb', line 65 def invitation_fields fields = [:invitation_sent_at, :invited_by_id, :invited_by_type] if Devise.invited_by_class_name fields -= :invited_by_type end fields end |
#invite!(invited_by = nil) ⇒ Object
Reset invitation token and send invitation again
112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/devise_invitable/model.rb', line 112 def invite!(invited_by = nil) was_invited = invited_to_sign_up? self.skip_confirmation! if self.new_record? && self.respond_to?(:skip_confirmation!) generate_invitation_token if self.invitation_token.nil? self.invitation_sent_at = Time.now.utc self.invited_by = invited_by if invited_by # Call these before_validate methods since we aren't validating on save self.downcase_keys if self.new_record? && self.respond_to?(:downcase_keys) self.strip_whitespace if self.new_record? && self.respond_to?(:strip_whitespace) if save(:validate => false) self.invited_by.decrement_invitation_limit! if !was_invited and self.invited_by.present? deliver_invitation unless @skip_invitation end end |
#invite_key_valid? ⇒ Boolean
146 147 148 149 150 151 |
# File 'lib/devise_invitable/model.rb', line 146 def invite_key_valid? return true unless self.class.invite_key.is_a? Hash # FIXME: remove this line when deprecation is removed self.class.invite_key.all? do |key, regexp| regexp.nil? || self.send(key).try(:match, regexp) end end |
#invited? ⇒ Boolean
106 107 108 109 |
# File 'lib/devise_invitable/model.rb', line 106 def invited? ActiveSupport::Deprecation.warn "invited? is deprecated and will be removed from DeviseInvitable 1.1.0 (use invited_to_sign_up? instead)" invited_to_sign_up? end |
#invited_to_sign_up? ⇒ Boolean
Verifies whether a user has been invited or not
92 93 94 |
# File 'lib/devise_invitable/model.rb', line 92 def invited_to_sign_up? persisted? && invitation_token.present? end |
#reset_password!(new_password, new_password_confirmation) ⇒ Object
141 142 143 144 |
# File 'lib/devise_invitable/model.rb', line 141 def reset_password!(new_password, new_password_confirmation) super accept_invitation! end |
#valid_invitation? ⇒ Boolean
Verify whether a invitation is active or not. If the user has been invited, we need to calculate if the invitation time has not expired for this user, in other words, if the invitation is still valid.
132 133 134 |
# File 'lib/devise_invitable/model.rb', line 132 def valid_invitation? invited_to_sign_up? && invitation_period_valid? end |
#valid_password?(password) ⇒ Boolean
Only verify password when is not invited
137 138 139 |
# File 'lib/devise_invitable/model.rb', line 137 def valid_password?(password) super unless invited_to_sign_up? end |