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)


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

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)


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

def 
  true
end

#alternate_email=(value) ⇒ Object



230
231
232
# File 'app/models/concerns/effective_devise_user.rb', line 230

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)


267
268
269
# File 'app/models/concerns/effective_devise_user.rb', line 267

def block_from_invitation?
  false
end

#destroyable? ⇒ Boolean

You cannot delete a user if they have any memberships, applicants, orders, or event registrations

Returns:

  • (Boolean)


308
309
310
311
312
313
314
315
# File 'app/models/concerns/effective_devise_user.rb', line 308

def destroyable?
  return false if respond_to?(:archived?) && !archived?
  return false if self.class.respond_to?(:effective_memberships_owner?) && (membership.present? || membership_histories.present?)
  return false if self.class.respond_to?(:effective_memberships_user?) && (applicants.present? || fee_payments.present?)
  return false if Effective::Order.where(user: self).present?
  return false if Effective::EventRegistrant.where(owner: self).or(Effective::EventRegistrant.where(user: self)).present?
  true
end

#email_to_s ⇒ Object

The user's to_s when in an email



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

def email_to_s
  to_s
end

#full_name ⇒ Object



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

def full_name
  [first_name.presence, last_name.presence].compact.join(' ')
end

#inactive_message ⇒ Object



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

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.



235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
# File 'app/models/concerns/effective_devise_user.rb', line 235

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



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

def reinvite!
  invite!
end

#send_devise_notification(notification, *args) ⇒ Object

Send devise & devise_invitable emails via active job



288
289
290
291
292
293
294
295
296
297
# File 'app/models/concerns/effective_devise_user.rb', line 288

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_select2 ⇒ Object



303
304
305
# File 'app/models/concerns/effective_devise_user.rb', line 303

def to_select2
  "<span>#{email_to_s}</span> <small>&lt;#{try(:public_email).presence || email}&gt;</small>"
end

#to_select2_search_columns ⇒ Object



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

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

#valid_password?(password) ⇒ Boolean

Any password will work in development mode

Returns:

  • (Boolean)


283
284
285
# File 'app/models/concerns/effective_devise_user.rb', line 283

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