discourse/lib/wizard/step_updater.rb

59 lines
1.3 KiB
Ruby
Raw Normal View History

# frozen_string_literal: true
2016-08-26 01:14:56 +08:00
class Wizard
class StepUpdater
include ActiveModel::Model
2016-08-26 01:14:56 +08:00
attr_accessor :refresh_required, :fields
def initialize(current_user, step, fields)
2016-08-26 01:14:56 +08:00
@current_user = current_user
@step = step
2016-09-08 06:04:01 +08:00
@refresh_required = false
@fields = fields
2016-08-26 01:14:56 +08:00
end
def update
@step.updater.call(self) if @step.present? && @step.updater.present?
if success?
logger = StaffActionLogger.new(@current_user)
logger.log_wizard_step(@step)
end
2016-09-09 04:58:07 +08:00
end
2016-08-26 01:14:56 +08:00
def success?
@errors.blank?
end
2016-09-08 06:04:01 +08:00
def refresh_required?
@refresh_required
end
def update_setting(id, value)
value = value.strip if value.is_a?(String)
2018-11-14 15:03:02 +08:00
if !value.is_a?(Upload) && SiteSetting.type_supervisor.get_type(id) == :upload
value = Upload.get_from_url(value) || ''
end
SiteSetting.set_and_log(id, value, @current_user) if SiteSetting.get(id) != value
end
2016-09-10 03:57:44 +08:00
def apply_setting(id)
update_setting(id, @fields[id])
rescue Discourse::InvalidParameters => e
errors.add(id, e.message)
end
def ensure_changed(id)
errors.add(id, '') if @fields[id] == SiteSetting.defaults[id]
end
def apply_settings(*ids)
2017-07-28 09:20:09 +08:00
ids.each { |id| apply_setting(id) }
end
2016-08-26 01:14:56 +08:00
end
end