base.rb 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. class Sessions::Backend::Base
  2. attr_writer :user
  3. attr_writer :time_now
  4. def initialize(user, asset_lookup, client, client_id, ttl = 10)
  5. @user = user
  6. @client = client
  7. @client_id = client_id
  8. @ttl = ttl
  9. @asset_lookup = asset_lookup
  10. @last_change = nil
  11. @time_now = Time.zone.now.to_i
  12. end
  13. def asset_push(record, assets)
  14. class_name = record.class.to_s
  15. @asset_lookup[class_name] ||= {}
  16. @asset_lookup[class_name][record.id] = {
  17. updated_at: record.updated_at,
  18. pushed_at: @time_now,
  19. }
  20. record.assets(assets)
  21. end
  22. def asset_needed?(record)
  23. return false if !asset_needed_by_updated_at?(record.class.to_s, record.id, record.updated_at)
  24. true
  25. end
  26. def asset_needed_by_updated_at?(class_name, record_id, updated_at)
  27. return true if @asset_lookup.blank?
  28. return true if @asset_lookup[class_name].blank?
  29. return true if @asset_lookup[class_name][record_id].blank?
  30. return true if @asset_lookup[class_name][record_id][:updated_at] < updated_at
  31. return true if @asset_lookup[class_name][record_id][:pushed_at].blank?
  32. return true if @asset_lookup[class_name][record_id][:pushed_at] < @time_now - 7200
  33. false
  34. end
  35. end