1
0
Fork 0
discourse-custom-wizard-unl.../lib/custom_wizard/log.rb

48 Zeilen
1 KiB
Ruby

2021-03-11 07:30:15 +01:00
# frozen_string_literal: true
2020-04-15 02:46:44 +02:00
class CustomWizard::Log
include ActiveModel::Serialization
2021-03-11 07:30:15 +01:00
2021-08-10 15:18:02 +02:00
attr_accessor :date, :wizard, :action, :user, :message
2021-03-11 07:30:15 +01:00
2020-04-15 02:46:44 +02:00
PAGE_LIMIT = 100
2021-03-11 07:30:15 +01:00
2020-04-15 02:46:44 +02:00
def initialize(attrs)
@date = attrs['date']
2021-08-10 15:18:02 +02:00
@wizard = attrs['wizard']
@action = attrs['action']
@user = attrs['user']
@message = attrs['message']
2020-04-15 02:46:44 +02:00
end
2021-03-11 07:30:15 +01:00
2021-08-10 15:18:02 +02:00
def self.create(wizard, action, user, message)
2020-04-15 02:46:44 +02:00
log_id = SecureRandom.hex(12)
2021-03-11 07:30:15 +01:00
2020-04-15 04:34:39 +02:00
PluginStore.set('custom_wizard_log',
log_id.to_s,
2020-04-15 02:46:44 +02:00
{
date: Time.now,
2021-08-10 15:18:02 +02:00
wizard: wizard,
action: action,
user: user,
2020-04-15 02:46:44 +02:00
message: message
}
)
end
2021-03-11 07:30:15 +01:00
2020-04-15 02:46:44 +02:00
def self.list_query
PluginStoreRow.where("
2020-04-15 04:34:39 +02:00
plugin_name = 'custom_wizard_log' AND
2020-04-15 02:46:44 +02:00
(value::json->'date') IS NOT NULL
").order("value::json->>'date' DESC")
end
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
def self.list(page = 0, limit = nil)
limit = limit.to_i > 0 ? limit.to_i : PAGE_LIMIT
page = page.to_i
2021-03-11 07:30:15 +01:00
2020-11-03 01:24:20 +01:00
self.list_query.limit(limit)
.offset(page * limit)
2020-04-15 02:46:44 +02:00
.map { |r| self.new(JSON.parse(r.value)) }
end
2021-03-11 07:30:15 +01:00
end