neocities/models/stat_path.rb

21 lines
506 B
Ruby
Raw Permalink Normal View History

# frozen_string_literal: true
class StatPath < Sequel::Model
2015-05-02 14:34:21 -07:00
RETAINMENT_DAYS = 7
2015-05-02 02:34:24 -07:00
many_to_one :site
2015-05-02 14:34:21 -07:00
def self.prune!
where{created_at < (RETAINMENT_DAYS-2).days.ago.to_date}.delete
end
2015-05-02 02:34:24 -07:00
def self.create_or_get(site_id, name)
opts = {site_id: site_id, name: name}
2015-05-02 14:34:21 -07:00
stat_path = where(opts).where{created_at > RETAINMENT_DAYS.days.ago}.first
2015-05-02 02:34:24 -07:00
DB[table_name].lock('EXCLUSIVE') {
stat_path = create opts.merge created_at: Date.today
} if stat_path.nil?
stat_path
end
end