session_helper.rb 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051
  1. module SessionHelper
  2. def self.default_collections(user, assets = {})
  3. # auto population collections, store all here
  4. default_collection = {}
  5. # load collections to deliver from external files
  6. dir = File.expand_path('../../', __FILE__)
  7. files = Dir.glob( "#{dir}/app/controllers/sessions/collection_*.rb")
  8. files.each do |file|
  9. load file
  10. (default_collection, assets) = ExtraCollection.session(default_collection, assets, user)
  11. end
  12. [default_collection, assets]
  13. end
  14. def self.models(user = nil)
  15. models = {}
  16. objects = ObjectManager.list_objects
  17. objects.each do |object|
  18. attributes = ObjectManager::Attribute.by_object(object, user)
  19. models[object] = attributes
  20. end
  21. models
  22. end
  23. def self.cleanup_expired
  24. # delete temp. sessions
  25. ActiveRecord::SessionStore::Session.where('persistent IS NULL AND updated_at < ?', Time.zone.now - 2.hours).delete_all
  26. # web sessions not updated the last x days
  27. ActiveRecord::SessionStore::Session.where('updated_at < ?', Time.zone.now - 60.days).delete_all
  28. end
  29. def self.get(id)
  30. ActiveRecord::SessionStore::Session.find_by(id: id)
  31. end
  32. def self.list(limit = 10_000)
  33. ActiveRecord::SessionStore::Session.order('updated_at DESC').limit(limit)
  34. end
  35. def self.destroy(id)
  36. session = ActiveRecord::SessionStore::Session.find_by(id: id)
  37. return if !session
  38. session.destroy
  39. end
  40. end