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

Defined in:
lib/devise_security_extension/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).



114
115
116
# File 'lib/devise_security_extension/models/expirable.rb', line 114

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_attributes first_name: nil, last_name: nil
  end
end


107
108
109
# File 'lib/devise_security_extension/models/expirable.rb', line 107

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



81
82
83
# File 'lib/devise_security_extension/models/expirable.rb', line 81

def expired_for(time = delete_expired_after)
  where('expired_at < ?', time.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


73
74
75
76
77
78
# File 'lib/devise_security_extension/models/expirable.rb', line 73

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