Module: Devise::Models::Invitable::ClassMethods
- Defined in:
- lib/devise_invitable/model.rb
Instance Method Summary collapse
-
#_invite(attributes = {}, invited_by = nil) {|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
-
#before_invitation_accepted(*args, &blk) ⇒ Object
Callback convenience methods.
- #find_by_invitation_token(original_token, only_valid) ⇒ Object
-
#invitation_token ⇒ Object
Generate a token checking if one does not already exist in the database.
- #invite!(attributes = {}, invited_by = nil, &block) ⇒ Object
-
#invite_key_fields ⇒ Object
Return fields to invite.
- #invite_mail!(attributes = {}, invited_by = nil, &block) ⇒ Object
Instance Method Details
#_invite(attributes = {}, invited_by = nil) {|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
220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 |
# File 'lib/devise_invitable/model.rb', line 220 def _invite(attributes={}, invited_by=nil, &block) invite_key_array = invite_key_fields attributes_hash = {} invite_key_array.each do |k,v| attributes_hash[k] = attributes.delete(k).to_s.strip end invitable = find_or_initialize_with_errors(invite_key_array, attributes_hash) invitable.assign_attributes(attributes) invitable.invited_by = invited_by invitable.skip_password = true 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! 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
261 262 263 264 265 266 267 268 269 |
# File 'lib/devise_invitable/model.rb', line 261 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
293 294 295 |
# File 'lib/devise_invitable/model.rb', line 293 def after_invitation_accepted(*args, &blk) set_callback(:invitation_accepted, :after, *args, &blk) end |
#before_invitation_accepted(*args, &blk) ⇒ Object
Callback convenience methods
289 290 291 |
# File 'lib/devise_invitable/model.rb', line 289 def before_invitation_accepted(*args, &blk) set_callback(:invitation_accepted, :before, *args, &blk) end |
#find_by_invitation_token(original_token, only_valid) ⇒ Object
271 272 273 274 275 276 277 278 279 280 281 |
# File 'lib/devise_invitable/model.rb', line 271 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) if !invitable.persisted? && Devise.allow_insecure_token_lookup invitable = find_or_initialize_with_error_by(:invitation_token, original_token) end invitable.errors.add(:invitation_token, :invalid) if invitable.invitation_token && invitable.persisted? && !invitable.valid_invitation? invitable.invitation_token = original_token invitable unless only_valid && invitable.errors.present? end |
#invitation_token ⇒ Object
Generate a token checking if one does not already exist in the database.
284 285 286 |
# File 'lib/devise_invitable/model.rb', line 284 def invitation_token generate_token(:invitation_token) end |
#invite!(attributes = {}, invited_by = nil, &block) ⇒ Object
246 247 248 249 |
# File 'lib/devise_invitable/model.rb', line 246 def invite!(attributes={}, invited_by=nil, &block) invitable, mail = _invite(attributes.with_indifferent_access, invited_by, &block) invitable end |
#invite_key_fields ⇒ Object
Return fields to invite
209 210 211 |
# File 'lib/devise_invitable/model.rb', line 209 def invite_key_fields invite_key.keys end |
#invite_mail!(attributes = {}, invited_by = nil, &block) ⇒ Object
251 252 253 254 |
# File 'lib/devise_invitable/model.rb', line 251 def invite_mail!(attributes={}, invited_by=nil, &block) invitable, mail = _invite(attributes, invited_by, &block) mail end |