0.0
Repository is archived
There's a lot of open issues
stealth_dom_id extends Rails' `dom_id` helper to generate DOM IDs using an alternative attribute instead of database primary keys. This helps prevent exposing internal database IDs.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
 Dependencies

Runtime

>= 3.0, < 8.1
>= 3.0, < 8.1
 Project Readme

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.