Skip to content

Add dom_target helper to create dom_id-like strings from an unlimited number of objects - #55204

Merged
byroot merged 1 commit into
rails:mainfrom
bensheldon:dom_target
Jul 1, 2025
Merged

Add dom_target helper to create dom_id-like strings from an unlimited number of objects#55204
byroot merged 1 commit into
rails:mainfrom
bensheldon:dom_target

Conversation

@bensheldon

@bensheldon bensheldon commented Jun 17, 2025

Copy link
Copy Markdown
Contributor

Motivation / Background

This Pull Request has been created because it's increasingly necessary to create complex DOM identifiers and the existing dom_id is limited.

This PR creates a new method ActionView::RecordIdentifier#dom_target that can create a unique identifier from an unlimited number of objects, which differentiates it from dom_id which only takes a single object and an optional string prefix.

dom_target(Post.find(45))                  # => "post_45"
dom_target(Post.find(45), :edit)           # => "post_45_edit"
dom_target(Post.find(45), :edit, :special) # => "post_45_edit_special"
dom_target(Post.find(45), Comment.find(1)) # => "post_45_comment_1"

I chose the name of target because I found myself wanting this for generating turbo-rails Turbo Broadcast targets. dom_target is inspired by the structure of turbo_stream_from(*streamables), which concatenates an unlimited number of objects into a single identifier.

Adding an new helper method is proposed as an alternative to adding new behavior to existing dom_id or dom_class helper methods, which had questions/objections. Related:

Additional information

Checklist

Before submitting the PR make sure the following are checked:

  • This Pull Request is related to one change. Unrelated changes should be opened in separate PRs.
  • Commit message has a detailed description of what changed and why. If this PR fixes a related issue include it in the commit message. Ex: [Fix #issue-number]
  • Tests are added or updated if you fix a bug or add a feature.
  • CHANGELOG files are updated for the changed libraries if there is a behavior change or additional feature. Minor bug fixes and documentation changes should not be included.

@rails-bot rails-bot Bot added the actionview label Jun 17, 2025
@bensheldon bensheldon changed the title Add dom_target helper to create dom_id-like strings from an unlim… Add dom_target helper to create dom_id-like strings from an unlimited number of objects Jun 17, 2025
@bensheldon

bensheldon commented Jun 17, 2025

Copy link
Copy Markdown
Contributor Author

I can fix up the changelog.

I want to change one behavior I noticed that I want to fix before it's baked:

dom_target(User) #=> "user" 
dom_target(User.new) #=> "new_user"

Edit: ✅

@byroot

byroot commented Jul 1, 2025

Copy link
Copy Markdown
Member

I did a small change to use a case/when, other than that, seems sensible to me.

@byroot
byroot merged commit 985e75f into rails:main Jul 1, 2025
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants