module Devise::Models::Authenticatable
def self.required_fields(klass)
def self.required_fields(klass) [] end
def active_for_authentication?
def active_for_authentication? true end
def apply_to_attribute_or_variable(attr, method)
def apply_to_attribute_or_variable(attr, method) if self[attr] self[attr] = self[attr].try(method) # Use respond_to? here to avoid a regression where globally # configured strip_whitespace_keys or case_insensitive_keys were # attempting to strip or downcase when a model didn't have the # globally configured key. elsif respond_to?(attr) && respond_to?("#{attr}=") new_value = send(attr).try(method) send("#{attr}=", new_value) end end
def authenticatable_salt
def authenticatable_salt end
def devise_mailer
def devise_mailer Devise.mailer end
def downcase_keys
def downcase_keys self.class.case_insensitive_keys.each { |k| apply_to_attribute_or_variable(k, :downcase) } end
def inactive_message
def inactive_message :inactive end
def inspect
Redefine inspect using serializable_hash, to ensure we don't accidentally
def inspect inspection = serializable_hash.collect do |k,v| "#{k}: #{respond_to?(:attribute_for_inspect) ? attribute_for_inspect(k) : v.inspect}" end "#<#{self.class} #{inspection.join(", ")}>" end
def send_devise_notification(notification, *args)
end
end
end
message.deliver
else
message.deliver_now
elsif message.respond_to?(:deliver_now)
# Remove once we move to Rails 4.2+ only, as `deliver` is deprecated.
message.deliver_later
if message.respond_to?(:deliver_later)
# Deliver later with Active Job's `deliver_later`
message = devise_mailer.send(notification, self, *args)
def render_and_send_devise_message(notification, *args)
end
@pending_devise_notifications ||= []
def pending_devise_notifications
end
pending_devise_notifications.clear
# could cause multiple emails to be sent.
# after_commit hook can be called multiple times which
# Empty the pending notifications array because the
end
render_and_send_devise_message(notification, *args)
pending_devise_notifications.each do |notification, args|
def send_pending_devise_notifications
private
end
end
render_and_send_devise_message(notification, *args)
else
pending_devise_notifications << [notification, args]
if new_record? || saved_changes?
# For Rails < 6 use `changed?` instead of `saved_changes?`.
# send now because after_commit will not be called.
# delivery until the after_commit callback otherwise
# If the record is new or changed then delay the
def send_devise_notification(notification, *args)
protected
after_commit :send_pending_devise_notifications
devise :database_authenticatable, :confirmable
class User
The following example uses Active Job's `deliver_later` :
deliveries until the after_commit callback is triggered.
you can override send_devise_notification to store the
just after the transaction was committed. To achieve this,
job, sidekiq, resque, etc), you must add the delivery to the queue
if you are using a queue to deliver e-mails (active job, delayed
need to customize the e-mail delivery logic. For instance,
to send a notification/mail. This can be overridden if you
This is an internal method called every time Devise needs
def send_devise_notification(notification, *args) message = devise_mailer.send(notification, self, *args) # Remove once we move to Rails 4.2+ only. if message.respond_to?(:deliver_now) message.deliver_now else message.deliver end end
def serializable_hash(options = nil)
and passing a new list of attributes you want to exempt. All attributes
are *not* accessible. You can remove this default by using :force_except
By default, it removes from the serializable model all attributes that
Redefine serializable_hash in models for more secure defaults.
def serializable_hash(options = nil) options = options.try(:dup) || {} options[:except] = Array(options[:except]).dup if options[:force_except] options[:except].concat Array(options[:force_except]) else options[:except].concat UNSAFE_ATTRIBUTES_FOR_SERIALIZATION end super(options) end
def strip_whitespace
def strip_whitespace self.class.strip_whitespace_keys.each { |k| apply_to_attribute_or_variable(k, :strip) } end
def unauthenticated_message
def unauthenticated_message :invalid end
def valid_for_authentication?
However, you should not overwrite this method, you should overwrite active_for_authentication?
if a model should be signed in or not.
find_for_authentication are the methods used in a Warden::Strategy to check
Check if the current object is valid for authentication. This method and
def valid_for_authentication? block_given? ? yield : true end