forked from fixin.me/fixin.me
Add web-based installation wizard
Replace the CLI-only setup (db:seed + manual application.rb edits) with a web wizard shown automatically on first visit when no admin account exists yet. SetupController (GET/POST /setup) collects the admin e-mail and password, a "skip e-mail confirmation" toggle, and an option to seed the built-in default units. Once submitted it creates the admin User, persists the chosen options as Setting records, and redirects to the sign-in page. ApplicationController gains a redirect_to_setup_if_needed before_action that catches every request (including Devise routes) when no admin exists, so a fresh installation always lands on the wizard rather than an empty sign-in form. A new Setting model provides a lightweight key-value store for runtime options that were previously hard-coded in application.rb (e.g. skip_email_confirmation). RegistrationsController now reads that flag from the database instead of from the application config. Seeds.rb is kept for headless / automated deployments and skips admin creation when an admin already exists (idempotent), with a comment pointing to the web wizard as the preferred path. Also extends the SQLite nil-limit fix (|| Float::INFINITY) to the Quantity model, which suffered the same ArgumentError as Unit. Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
This commit is contained in:
59
app/controllers/setup_controller.rb
Normal file
59
app/controllers/setup_controller.rb
Normal file
@@ -0,0 +1,59 @@
|
||||
# Handles the one-time web-based installation wizard.
|
||||
#
|
||||
# The wizard is only accessible when no admin account exists yet. Once an
|
||||
# admin has been created the controller redirects every request to the root
|
||||
# path, so it can never be used to overwrite an existing installation.
|
||||
class SetupController < ActionController::Base
|
||||
# Use the full application layout (header, flash, etc.) so the page looks
|
||||
# consistent with the rest of the site.
|
||||
layout "application"
|
||||
|
||||
before_action :redirect_if_installed
|
||||
|
||||
def new
|
||||
end
|
||||
|
||||
def create
|
||||
email = params[:admin_email].to_s.strip
|
||||
password = params[:admin_password].to_s
|
||||
confirm = params[:admin_password_confirmation].to_s
|
||||
|
||||
errors = []
|
||||
errors << t(".email_blank") if email.blank?
|
||||
errors << t(".password_blank") if password.blank?
|
||||
errors << t(".password_mismatch") if password != confirm
|
||||
|
||||
if errors.any?
|
||||
flash.now[:alert] = errors.join(" ")
|
||||
return render :new, status: :unprocessable_entity
|
||||
end
|
||||
|
||||
user = User.new(email: email, password: password, status: :admin)
|
||||
user.skip_confirmation!
|
||||
|
||||
unless user.save
|
||||
flash.now[:alert] = user.errors.full_messages.join(" ")
|
||||
return render :new, status: :unprocessable_entity
|
||||
end
|
||||
|
||||
# Persist runtime settings chosen during setup.
|
||||
Setting.set("skip_email_confirmation",
|
||||
params[:skip_email_confirmation] == "1")
|
||||
|
||||
# Optionally seed the built-in default units.
|
||||
if params[:seed_units] == "1"
|
||||
load Rails.root.join("db/seeds/units.rb")
|
||||
end
|
||||
|
||||
redirect_to new_user_session_path, notice: t(".success")
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def redirect_if_installed
|
||||
redirect_to root_path if User.exists?(status: :admin)
|
||||
rescue ActiveRecord::StatementInvalid
|
||||
# Tables are not yet migrated — stay on the setup page so the user sees a
|
||||
# meaningful error rather than a crash.
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user