Skip to content

Commit 682166f

Browse files
committed
feat: enhance setup wizard with improved error handling and localized messages for admin and platform creation
1 parent a0d56b1 commit 682166f

File tree

8 files changed

+54
-27
lines changed

8 files changed

+54
-27
lines changed

app/controllers/better_together/setup_wizard_steps_controller.rb

Lines changed: 21 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -37,18 +37,20 @@ def create_host_platform # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
3737
mark_current_step_as_completed
3838
wizard.reload
3939
determine_wizard_outcome
40-
else
41-
flash.now[:alert] = t('.flash.please_address_errors')
42-
render wizard_step_definition.template
40+
return
4341
end
4442
end
45-
else
46-
flash.now[:alert] = t('.flash.please_address_errors')
47-
render wizard_step_definition.template
4843
end
44+
45+
# If we get here, validation or save failed
46+
@platform = base_platform
47+
@platform.assign_attributes(platform_params) if platform_params.present?
48+
flash.now[:alert] = t('.flash.please_address_errors')
49+
render wizard_step_definition.template, status: :unprocessable_entity
4950
rescue ActiveRecord::RecordInvalid => e
51+
@platform = e.record
5052
flash.now[:alert] = e.record.errors.full_messages.to_sentence
51-
render wizard_step_definition.template
53+
render wizard_step_definition.template, status: :unprocessable_entity
5254
end
5355
# rubocop:enable Metrics/MethodLength
5456

@@ -73,9 +75,7 @@ def create_admin # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
7375

7476
if @form.validate(user_params)
7577
ActiveRecord::Base.transaction do
76-
# byebug
7778
user = ::BetterTogether::User.new(user_params)
78-
user.build_person(person_params)
7979

