base.rb 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  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. end
  12. def to_run?
  13. return true if !@time_now
  14. return true if Time.zone.now.to_i > (@time_now + @ttl)
  15. false
  16. end
  17. def asset_push(record, assets)
  18. if !@time_now
  19. @time_now = Time.zone.now.to_i
  20. end
  21. class_name = record.class.to_s
  22. @asset_lookup[class_name] ||= {}
  23. @asset_lookup[class_name][record.id] = {
  24. updated_at: record.updated_at,
  25. pushed_at: @time_now,
  26. }
  27. record.assets(assets)
  28. end
  29. def asset_needed?(record)
  30. return false if !asset_needed_by_updated_at?(record.class.to_s, record.id, record.updated_at)
  31. true
  32. end
  33. def asset_needed_by_updated_at?(class_name, record_id, updated_at)
  34. return true if @asset_lookup.blank?
  35. return true if @asset_lookup[class_name].blank?
  36. return true if @asset_lookup[class_name][record_id].blank?
  37. return true if @asset_lookup[class_name][record_id][:updated_at] < updated_at
  38. return true if @asset_lookup[class_name][record_id][:pushed_at].blank?
  39. return true if @asset_lookup[class_name][record_id][:pushed_at] < @time_now - 7200
  40. false
  41. end
  42. end