Add locale step

This commit is contained in:
Robin Ward
2016-09-07 18:04:01 -04:00
parent 3f6e3b9aff
commit c94e6f1b96
25 changed files with 746 additions and 90 deletions

View File

@@ -9,11 +9,15 @@ class Wizard
@steps = []
end
def create_step(args)
Step.new(args)
def create_step(step_name)
Step.new(step_name)
end
def append_step(step)
step = create_step(step) if step.is_a?(String)
yield step if block_given?
last_step = @steps.last
@steps << step
@@ -31,26 +35,40 @@ class Wizard
def self.build
wizard = Wizard.new
title = wizard.create_step('forum-title')
title.add_field(id: 'title', type: 'text', required: true, value: SiteSetting.title)
title.add_field(id: 'site_description', type: 'text', required: true, value: SiteSetting.site_description)
wizard.append_step(title)
contact = wizard.create_step('contact')
contact.add_field(id: 'contact_email', type: 'text', required: true, value: SiteSetting.contact_email)
contact.add_field(id: 'contact_url', type: 'text', value: SiteSetting.contact_url)
contact.add_field(id: 'site_contact_username', type: 'text', value: SiteSetting.site_contact_username)
wizard.append_step(contact)
wizard.append_step('locale') do |step|
languages = step.add_field(id: 'default_locale',
type: 'dropdown',
required: true,
value: SiteSetting.default_locale)
theme = wizard.create_step('colors')
scheme = theme.add_field(id: 'color_scheme', type: 'dropdown', required: true)
ColorScheme.themes.each {|t| scheme.add_option(t[:id], t) }
LocaleSiteSetting.values.each do |locale|
languages.add_choice(locale[:value], label: locale[:name])
end
end
theme.add_field(id: 'scheme_preview', type: 'component')
wizard.append_step(theme)
wizard.append_step('forum-title') do |step|
step.add_field(id: 'title', type: 'text', required: true, value: SiteSetting.title)
step.add_field(id: 'site_description', type: 'text', required: true, value: SiteSetting.site_description)
end
finished = wizard.create_step('finished')
wizard.append_step(finished);
wizard.append_step('contact') do |step|
step.add_field(id: 'contact_email', type: 'text', required: true, value: SiteSetting.contact_email)
step.add_field(id: 'contact_url', type: 'text', value: SiteSetting.contact_url)
step.add_field(id: 'site_contact_username', type: 'text', value: SiteSetting.site_contact_username)
end
wizard.append_step('colors') do |step|
theme_id = ColorScheme.where(via_wizard: true).pluck(:theme_id)
theme_id = theme_id.present? ? theme_id[0] : 'default'
themes = step.add_field(id: 'theme_id', type: 'dropdown', required: true, value: theme_id)
ColorScheme.themes.each {|t| themes.add_choice(t[:id], data: t) }
step.add_field(id: 'theme_preview', type: 'component')
end
wizard.append_step('finished')
wizard
end

View File

@@ -1,7 +1,18 @@
class Wizard
class Field
attr_reader :id, :type, :required, :value, :options, :option_data
class Choice
attr_reader :id, :label, :data
attr_accessor :field
def initialize(id, opts)
@id = id
@data = opts[:data]
@label = opts[:label]
end
end
class Field
attr_reader :id, :type, :required, :value, :choices
attr_accessor :step
def initialize(attrs)
@@ -11,13 +22,15 @@ class Wizard
@type = attrs[:type]
@required = !!attrs[:required]
@value = attrs[:value]
@options = []
@option_data = {}
@choices = []
end
def add_option(id, data=nil)
@options << id
@option_data[id] = data
def add_choice(id, opts=nil)
choice = Choice.new(id, opts || {})
choice.field = self
@choices << choice
choice
end
end

View File

@@ -5,6 +5,7 @@ class Wizard
def initialize(current_user, id)
@current_user = current_user
@id = id
@refresh_required = false
end
def update(fields)
@@ -12,6 +13,12 @@ class Wizard
send(updater_method, fields.symbolize_keys) if respond_to?(updater_method)
end
def update_locale(fields)
old_locale = SiteSetting.default_locale
update_setting(:default_locale, fields, :default_locale)
@refresh_required = true if old_locale != fields[:default_locale]
end
def update_forum_title(fields)
update_setting(:title, fields, :title)
update_setting(:site_description, fields, :site_description)
@@ -24,7 +31,7 @@ class Wizard
end
def update_colors(fields)
scheme_name = fields[:color_scheme]
scheme_name = fields[:theme_id]
theme = ColorScheme.themes.find {|s| s[:id] == scheme_name }
@@ -36,7 +43,8 @@ class Wizard
attrs = {
enabled: true,
name: I18n.t("wizard.step.colors.fields.color_scheme.options.#{scheme_name}"),
colors: colors
colors: colors,
theme_id: scheme_name
}
scheme = ColorScheme.where(via_wizard: true).first
@@ -55,6 +63,10 @@ class Wizard
@errors.blank?
end
def refresh_required?
@refresh_required
end
protected
def update_setting(id, fields, field_id)