Module: EffectiveDeviseUser

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/effective_devise_user.rb

Overview

EffectiveDeviseUsr

Mark your user model with devise_for then effective_devise_user

Defined Under Namespace

Modules: Base, ClassMethods

Instance Method Summary collapse

Instance Method Details

#active_for_authentication?Boolean

Returns:

  • (Boolean)


257
258
259
# File 'app/models/concerns/effective_devise_user.rb', line 257

def active_for_authentication?
  super && (respond_to?(:archived?) ? !archived? : true)
end

#allow_sign_up_from_invitation?Boolean

This allows the Sign Up form to work for existing users with a pending invitation It assigns the attributes from the sign_up_params, saves the user, accepts the invitation Note that this action skips the invitation_token validation and is therefore insecure.

Returns:

  • (Boolean)


269
270
271
# File 'app/models/concerns/effective_devise_user.rb', line 269

def 
  true
end

#alternate_email=(value) ⇒ Object



225
226
227
# File 'app/models/concerns/effective_devise_user.rb', line 225

def alternate_email=(value)
  super(value.to_s.strip.downcase.presence)
end

#block_from_invitation?Boolean

Allow users to sign in even if they have a pending invitation

Returns:

  • (Boolean)


262
263
264
# File 'app/models/concerns/effective_devise_user.rb', line 262

def block_from_invitation?
  false
end

#email_to_sObject

The user’s to_s when in an email



221
222
223
# File 'app/models/concerns/effective_devise_user.rb', line 221

def email_to_s
  to_s
end

#inactive_messageObject



273
274
275
# File 'app/models/concerns/effective_devise_user.rb', line 273

def inactive_message
  (respond_to?(:archived?) && archived?) ? :archived : super
end

#invite!(invited_by = nil, options = {}) ⇒ Object

Devise invitable ignores model validations, so we manually check for duplicate email addresses.



230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
# File 'app/models/concerns/effective_devise_user.rb', line 230

def invite!(invited_by = nil, options = {})
  if new_record?
    value = email.to_s.strip.downcase

    if value.blank?
      errors.add(:email, "can't be blank")
      return false
    end

    if self.class.where(email: value).present?
      errors.add(:email, 'has already been taken')
      return false
    end

    if respond_to?(:alternate_email) && self.class.where(alternate_email: value).present?
      errors.add(:email, 'has already been taken')
      return false
    end
  end

  super
end

#reinvite!Object



253
254
255
# File 'app/models/concerns/effective_devise_user.rb', line 253

def reinvite!
  invite!
end

#send_devise_notification(notification, *args) ⇒ Object

Send devise & devise_invitable emails via active job



283
284
285
286
287
288
289
290
291
292
# File 'app/models/concerns/effective_devise_user.rb', line 283

def send_devise_notification(notification, *args)
  raise('expected args Hash') unless args.respond_to?(:last) && args.last.kind_of?(Hash)

  if defined?(Tenant)
    tenant = Tenant.current || raise('expected a current tenant')
    args.last[:tenant] ||= tenant
  end

  devise_mailer.send(notification, self, *args).deliver_now
end

#to_select2Object



298
299
300
# File 'app/models/concerns/effective_devise_user.rb', line 298

def to_select2
  "<span>#{email_to_s}</span> <small>#{email}</small>"
end

#to_select2_search_columnsObject



294
295
296
# File 'app/models/concerns/effective_devise_user.rb', line 294

def to_select2_search_columns
  [:email, :public_email, :first_name, :last_name, :name]
end

#valid_password?(password) ⇒ Boolean

Any password will work in development mode

Returns:

  • (Boolean)


278
279
280
# File 'app/models/concerns/effective_devise_user.rb', line 278

def valid_password?(password)
  Rails.env.development? || super
end