{"id":13879092,"url":"https://github.com/excid3/prefixed_ids","last_synced_at":"2025-04-12T01:54:06.302Z","repository":{"id":40012184,"uuid":"339584851","full_name":"excid3/prefixed_ids","owner":"excid3","description":"Friendly Prefixed IDs for your Ruby on Rails models","archived":false,"fork":false,"pushed_at":"2025-03-10T22:47:29.000Z","size":237,"stargazers_count":356,"open_issues_count":7,"forks_count":28,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-04-12T01:54:01.624Z","etag":null,"topics":["activerecord","rails","ruby","ruby-on-rails"],"latest_commit_sha":null,"homepage":"https://github.com/excid3/prefixed_ids","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/excid3.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"MIT-LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":["excid3"],"patreon":null,"open_collective":null,"ko_fi":null,"tidelift":null,"community_bridge":null,"liberapay":null,"issuehunt":null,"otechie":null,"custom":null}},"created_at":"2021-02-17T01:58:46.000Z","updated_at":"2025-04-10T14:31:18.000Z","dependencies_parsed_at":"2024-02-06T15:14:01.504Z","dependency_job_id":"78f24a03-1423-49a3-9f50-1b87f4b5851d","html_url":"https://github.com/excid3/prefixed_ids","commit_stats":{"total_commits":104,"total_committers":17,"mean_commits":6.117647058823529,"dds":"0.29807692307692313","last_synced_commit":"833f8b4fcff502842b8aead6cc79872fd2d24e5b"},"previous_names":[],"tags_count":16,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/excid3%2Fprefixed_ids","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/excid3%2Fprefixed_ids/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/excid3%2Fprefixed_ids/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/excid3%2Fprefixed_ids/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/excid3","download_url":"https://codeload.github.com/excid3/prefixed_ids/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248505873,"owners_count":21115354,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["activerecord","rails","ruby","ruby-on-rails"],"created_at":"2024-08-06T08:02:09.581Z","updated_at":"2025-04-12T01:54:06.278Z","avatar_url":"https://github.com/excid3.png","language":"Ruby","funding_links":["https://github.com/sponsors/excid3"],"categories":["Ruby","ActiveRecord"],"sub_categories":[],"readme":"\u003cp align=\"center\"\u003e\n  \u003ch1\u003ePrefixed IDs\u003c/h1\u003e\n\u003c/p\u003e\n\n### 🆔 Friendly Prefixed IDs for your Ruby on Rails models\n\n[![Build Status](https://github.com/excid3/prefixed_ids/workflows/Tests/badge.svg)](https://github.com/excid3/prefixed_ids/actions) [![Gem Version](https://badge.fury.io/rb/prefixed_ids.svg)](https://badge.fury.io/rb/prefixed_ids)\n\nGenerate prefixed IDs for your models with a friendly prefix. For example:\n\n```ruby\nuser_12345abcd\nacct_23lksjdg3\n```\n\nThis gem works by hashing the record's original `:id` attribute using [`Hashids`](https://hashids.org/ruby/), which transforms numbers like 347 into a string like yr8. It uses the table's name and an optional additional salt to hash values, returning a string like `tablename_hashedvalue`.\n\nInspired by [Stripe's prefixed IDs](https://stripe.com/docs/api) in their API.\n\n## 🚀 Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'prefixed_ids'\n```\n\n## 📝 Usage\n\nAdd `has_prefix_id :my_prefix` to your models to autogenerate prefixed IDs.\n\n```ruby\nclass User \u003c ApplicationRecord\n  has_prefix_id :user\nend\n```\n\n\u003e [!NOTE]\n\u003e Add `has_prefix_id` _before_ associations because it overrides `has_many` to include prefix ID helpers.\n\n### Prefix ID Param\n\nBy default, Prefixed IDs overrides `to_param` in the model to use prefix IDs.\n\nTo get the prefix ID for a record:\n\n```ruby\n@user.to_param\n#=\u003e \"user_12345abcd\"\n```\n\nIf `to_param` override is disabled:\n\n```ruby\n@user.prefix_id\n#=\u003e \"user_12345abcd\"\n```\n\n##### Query by Prefixed ID\n\nBy default, prefixed_ids overrides `find` and `to_param` to seamlessly URLs automatically.\n\n```ruby\nUser.first.to_param\n#=\u003e \"user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe\"\n\nUser.find(\"user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe\")\n#=\u003e #\u003cUser\u003e\n```\n\n\u003e [!NOTE]\n\u003e `find` still finds records by primary key. For example, `User.find(1)` still works.\n\nYou can also use `find_by_prefix_id` or `find_by_prefix_id!` when the `find` override is disabled:\n\n```ruby\nUser.find_by_prefix_id(\"user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe\") # Returns a User or nil\nUser.find_by_prefix_id!(\"user_5vJjbzXq9KrLEMm32iAnOP0xGDYk6dpe\") # Raises an exception if not found\n```\n\nTo disable `find` and `to_param` overrides, pass the following options:\n\n```ruby\nclass User \u003c ApplicationRecord\n  has_prefix_id :user, override_find: false, override_param: false\nend\n```\n\n\u003e [!NOTE]\n\u003e If you're aiming to masking primary key ID for security reasons, make sure to use `find_by_prefix_id` and [add a salt](#salt).\n\n##### Salt\n\nA salt is a secret value that makes it impossible to reverse engineer IDs. We recommend adding a salt to make your Prefix IDs unguessable.\n\n###### Global Salt\n\n```ruby\n# config/initializers/prefixed_ids.rb\nPrefixedIds.salt = \"salt\"\n```\n\n###### Per Model Salt\n\n```ruby\nclass User\n  has_prefix_id :user, salt: \"usersalt\"\nend\n```\n\n### Find Any Model By Prefix ID\n\nImagine you have a prefixed ID but you don't know which model it belongs to:\n\n```ruby\nPrefixedIds.find(\"user_5vJjbzXq9KrLEMm3\")\n#=\u003e #\u003cUser\u003e\n\nPrefixedIds.find(\"acct_2iAnOP0xGDYk6dpe\")\n#=\u003e #\u003cAccount\u003e\n```\n\nThis works similarly to GlobalIDs.\n\n### Customizing Prefix IDs\n\nYou can customize the prefix, length, and attribute name for PrefixedIds.\n\n```ruby\nclass Account \u003c ApplicationRecord\n  has_prefix_id :acct, minimum_length: 32, override_find: false, override_param: false, salt: \"\", fallback: false\nend\n```\n\nBy default, `find` will accept both Prefix IDs and regular IDs. Setting `fallback: false` will disable finding by regular IDs and will only allow Prefix IDs.\n\n## Development\n\nAfter checking out the repo, run `bundle install` to install dependencies. Then, run `rake test` to run the tests. You can also run `bin/rails console` for an interactive prompt that will allow you to experiment.\n\nTo install this gem onto your local machine, run `bundle exec rake install`. To release a new version, update the version number in `version.rb`, and then run `bundle exec rake release`, which will create a git tag for the version, push git commits and the created tag, and push the `.gem` file to [rubygems.org](https://rubygems.org).\n\n## 🙏 Contributing\n\nBug reports and pull requests are welcome on GitHub at https://github.com/excid3/prefixed_ids. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to the [code of conduct](https://github.com/[USERNAME]/prefixed_ids/blob/master/CODE_OF_CONDUCT.md).\n\n## 📝 License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the PrefixedIds project's codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/prefixed_ids/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexcid3%2Fprefixed_ids","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fexcid3%2Fprefixed_ids","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fexcid3%2Fprefixed_ids/lists"}