2016-03-03 08:56:13 +00:00
|
|
|
class Sessions::Backend::Base
|
2017-01-31 17:13:45 +00:00
|
|
|
|
2018-06-29 17:04:39 +00:00
|
|
|
attr_writer :user
|
2018-08-06 09:43:40 +00:00
|
|
|
attr_writer :time_now
|
2018-06-29 17:04:39 +00:00
|
|
|
|
2017-06-16 20:43:09 +00:00
|
|
|
def initialize(user, asset_lookup, client, client_id, ttl = 10)
|
2016-03-03 08:56:13 +00:00
|
|
|
@user = user
|
|
|
|
@client = client
|
|
|
|
@client_id = client_id
|
|
|
|
@ttl = ttl
|
|
|
|
@asset_lookup = asset_lookup
|
|
|
|
@last_change = nil
|
2018-08-06 09:43:40 +00:00
|
|
|
@time_now = Time.zone.now.to_i
|
2016-03-03 08:56:13 +00:00
|
|
|
end
|
|
|
|
|
2017-10-18 00:09:45 +00:00
|
|
|
def asset_push(record, assets)
|
2016-03-03 08:56:13 +00:00
|
|
|
class_name = record.class.to_s
|
2017-10-18 00:09:45 +00:00
|
|
|
@asset_lookup[class_name] ||= {}
|
|
|
|
@asset_lookup[class_name][record.id] = {
|
|
|
|
updated_at: record.updated_at,
|
2018-12-19 17:31:51 +00:00
|
|
|
pushed_at: @time_now,
|
2017-10-18 00:09:45 +00:00
|
|
|
}
|
|
|
|
record.assets(assets)
|
|
|
|
end
|
|
|
|
|
|
|
|
def asset_needed?(record)
|
|
|
|
return false if !asset_needed_by_updated_at?(record.class.to_s, record.id, record.updated_at)
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2017-10-18 00:09:45 +00:00
|
|
|
true
|
|
|
|
end
|
2017-05-02 03:24:05 +00:00
|
|
|
|
2017-10-18 00:09:45 +00:00
|
|
|
def asset_needed_by_updated_at?(class_name, record_id, updated_at)
|
|
|
|
return true if @asset_lookup.blank?
|
|
|
|
return true if @asset_lookup[class_name].blank?
|
|
|
|
return true if @asset_lookup[class_name][record_id].blank?
|
|
|
|
return true if @asset_lookup[class_name][record_id][:updated_at] < updated_at
|
|
|
|
return true if @asset_lookup[class_name][record_id][:pushed_at].blank?
|
2018-08-06 09:43:40 +00:00
|
|
|
return true if @asset_lookup[class_name][record_id][:pushed_at] < @time_now - 7200
|
2018-10-09 06:17:41 +00:00
|
|
|
|
2016-03-03 08:56:13 +00:00
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
end
|