Skip to content

Make saved ID read-only and add tests#298

Merged
stefan-burke merged 2 commits intomainfrom
terragon/fix-editable-unit-id-aqm43g
Oct 13, 2025
Merged

Make saved ID read-only and add tests#298
stefan-burke merged 2 commits intomainfrom
terragon/fix-editable-unit-id-aqm43g

Conversation

@stefan-burke
Copy link
Copy Markdown
Member

Summary

  • When UNIT_BADGES is enabled, the saved unit ID becomes read-only after creation.
  • Adds UI, controller, and test coverage to ensure the badge ID cannot be changed via UI or requests.

Changes

UI

  • app/views/units/_form.html.erb
    • If UNIT_BADGES is enabled:
      • Existing (persisted) units display the ID in a read-only format on the form.
      • New units display an editable ID field (creation flow).

Backend

  • app/controllers/units_controller.rb
    • Permit :id for create actions only when UNIT_BADGES is enabled. Creation actions include create and create_from_inspection.
    • This ensures the ID can be supplied during creation, but not updated later.

Tests

  • spec/features/units/unit_badges_spec.rb

    • Added scenarios for ID immutability on edit:
      • ID field is not present on the edit form.
      • ID remains unchanged when editing other fields.
      • Badge ID is shown on the edit page in display format.
  • spec/requests/units/units_spec.rb

    • Added request specs for ID immutability:
      • Patching a unit with a new ID does not update the original ID.
      • ID parameter is ignored in update requests in favor of existing ID.

Test plan

  • Run the unit badge tests:
    • bundle exec rspec spec/features/units/unit_badges_spec.rb
    • bundle exec rspec spec/requests/units/units_spec.rb
  • Verify:
    • On edit, the ID is displayed read-only when UNIT_BADGES is enabled.
    • Creating a new unit allows setting an ID when UNIT_BADGES is enabled.
    • Updating a unit cannot change its ID, while other attributes update normally.

Notes

  • The new behavior is gated by the UNIT_BADGES environment variable. Tests set and unset this flag to validate both scenarios. Ensure CI runs with UNIT_BADGES enabled where coverage is required.

🌿 Generated by Terry


ℹ️ Tag @terragon-labs to ask questions and address PR feedback

📎 Task: https://www.terragonlabs.com/task/fb797c38-78b3-4192-9cbf-c8e6f809b5fe

stefan-burke and others added 2 commits October 13, 2025 00:12
…y ID

- Permit :id param only on unit creation, not on update, when UNIT_BADGES enabled
- Display ID as read-only on unit edit form when UNIT_BADGES is enabled
- Add feature and request specs to ensure ID is immutable after creation
- Prevent ID changes via form or direct requests on updates
- Improve user experience by showing badge ID on edit without allowing modification

Co-authored-by: terragon-labs[bot] <terragon-labs[bot]@users.noreply.github.com>
Replaced manual HTML markup for displaying the read-only unit ID with a render call to 'chobble_forms/display_field'. This improves code readability and consistency in the units form view.

Co-authored-by: terragon-labs[bot] <terragon-labs[bot]@users.noreply.github.com>
@stefan-burke stefan-burke merged commit f0789cb into main Oct 13, 2025
16 checks passed
@stefan-burke stefan-burke deleted the terragon/fix-editable-unit-id-aqm43g branch October 13, 2025 00:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant