0
0
Fork 1
Spiegel von https://github.com/paviliondev/discourse-custom-wizard.git synchronisiert 2024-11-23 01:40:29 +01:00
discourse-custom-wizard/lib/custom_wizard/template.rb

121 Zeilen
3 KiB
Ruby

# frozen_string_literal: true
2020-10-31 08:05:50 +01:00
class CustomWizard::Template
2020-11-03 01:24:20 +01:00
include HasErrors
attr_reader :data,
:opts
def initialize(data)
@data = data
2020-10-31 08:05:50 +01:00
end
2020-11-03 01:24:20 +01:00
def save(opts={})
@opts = opts
2020-10-31 08:05:50 +01:00
2020-11-03 01:24:20 +01:00
normalize_data
validate_data
prepare_data
2020-10-31 08:05:50 +01:00
2020-11-03 01:24:20 +01:00
return false if errors.any?
2020-10-31 08:05:50 +01:00
ActiveRecord::Base.transaction do
2020-11-03 01:24:20 +01:00
schedule_save_jobs unless opts[:skip_jobs]
PluginStore.set(CustomWizard::PLUGIN_NAME, @data[:id], @data)
2020-10-31 08:05:50 +01:00
end
2020-11-03 01:24:20 +01:00
@data[:id]
end
def self.save(data, opts={})
new(data).save(opts)
end
def self.find(wizard_id)
PluginStore.get(CustomWizard::PLUGIN_NAME, wizard_id)
2020-10-31 08:05:50 +01:00
end
def self.remove(wizard_id)
2020-11-03 01:24:20 +01:00
wizard = CustomWizard::Wizard.create(wizard_id)
2020-10-31 08:05:50 +01:00
return false if !wizard
2020-11-03 01:24:20 +01:00
ActiveRecord::Base.transaction do
PluginStore.remove(CustomWizard::PLUGIN_NAME, wizard.id)
2020-11-03 01:24:20 +01:00
2020-10-31 08:05:50 +01:00
if wizard.after_time
Jobs.cancel_scheduled_job(:set_after_time_wizard)
2020-11-03 01:24:20 +01:00
Jobs.enqueue(:clear_after_time_wizard, wizard_id: wizard_id)
2020-10-31 08:05:50 +01:00
end
end
true
2020-10-31 08:05:50 +01:00
end
def self.exists?(wizard_id)
PluginStoreRow.exists?(plugin_name: 'custom_wizard', key: wizard_id)
end
2020-11-03 01:24:20 +01:00
def self.list(setting: nil, order: :id)
query = "plugin_name = 'custom_wizard'"
query += "AND (value::json ->> '#{setting}')::boolean IS TRUE" if setting
PluginStoreRow.where(query).order(order)
2020-10-31 08:05:50 +01:00
.reduce([]) do |result, record|
attrs = JSON.parse(record.value)
if attrs.present? &&
attrs.is_a?(Hash) &&
attrs['id'].present? &&
attrs['name'].present?
result.push(attrs)
end
result
end
end
2020-11-03 01:24:20 +01:00
private
def normalize_data
@data = ::JSON.parse(@data) if @data.is_a?(String)
@data = @data.with_indifferent_access
end
def prepare_data
@data[:steps].each do |step|
if step[:raw_description]
step[:description] = PrettyText.cook(step[:raw_description])
end
end
end
def validate_data
validator = CustomWizard::TemplateValidator.new(@data, @opts)
2020-11-03 01:24:20 +01:00
validator.perform
add_errors_from(validator)
end
def schedule_save_jobs
if @data[:after_time] && @data[:after_time_scheduled]
wizard_id = @data[:id]
old_data = CustomWizard::Template.find(data[:id])
begin
enqueue_wizard_at = Time.parse(@data[:after_time_scheduled]).utc
rescue ArgumentError
errors.add :validation, I18n.t("wizard.validation.after_time")
raise ActiveRecord::Rollback.new
end
if enqueue_wizard_at
Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard_id)
Jobs.enqueue_at(enqueue_wizard_at, :set_after_time_wizard, wizard_id: wizard_id)
elsif old_data && old_data[:after_time]
Jobs.cancel_scheduled_job(:set_after_time_wizard, wizard_id: wizard_id)
Jobs.enqueue(:clear_after_time_wizard, wizard_id: wizard_id)
end
end
2020-10-31 08:05:50 +01:00
end
end