Class: Wizard::Builder

Inherits:
Object
  • Object
show all
Defined in:
lib/wizard/builder.rb

Instance Method Summary collapse

Constructor Details

#initialize(user) ⇒ Builder

Returns a new instance of Builder.



5
6
7
# File 'lib/wizard/builder.rb', line 5

def initialize(user)
  @wizard = Wizard.new(user)
end

Instance Method Details

#buildObject



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
# File 'lib/wizard/builder.rb', line 9

def build
  return @wizard unless SiteSetting.wizard_enabled? && @wizard.user.try(:staff?)

  @wizard.append_step("introduction") do |step|
    step.emoji = "wave"
    step.description_vars = { base_path: Discourse.base_path }

    step.add_field(
      id: "title",
      type: "text",
      required: true,
      value: SiteSetting.title == SiteSetting.defaults[:title] ? "" : SiteSetting.title,
    )
    step.add_field(
      id: "site_description",
      type: "text",
      required: false,
      value: SiteSetting.site_description,
    )

    languages =
      step.add_field(
        id: "default_locale",
        type: "dropdown",
        required: false,
        value: SiteSetting.default_locale,
      )

    LocaleSiteSetting.values.each do |locale|
      languages.add_choice(locale[:value], label: locale[:name])
    end

    step.on_update do |updater|
      updater.ensure_changed(:title)

      updater.apply_settings(:title, :site_description) if updater.errors.blank?

      old_locale = SiteSetting.default_locale
      updater.apply_setting(:default_locale)

      if old_locale != updater.fields[:default_locale]
        Scheduler::Defer.later "Reseed" do
          SeedData::Categories.with_default_locale.update(skip_changed: true)
          SeedData::Topics.with_default_locale.update(skip_changed: true)
        end

        updater.refresh_required = true
      end
    end
  end

  @wizard.append_step("privacy") do |step|
    step.emoji = "hugs"
    step.add_field(
      id: "login_required",
      type: "checkbox",
      icon: "unlock",
      value: SiteSetting.,
    )

    step.add_field(
      id: "invite_only",
      type: "checkbox",
      icon: "user-plus",
      value: SiteSetting.invite_only,
    )

    step.add_field(
      id: "must_approve_users",
      type: "checkbox",
      icon: "user-shield",
      value: SiteSetting.must_approve_users,
    )

    if defined?(::Chat)
      step.add_field(
        id: "chat_enabled",
        type: "checkbox",
        icon: "d-chat",
        value: SiteSetting.chat_enabled,
      )
    end

    step.add_field(
      id: "enable_sidebar",
      type: "checkbox",
      icon: "bars",
      value: SiteSetting.navigation_menu == NavigationMenuSiteSetting::SIDEBAR,
    )

    step.on_update do |updater|
      updater.update_setting(:login_required, updater.fields[:login_required])
      updater.update_setting(:invite_only, updater.fields[:invite_only])
      updater.update_setting(:must_approve_users, updater.fields[:must_approve_users])
      updater.update_setting(:chat_enabled, updater.fields[:chat_enabled]) if defined?(::Chat)
      updater.update_setting(:navigation_menu, updater.fields[:enable_sidebar])
    end
  end

  @wizard.append_step("ready") do |step|
    # no form on this page, just info.
    step.emoji = "rocket"
  end

  @wizard.append_step("branding") do |step|
    step.emoji = "framed_picture"
    step.add_field(id: "logo", type: "image", value: SiteSetting.site_logo_url)
    step.add_field(id: "logo_small", type: "image", value: SiteSetting.site_logo_small_url)

    step.on_update do |updater|
      if SiteSetting.site_logo_url != updater.fields[:logo] ||
           SiteSetting.site_logo_small_url != updater.fields[:logo_small]
        updater.apply_settings(:logo, :logo_small)
        updater.refresh_required = true
      end
    end
  end

  @wizard.append_step("styling") do |step|
    step.emoji = "art"
    default_theme = Theme.find_by(id: SiteSetting.default_theme_id)
    default_theme_override = SiteSetting.exists?(name: "default_theme_id")

    base_scheme = default_theme&.color_scheme&.base_scheme_id
    color_scheme_name = default_theme&.color_scheme&.name

    scheme_id =
      default_theme_override ? (base_scheme || color_scheme_name) : ColorScheme::LIGHT_THEME_ID

    themes =
      step.add_field(
        id: "color_scheme",
        type: "dropdown",
        required: !default_theme_override,
        value: scheme_id || ColorScheme::LIGHT_THEME_ID,
        show_in_sidebar: true,
      )

    # fix for the case when base_scheme is nil
    if scheme_id && default_theme_override && base_scheme.nil?
      scheme = default_theme.color_scheme
      themes.add_choice(scheme_id, data: { colors: scheme.colors_hashes })
    end

    ColorScheme.base_color_scheme_colors.each do |t|
      themes.add_choice(t[:id], data: { colors: t[:colors] })
    end

    body_font =
      step.add_field(
        id: "body_font",
        type: "dropdown",
        value: SiteSetting.base_font,
        show_in_sidebar: true,
      )

    heading_font =
      step.add_field(
        id: "heading_font",
        type: "dropdown",
        value: SiteSetting.heading_font,
        show_in_sidebar: true,
      )

    DiscourseFonts.fonts.each do |font|
      body_font.add_choice(font[:key], label: font[:name])
      heading_font.add_choice(font[:key], label: font[:name])
    end

    current =
      (
        if SiteSetting.top_menu.starts_with?("categories")
          SiteSetting.desktop_category_page_style
        else
          "latest"
        end
      )
    style =
      step.add_field(
        id: "homepage_style",
        type: "dropdown",
        required: false,
        value: current,
        show_in_sidebar: true,
      )
    style.add_choice("latest")
    CategoryPageStyle.values.each { |page| style.add_choice(page[:value]) }

    step.add_field(id: "styling_preview", type: "component")

    step.on_update do |updater|
      updater.update_setting(:base_font, updater.fields[:body_font])
      updater.update_setting(:heading_font, updater.fields[:heading_font])

      top_menu = SiteSetting.top_menu.split("|")
      if updater.fields[:homepage_style] == "latest" && top_menu[0] != "latest"
        top_menu.delete("latest")
        top_menu.insert(0, "latest")
      elsif updater.fields[:homepage_style] != "latest"
        top_menu.delete("categories")
        top_menu.insert(0, "categories")
        updater.update_setting(:desktop_category_page_style, updater.fields[:homepage_style])
      end
      updater.update_setting(:top_menu, top_menu.join("|"))

      scheme_name = ((updater.fields[:color_scheme] || "") || ColorScheme::LIGHT_THEME_ID)

      next unless scheme_name.present? && ColorScheme.is_base?(scheme_name)

      name = I18n.t("color_schemes.#{scheme_name.downcase.gsub(" ", "_")}_theme_name")

      scheme = ColorScheme.find_by(base_scheme_id: scheme_name, via_wizard: true)
      scheme ||=
        ColorScheme.create_from_base(name: name, via_wizard: true, base_scheme_id: scheme_name)

      if default_theme
        default_theme.color_scheme_id = scheme.id
        default_theme.save!
      else
        theme =
          Theme.create!(
            name: I18n.t("color_schemes.default_theme_name"),
            user_id: @wizard.user.id,
            color_scheme_id: scheme.id,
          )

        theme.set_default!
      end

      updater.update_setting(:default_dark_mode_color_scheme_id, -1) if scheme.is_dark?
      updater.refresh_required = true
    end
  end

  @wizard.append_step("corporate") do |step|
    step.emoji = "briefcase"
    step.description_vars = { base_path: Discourse.base_path }
    step.add_field(id: "company_name", type: "text", value: SiteSetting.company_name)
    step.add_field(id: "governing_law", type: "text", value: SiteSetting.governing_law)
    step.add_field(id: "contact_url", type: "text", value: SiteSetting.contact_url)
    step.add_field(id: "city_for_disputes", type: "text", value: SiteSetting.city_for_disputes)
    step.add_field(id: "contact_email", type: "text", value: SiteSetting.contact_email)

    step.on_update do |updater|
      update_tos do |raw|
        replace_setting_value(updater, raw, "company_name")
        replace_setting_value(updater, raw, "governing_law")
        replace_setting_value(updater, raw, "city_for_disputes")
      end

      if updater.errors.blank?
        updater.apply_settings(
          :company_name,
          :governing_law,
          :city_for_disputes,
          :contact_url,
          :contact_email,
        )
      end
    end
  end

  DiscourseEvent.trigger(:build_wizard, @wizard)
  @wizard
end