fix: auto-convert project name in settings & consolidate code#4436
fix: auto-convert project name in settings & consolidate code#4436
Conversation
The project creation form auto-converts uppercase and spaces to the lowercase-hyphens-digits format, but the settings form did not, causing users to get a validation error when entering names like "DEP-burundi-datafi". Aligns the settings form with the creation form by using a raw_name input that gets converted via Helpers.url_safe_name/1 before saving.
The project name input was changed from `name` to `raw_name` for the url-safe conversion feature, but two test assertions still referenced the old field name.
Codecov Report✅ All modified and coverable lines are covered by tests. Additional details and impacted files@@ Coverage Diff @@
## main #4436 +/- ##
==========================================
+ Coverage 89.33% 89.43% +0.10%
==========================================
Files 425 425
Lines 20133 20147 +14
==========================================
+ Hits 17985 18018 +33
+ Misses 2148 2129 -19 ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
stuartc
left a comment
There was a problem hiding this comment.
Some issues, mostly around the validation not being tested - and errors not shown probably (raw_name doesn't have a schema so I can't see it getting errors?).
Also there is a smell around the copy pasting of coerce_raw_name_to_safe_name, not just in this PR, in other places. There are 5 different files with an almost copy paste of this kind of function. I first thought we should put the transform into the schema, but then thought that might cause rendering weirdness with the schema changing the input - Collection has a form_changeset function that handles the :raw_name and the model has that as a virtual field. Keeping the logic a model concern and not a view concern.
Replace 5 duplicate `coerce_raw_name_to_safe_name` functions with `Project.form_changeset/2` and `form_with_users_changeset/2` that use a virtual `:raw_name` field to derive the URL-safe `:name` automatically. - Add `Helpers.derive_name_param/1` so all save handlers derive the name server-side instead of relying on hidden-field timing - Fix duplicate "can't be blank" error by moving `validate_required([:name])` out of shared `validate/1` into callers that cast `:name` directly - Fix dashboard creation modal error path assigning to wrong key (`:project_changeset` → `:changeset`) - Extract shared `<.name_badge>` component in Pills for the name preview badge used across all 5 forms
Add tests for previously untested code paths: - settings cancel-retention-change handler resets form - settings/dashboard/admin save error paths with copy_error - sandbox edit save error path with copy_error - fix broken changeset construction in sandbox edit stub
There was a problem hiding this comment.
Thanks @stuartc, great catch on all fronts. I've reworked the PR following your suggestion to use the Collection pattern. Project now has a virtual :raw_name field and a form_changeset/2 that handles the derivation at the schema level, so all copies of coerce_raw_name_to_safe_name are gone. Errors on :name get copied back to :raw_name so they show up on the visible input. Also extracted a shared <.name_badge> component since the preview badge was duplicated across all the forms. I think this now looks way better, thanks a lot again. Would you mind having another look ?
Description
Fixes the project settings form to auto-convert project names to URL-safe format (lowercase, hyphens, digits), matching the behavior of the project creation form. Previously, entering a name like "DEP-burundi-datafi" in settings would fail validation since the form submitted the raw input directly.
This PR also consolidates all duplicate
coerce_raw_name_to_safe_namefunctions intoProject.form_changeset/2andProject.form_with_users_changeset/2, using a virtual:raw_nameschema field. This follows the pattern already established byCollection.form_changeset.Along the way, a pre-existing bug was fixed in the dashboard creation modal where the save error path assigned to
:project_changesetinstead of:changeset, meaning validation errors from failed project creation were silently swallowed. Additionally, the@nameassign is now initialized inupdate/2(previously it only got set after the first validation event), and whitespace inside the name preview badge<span>tags was cleaned up to remove a trailing space.Closes #4437
Validation steps
Project settings
my-cool-projectCreate New Project
hello-worldCreate / Edit Sandboxes
my-sandboxCreate / Edit Collections
Create / Edit Projects (superuser interface)
AI Usage
Pre-submission checklist
/reviewwith Claude Code):owner,:admin,:editor,:viewer)