trabajo-afectivo/app/models/stats_store.rb

56 lines
911 B
Ruby
Raw Normal View History

2022-01-01 13:38:12 +00:00
# Copyright (C) 2012-2022 Zammad Foundation, https://zammad-foundation.org/
2015-09-06 22:42:11 +00:00
class StatsStore < ApplicationModel
include HasSearchIndexBackend
belongs_to :stats_storable, polymorphic: true
2015-09-06 22:42:11 +00:00
store :data
2015-09-06 22:42:11 +00:00
=begin
2016-10-06 16:59:40 +00:00
item = StatsStore.sync(
stats_storable: current_user,
key: 'dashboard',
data: {some data},
2015-09-06 22:42:11 +00:00
)
=end
def self.sync(params)
data = params[:data]
params.delete(:data)
item = find_by(params)
2015-09-06 22:42:11 +00:00
if item
item.data = data
item.save
return item
2015-09-06 22:42:11 +00:00
end
params[:data] = data
params[:created_by_id] = 1
create(params)
end
=begin
cleanup old stats store
StatsStore.cleanup
optional you can put the max oldest stats store entries as argument
2015-09-06 22:42:11 +00:00
StatsStore.cleanup(12.months)
2015-09-06 22:42:11 +00:00
=end
def self.cleanup(diff = 12.months)
2015-09-08 12:11:03 +00:00
StatsStore.where('updated_at < ?', Time.zone.now - diff).delete_all
2015-09-06 22:42:11 +00:00
true
end
end