{"id":18319740,"url":"https://github.com/redding/much-slug","last_synced_at":"2025-04-09T14:22:14.844Z","repository":{"id":54555116,"uuid":"151874061","full_name":"redding/much-slug","owner":"redding","description":"Friendly, human-readable identifiers for database records.","archived":false,"fork":false,"pushed_at":"2022-04-03T05:21:32.000Z","size":54,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":2,"default_branch":"main","last_synced_at":"2025-03-17T09:50:14.708Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"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/redding.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2018-10-06T19:22:37.000Z","updated_at":"2021-02-10T21:29:39.000Z","dependencies_parsed_at":"2022-08-13T19:31:14.431Z","dependency_job_id":null,"html_url":"https://github.com/redding/much-slug","commit_stats":null,"previous_names":[],"tags_count":3,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fmuch-slug","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fmuch-slug/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fmuch-slug/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/redding%2Fmuch-slug/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/redding","download_url":"https://codeload.github.com/redding/much-slug/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248054200,"owners_count":21039952,"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":[],"created_at":"2024-11-05T18:14:08.212Z","updated_at":"2025-04-09T14:22:14.801Z","avatar_url":"https://github.com/redding.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# MuchSlug\n\nFriendly, human-readable identifiers for database records.\n\n## Usage\n\nMuchSlug creates derived slug values on database records. Typically this means deriving the slug value from record attributes and syncing/caching that value in a dedicated field on the record.\n\n### ActiveRecord\n\nGiven a `:slug` field on a record:\n\n```ruby\nclass AddSlugToProjects \u003c ActiveRecord::Migration[6.1]\n  def change\n    add_column(:projects, :slug, :string, index: { unique: true })\n  end\nend\n```\n\nMix-in `MuchSlug::ActiveRecord` and configure:\n\n```ruby\nclass ProjectRecord \u003c ApplicationRecord\n  self.table_name = \"projects\"\n\n  include MuchSlug::ActiveRecord\n  has_slug(\n    source: -\u003e { \"#{id}-#{name}\" },\n  )\n\n  # ...\nend\n```\n\nThe record will save slug values as it is saved:\n\n```ruby\nproject = ProjectRecord.last\nproject.id   # =\u003e 123\nproject.name # =\u003e \"Sprockets 2.0\"\nproject.slug # =\u003e nil\n\n# slug values are updated late-bound after record saves\nproject.save\nproject.slug # =\u003e \"123-Sprockets-2-0\"\n\nproject.name = \"Widgets For Me\"\nproject.slug # =\u003e \"123-Sprockets-2-0\"\nproject.save\nproject.slug # =\u003e \"123-Widgets-For-Me\"\n\n# new Projects also have their slugs assigned once they are saved\nproject = Project.new(name: \"Do The Things\")\nproject.slug # =\u003e nil\nproject.save!\nproject.slug # =\u003e \"124-Do-The-Things\"\n```\n\n### Notes\n\n#### Attribute\n\nBy default, the record attribute for a slug is `\"slug\"`. You can override this when configuring slugs:\n\n```ruby\nclass ProjectRecord \u003c ApplicationRecord\n  self.table_name = \"projects\"\n\n  include MuchSlug::ActiveRecord\n  has_slug(\n    source: -\u003e { \"#{id}-#{name}\" },\n  )\n  has_slug(\n    attribute: :full_slug\n    source:    -\u003e { \"#{id}-#{full_name}\" },\n  )\n\n  # ...\nend\n```\n\n#### Preprocessor\n\nBy default, MuchSlug doesn't pre-process the slug value source before generating the slug value. You can specify a custom pre-processor by passing any Proc-like object:\n\n```ruby\nclass ProjectRecord \u003c ApplicationRecord\n  self.table_name = \"projects\"\n\n  include MuchSlug::ActiveRecord\n  has_slug(\n    source:       -\u003e { \"#{id}-#{name}\" },\n    preprocessor: :downcase\n  )\n  has_slug(\n    attribute:    :full_slug\n    source:       -\u003e { \"#{id}-#{full_name}\" },\n    preprocessor: -\u003e { |source_value| source_value[0..30] }\n  )\n\n  # ...\nend\n```\n\n#### Separator\n\nMuchSlug replaces any non-word characters with a separator. This helps make slugs URL-friendly. By default, MuchSlug uses `\"-\"` for the separator. You can specify a custom separator value when configuring slugs:\n\n```ruby\nclass ProjectRecord \u003c ApplicationRecord\n  self.table_name = \"projects\"\n\n  include MuchSlug::ActiveRecord\n  has_slug(\n    source:    -\u003e { \"#{id}.#{name}\" },\n    separator: \".\"\n  )\n\n  # ...\nend\n\nproject = ProjectRecord.last\nproject.id   # =\u003e 123\nproject.name # =\u003e \"Sprockets 2.0\"\nproject.save\nproject.slug # =\u003e \"123.Sprockets.2.0\"\n```\n\n#### Allowing Underscores\n\nBy default, MuchSlug doesn't allow underscores in source values and treats them like non-word characters. This means it replaces underscores with the separator. You can override this to allow underscores when configuring slugs:\n\n```ruby\nclass ProjectRecord \u003c ApplicationRecord\n  self.table_name = \"projects\"\n\n  include MuchSlug::ActiveRecord\n  has_slug(\n    source: -\u003e { \"#{id}-#{name}\" }\n  )\n  has_slug(\n    attribute:         :full_slug\n    source:            -\u003e { \"#{id}-#{full_name}\" },\n    allow_underscores: true\n\n  # ...\nend\n\nproject = ProjectRecord.last\nproject.id        # =\u003e 123\nproject.name      # =\u003e \"SP_2.0\"\nproject.full_name # =\u003e \"Sprockets_2.0\"\nproject.save\nproject.slug      # =\u003e \"123-SP-2-0\"\nproject.full_slug # =\u003e \"123-Sprockets_2-0\"\n```\n\n#### Manual Slug Updates\n\nSlugs are updated anytime a record is saved with changes that affect the slug. If you want an explicit, intention-revealing way to update the slugs manually, use `MuchSlug.update_slugs`:\n\n```ruby\nproject = ProjectRecord.last\nproject.id   # =\u003e 123\nproject.name # =\u003e \"Sprockets 2.0\"\n\nMuchSlug.update_slugs(project)\nproject.slug # =\u003e \"123-Sprockets-2-0\"\n```\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n    gem \"much-slug\"\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install much-slug\n\n## Contributing\n\n1. Fork it\n2. Create your feature branch (`git checkout -b my-new-feature`)\n3. Commit your changes (`git commit -am 'Added some feature'`)\n4. Push to the branch (`git push origin my-new-feature`)\n5. Create new Pull Request\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredding%2Fmuch-slug","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fredding%2Fmuch-slug","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fredding%2Fmuch-slug/lists"}