Skip to content
This repository was archived by the owner on May 26, 2025. It is now read-only.

Rails-Designer/stealth_dom_id

Repository files navigation

stealth_dom_id

stealth_dom_id extends Rails' dom_id helper by allowing you to generate DOM IDs using any attribute instead of the default database primary keys. This helps keep your internal database IDs private.

Installation

Add the gem to your application's Gemfile by running:

bundle add stealth_dom_id

Usage

Instead of exposing database IDs in your HTML elements with the standard dom_id helper:

<%= dom_id(@user) %>
# => "user_1"

You can use any available attribute of your choice:

<%= dom_id(@user, attribute: :public_id) %>
# => "user_a1b2c3"

The attribute parameter is optional and defaults to the model's primary key. Just like Rails' built-in dom_id, you can also include a prefix:

<%= dom_id(@user, :admin, attribute: :public_id) %>
# => "admin_user_a1b2c3"

Configuration

You can set a default attribute to be used across your application. Add this to an initializer (e.g., config/initializers/stealth_dom_id.rb):

StealthDomId.configure do |config|
  config.default_attribute = :public_id
end

Now public_id will be used whenever no specific attribute is provided.

Development

After checking out the repo, run bin/setup to install dependencies. Then, run rake test to run the tests. You can also run bin/console for an interactive prompt that will allow you to experiment.

To install this gem onto your local machine, run bundle exec rake install.

License

The gem is available as open source under the terms of the MIT License.

About

Extends Rails' `dom_id` helper to support custom attribute-based identifiers

Topics

Resources

License

Stars

Watchers

Forks