8080
if user.save!
8181
helpers.host_platform.person_platform_memberships.create!(
@@ -91,24 +91,24 @@ def create_admin # rubocop:todo Metrics/AbcSize, Metrics/MethodLength
9191
helpers.host_community.creator = user.person
9292
helpers.host_community.save!
9393

94-
# If Devise's :confirmable is enabled, this will send a confirmation email
95-
user.send_confirmation_instructions(confirmation_url: user_confirmation_path)
9694
mark_current_step_as_completed
9795
wizard.reload
9896
determine_wizard_outcome
99-
else
100-
# Handle the case where the user could not be saved
101-
flash.now[:alert] = user.errors.full_messages.to_sentence
102-
render wizard_step_definition.template
97+
return
10398
end
10499
end
105-
else
106-
flash.now[:alert] = t('.flash.please_address_errors')
107-
render wizard_step_definition.template
108100
end
101+
102+
# If we get here, validation or save failed
103+
@user = ::BetterTogether::User.new(user_params)
104+
@user.build_person unless @user.person
105+
flash.now[:alert] = t('.flash.please_address_errors')
106+
render wizard_step_definition.template, status: :unprocessable_entity
109107
rescue ActiveRecord::RecordInvalid => e
108+
@user = e.record
109+
@user.build_person unless @user.person
110110
flash.now[:alert] = e.record.errors.full_messages.to_sentence
111-
render wizard_step_definition.template
111+
render wizard_step_definition.template, status: :unprocessable_entity
112112
end
113113
# rubocop:enable Metrics/MethodLength
114114

@@ -134,13 +134,10 @@ def platform_params
134134
params.require(:platform).permit(:name, :description, :url, :time_zone, :privacy)
135135
end
136136

137-
def person_params
138-
params.require(:user).require(:person_attributes).permit(%i[identifier name description])
139-
end
140-
141137
def user_params
142138
params.require(:user).permit(
143-
:email, :password, :password_confirmation
139+
:email, :password, :password_confirmation,
140+
person_attributes: %i[identifier name description]
144141
)
145142
end
146143

app/forms/better_together/host_platform_admin_form.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,9 @@ class HostPlatformAdminForm < ::Reform::Form
1010
property :password
1111
property :password_confirmation
1212

13-
property :person do
13+
property :person, populator: lambda { |_options|
14+
self.person = model.person || model.build_person
15+
} do
1416
property :identifier
1517
property :description
1618
property :name

app/views/better_together/wizard_step_definitions/host_setup/admin_creation.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<% min_password_length = Devise.password_length.min %>
22

3-
<%= form_for @form, url: setup_wizard_step_create_admin_path(wizard_id: 'host_setup', wizard_step_definition_id: :admin_creation), method: :post, class: 'needs-validation form', id: 'admin-creation-form', data: { turbo: false, controller: 'better_together--form-validation', action: 'submit->better_together--form-validation#validateBeforeSubmit' } do |f| %>
3+
<%= form_for @form, url: setup_wizard_step_create_admin_path(wizard_id: 'host_setup', wizard_step_definition_id: :admin_creation), method: :post, class: 'needs-validation form', id: 'admin-creation-form', data: { controller: 'better_together--form-validation' } do |f| %>
44

55
<!-- Hero Section -->
66
<div class="container-fluid p-0">

app/views/better_together/wizard_step_definitions/host_setup/platform_details.html.erb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@
4949
</div>
5050
<% end %>
5151

52-
<%= form_for @form, url: setup_wizard_step_create_host_platform_path, method: :post, class: 'needs-validation', novalidate: true do |f| %>
52+
<%= form_for @form, url: setup_wizard_step_create_host_platform_path, method: :post, class: 'needs-validation', data: { controller: 'better_together--form-validation' } do |f| %>
5353
<div class="mb-3">
5454
<%= f.label :name, t('.fields.name.label'), class: 'form-label' %>
5555
<%= f.text_field :name, autofocus: true, placeholder: t('.fields.name.placeholder'), class: "form-control#{' is-invalid' if @form.errors[:name].any?}", required: true %>

config/locales/en.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1461,6 +1461,13 @@ en:
14611461
platform: Platform
14621462
privacy: Privacy
14631463
title: Settings
1464+
setup_wizard_steps:
1465+
create_admin:
1466+
flash:
1467+
please_address_errors: Please address the errors below.
1468+
create_host_platform:
1469+
flash:
1470+
please_address_errors: Please address the errors below.
14641471
share_buttons:
14651472
aria_label: Share on %{platform}
14661473
bluesky: Share on Bluesky

config/locales/es.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1473,6 +1473,13 @@ es:
14731473
platform: Plataforma
14741474
privacy: Privacidad
14751475
title: Configuración
1476+
setup_wizard_steps:
1477+
create_admin:
1478+
flash:
1479+
please_address_errors: Por favor, corrija los errores a continuación.
1480+
create_host_platform:
1481+
flash:
1482+
please_address_errors: Por favor, corrija los errores a continuación.
14761483
share_buttons:
14771484
aria_label: Compartir en %{platform}
14781485
bluesky: Compartir en Bluesky

config/locales/fr.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,13 @@ fr:
14801480
platform: Plateforme
14811481
privacy: Confidentialité
14821482
title: Paramètres
1483+
setup_wizard_steps:
1484+
create_admin:
1485+
flash:
1486+
please_address_errors: Veuillez corriger les erreurs ci-dessous.
1487+
create_host_platform:
1488+
flash:
1489+
please_address_errors: Veuillez corriger les erreurs ci-dessous.
14831490
share_buttons:
14841491
aria_label: Partager sur %{platform}
14851492
bluesky: Partager sur Bluesky

config/locales/uk.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1476,6 +1476,13 @@ uk:
14761476
platform: Platform
14771477
privacy: Privacy
14781478
title: Settings
1479+
setup_wizard_steps:
1480+
create_admin:
1481+
flash:
1482+
please_address_errors: Будь ласка, виправте помилки нижче.
1483+
create_host_platform:
1484+
flash:
1485+
please_address_errors: Будь ласка, виправте помилки нижче.
14791486
share_buttons:
14801487
aria_label: Share on %{platform}
14811488
bluesky: Share on Bluesky

0 commit comments

Comments
 (0)