Module: Devise::Models::Expirable::ClassMethods

Defined in:
lib/devise-security/models/expirable.rb

Instance Method Summary collapse

Instance Method Details

#delete_all_expiredObject

Version of #delete_all_expired_for without arguments (uses configured delete_expired_after default value).


120
121
122
# File 'lib/devise-security/models/expirable.rb', line 120

def delete_all_expired
  delete_all_expired_for(delete_expired_after)
end

#delete_all_expired_for(time) ⇒ Object

Sample method for daily cron to delete all expired entries after a given amount of time.

In your overwritten method you can “blank out” the object instead of deleting it.

*Word of warning*: You have to handle the dependent method on the resource relations (:destroy or :nullify) and catch this behavior (see api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html#label-Deleting+from+associations).

Examples:

Resource.delete_all_expired_for 90.days

You can overide this in your resource model

def self.delete_all_expired_for(time = 90.days)
  puts 'overwritten delete call'
end

Overwritten version to blank out the object.

def self.delete_all_expired_for(time = 90.days)
  expired_for(time).each do |u|
    u.update first_name: nil, last_name: nil
  end
end

113
114
115
# File 'lib/devise-security/models/expirable.rb', line 113

def delete_all_expired_for(time)
  expired_for(time).delete_all
end

#expired_for(time = delete_expired_after) ⇒ Object

Scope method to collect all expired users since time ago


87
88
89
# File 'lib/devise-security/models/expirable.rb', line 87

def expired_for(time = delete_expired_after)
  where('expired_at < ?', time.seconds.ago)
end

#mark_expiredObject

Sample method for daily cron to mark expired entries.

Examples:

You can overide this in your resource model

def self.mark_expired
  puts 'overwritten mark_expired'
end

79
80
81
82
83
84
# File 'lib/devise-security/models/expirable.rb', line 79

def mark_expired
  all.each do |u|
    u.expire! if u.expired? && u.expired_at.nil?
  end
  return
end