discourse/lib/wizard/step_updater.rb

97 lines
2.7 KiB
Ruby
Raw Normal View History

2016-08-25 12:14:56 -05:00
class Wizard
class StepUpdater
include ActiveModel::Model
2016-08-25 12:14:56 -05:00
def initialize(current_user, id)
@current_user = current_user
@id = id
2016-09-07 17:04:01 -05:00
@refresh_required = false
2016-08-25 12:14:56 -05:00
end
def update(fields)
updater_method = "update_#{@id.underscore}".to_sym
send(updater_method, fields.symbolize_keys) if respond_to?(updater_method)
2016-08-25 12:14:56 -05:00
end
2016-09-07 17:04:01 -05:00
def update_locale(fields)
old_locale = SiteSetting.default_locale
2016-09-09 14:57:44 -05:00
update_setting_field(:default_locale, fields, :default_locale)
2016-09-07 17:04:01 -05:00
@refresh_required = true if old_locale != fields[:default_locale]
end
2016-09-09 14:57:44 -05:00
def update_privacy(fields)
update_setting(:login_required, fields[:privacy] == 'restricted')
update_setting(:invite_only, fields[:privacy] == 'restricted')
end
2016-08-25 12:14:56 -05:00
def update_forum_title(fields)
2016-09-09 14:57:44 -05:00
update_setting_field(:title, fields, :title)
update_setting_field(:site_description, fields, :site_description)
2016-08-25 12:14:56 -05:00
end
def update_contact(fields)
2016-09-09 14:57:44 -05:00
update_setting_field(:contact_email, fields, :contact_email)
update_setting_field(:contact_url, fields, :contact_url)
update_setting_field(:site_contact_username, fields, :site_contact_username)
end
2016-09-02 10:42:14 -05:00
def update_colors(fields)
2016-09-07 17:04:01 -05:00
scheme_name = fields[:theme_id]
2016-09-02 10:42:14 -05:00
theme = ColorScheme.themes.find {|s| s[:id] == scheme_name }
colors = []
theme[:colors].each do |name, hex|
colors << {name: name, hex: hex[1..-1] }
end
attrs = {
enabled: true,
name: I18n.t("wizard.step.colors.fields.color_scheme.options.#{scheme_name}"),
2016-09-07 17:04:01 -05:00
colors: colors,
theme_id: scheme_name
2016-09-02 10:42:14 -05:00
}
scheme = ColorScheme.where(via_wizard: true).first
if scheme.present?
attrs[:colors] = colors
revisor = ColorSchemeRevisor.new(scheme, attrs)
revisor.revise
else
attrs[:via_wizard] = true
scheme = ColorScheme.new(attrs)
scheme.save!
end
end
2016-09-08 15:58:07 -05:00
def update_logos(fields)
2016-09-09 14:57:44 -05:00
update_setting_field(:logo_url, fields, :logo_url)
update_setting_field(:logo_small_url, fields, :logo_small_url)
update_setting_field(:favicon_url, fields, :favicon_url)
update_setting_field(:apple_touch_icon_url, fields, :apple_touch_icon_url)
2016-09-08 15:58:07 -05:00
end
2016-08-25 12:14:56 -05:00
def success?
@errors.blank?
end
2016-09-07 17:04:01 -05:00
def refresh_required?
@refresh_required
end
2016-08-25 12:14:56 -05:00
protected
2016-09-09 14:57:44 -05:00
def update_setting(id, value)
2016-08-25 12:14:56 -05:00
value.strip! if value.is_a?(String)
SiteSetting.set_and_log(id, value, @current_user) if SiteSetting.send(id) != value
2016-09-09 14:57:44 -05:00
end
def update_setting_field(id, fields, field_id)
update_setting(id, fields[field_id])
2016-08-25 12:14:56 -05:00
rescue Discourse::InvalidParameters => e
errors.add(field_id, e.message)
2016-08-25 12:14:56 -05:00
end
end
end