{"id":15595326,"url":"https://github.com/michaelherold/activerecord-ksuid","last_synced_at":"2025-04-05T06:24:59.106Z","repository":{"id":46577285,"uuid":"312178385","full_name":"michaelherold/activerecord-ksuid","owner":"michaelherold","description":"K-Sortable Universal IDentifiers for ActiveRecord","archived":false,"fork":false,"pushed_at":"2021-10-08T01:56:05.000Z","size":62,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-04-05T01:05:24.511Z","etag":null,"topics":["activerecord","hacktoberfest","ruby"],"latest_commit_sha":null,"homepage":"","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/michaelherold.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":"CONTRIBUTING.md","funding":null,"license":"LICENSE.md","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-11-12T05:38:26.000Z","updated_at":"2024-09-26T10:28:36.000Z","dependencies_parsed_at":"2022-09-10T02:02:17.437Z","dependency_job_id":null,"html_url":"https://github.com/michaelherold/activerecord-ksuid","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaelherold%2Factiverecord-ksuid","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaelherold%2Factiverecord-ksuid/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaelherold%2Factiverecord-ksuid/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/michaelherold%2Factiverecord-ksuid/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/michaelherold","download_url":"https://codeload.github.com/michaelherold/activerecord-ksuid/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":247296662,"owners_count":20915670,"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","hacktoberfest","ruby"],"created_at":"2024-10-03T00:54:17.203Z","updated_at":"2025-04-05T06:24:59.076Z","avatar_url":"https://github.com/michaelherold.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# KSUID for ActiveRecord\n\n[![Build Status](https://github.com/michaelherold/activerecord-ksuid/workflows/Continuous%20integration/badge.svg)][actions]\n[![Test Coverage](https://api.codeclimate.com/v1/badges/672e331f6006d1a00ec8/test_coverage)][test-coverage]\n[![Maintainability](https://api.codeclimate.com/v1/badges/672e331f6006d1a00ec8/maintainability)][maintainability]\n[![Inline docs](http://inch-ci.org/github/michaelherold/activerecord-ksuid.svg?branch=main)][inch]\n\n[inch]: http://inch-ci.org/github/michaelherold/activerecord-ksuid\n[maintainability]: https://codeclimate.com/github/michaelherold/activerecord-ksuid/maintainability\n[test-coverage]: https://codeclimate.com/github/michaelherold/activerecord-ksuid/test_coverage\n[actions]: https://github.com/michaelherold/activerecord-ksuid/actions\n\nUsing K-Sortable Unique IDentifiers in ActiveRecord and wish there was an easier way to do it? Look no further! `activerecord-ksuid` adds support for the data type to your application with zero friction.\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'activerecord-ksuid'\n```\n\nAnd then execute:\n\n    $ bundle install\n\nOr install it yourself as:\n\n    $ gem install activerecord-ksuid\n\n## Usage\n\nWhether you are using ActiveRecord inside an existing project or in a new project, usage is simple. Additionally, you can use it with or without Rails.\n\n### Adding to an existing model\n\nWithin a Rails project, it is very easy to get started using KSUIDs within your models. You can use the `ksuid` column type in a Rails migration to add a column to an existing model:\n\n    rails generate migration add_ksuid_to_events ksuid:ksuid\n\nThis will generate a migration like the following:\n\n```ruby\nclass AddKsuidToEvents \u003c ActiveRecord::Migration[5.2]\n  def change\n    add_column :events, :unique_id, :ksuid\n  end\nend\n```\n\nThen, to add proper handling to the field, you will want to mix a module into the model:\n\n```ruby\nclass Event \u003c ApplicationRecord\n  include ActiveRecord::KSUID[:unique_id]\nend\n```\n\n### Creating a new model\n\nTo create a new model with a `ksuid` field that is stored as a KSUID, use the `ksuid` column type. Using the Rails generators, this looks like:\n\n    rails generate model Event my_field_name:ksuid\n\nIf you would like to add a KSUID to an existing model, you can do so with the following:\n\n```ruby\nclass AddKsuidToEvents \u003c ActiveRecord::Migration[5.2]\n  change_table :events do |table|\n    table.ksuid :my_field_name\n  end\nend\n```\n\nOnce you have generated the table that you will use for your model, you will need to include a module into the model class, as follows:\n\n```ruby\nclass Event \u003c ApplicationRecord\n  include ActiveRecord::KSUID[:my_field_name]\nend\n```\n\n#### With a KSUID primary key\n\nYou can also use a KSUID as the primary key on a table, much like you can use a UUID in vanilla Rails. To do so requires a little more finagling than you can manage through the generators. When hand-writing the migration, it will look like this:\n\n```ruby\nclass CreateEvents \u003c ActiveRecord::Migration[5.2]\n  create_table :events, id: false do |table|\n    table.ksuid :id, primary_key: true\n  end\nend\n```\n\nYou will need to mix in the module into your model as well:\n\n```ruby\nclass Event \u003c ApplicationRecord\n  include ActiveRecord::KSUID[:id]\nend\n```\n\n### Outside of Rails\n\nOutside of Rails, you cannot rely on the Railtie to load the appropriate files for you automatically. Toward the start of your application's boot process, you will want to require the following:\n\n```ruby\nrequire 'active_record/ksuid'\n\n# If you will be using the ksuid column type in a migration\nrequire 'active_record/ksuid/table_definition'\n```\n\nOnce you have required the file(s) that you need, everything else will work as it does above.\n\n### Binary vs. String KSUIDs\n\nThese examples all store your identifier as a string-based KSUID. If you would like to use binary KSUIDs instead, use the `ksuid_binary` column type. Unless you need to be super-efficient with your database, we recommend using string-based KSUIDs because it makes looking at the data while in the database a little easier to understand.\n\nWhen you include the KSUID module into your model, you will want to pass the `:binary` option as well:\n\n```ruby\nclass Event \u003c ApplicationRecord\n  include ActiveRecord::KSUID[:my_field_name, binary: true]\nend\n```\n\n### Use the KSUID as your `created_at` timestamp\n\nSince KSUIDs include a timestamp as well, you can infer the `#created_at` timestamp from the KSUID when it is being used as your primary key. The module builder enables that option automatically with the `:created_at` option, like so:\n\n```ruby\nclass Event \u003c ApplicationRecord\n  include ActiveRecord::KSUID[:my_field_name, created_at: true]\nend\n```\n\nThis allows you to be efficient in your database design if that is a constraint you need to satisfy.\n\n## Contributing\n\nSo you’re interested in contributing to KSUID for ActiveRecord? Check out our [contributing guidelines](CONTRIBUTING.md) for more information on how to do that.\n\n## Supported Ruby Versions\n\nThis library aims to support and is [tested against][actions] the following Ruby versions:\n\n* Ruby 2.5\n* Ruby 2.6\n* Ruby 2.7\n* JRuby 9.2\n\nIf something doesn't work on one of these versions, it's a bug.\n\nThis library may inadvertently work (or seem to work) on other Ruby versions, however support will only be provided for the versions listed above.\n\nIf you would like this library to support another Ruby version or implementation, you may volunteer to be a maintainer. Being a maintainer entails making sure all tests run and pass on that implementation. When something breaks on your implementation, you will be responsible for providing patches in a timely fashion. If critical issues for a particular implementation exist at the time of a major release, support for that Ruby version may be dropped.\n\n## Versioning\n\nThis library aims to adhere to [Semantic Versioning 2.0.0][semver]. Violations of this scheme should be reported as bugs. Specifically, if a minor or patch version is released that breaks backward compatibility, that version should be immediately yanked and/or a new version should be immediately released that restores compatibility. Breaking changes to the public API will only be introduced with new major versions. As a result of this policy, you can (and should) specify a dependency on this gem using the [Pessimistic Version Constraint][pessimistic] with two digits of precision. For example:\n\n    spec.add_dependency \"activerecord-ksuid\", \"~\u003e 0.1\"\n\n[pessimistic]: http://guides.rubygems.org/patterns/#pessimistic-version-constraint\n[semver]: http://semver.org/spec/v2.0.0.html\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichaelherold%2Factiverecord-ksuid","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmichaelherold%2Factiverecord-ksuid","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmichaelherold%2Factiverecord-ksuid/lists"}