{"id":13879747,"url":"https://github.com/freeletics/array_enum","last_synced_at":"2025-10-08T22:05:22.684Z","repository":{"id":45144930,"uuid":"163827812","full_name":"freeletics/array_enum","owner":"freeletics","description":"String to integer mapping for PostgreSQL array columns","archived":false,"fork":false,"pushed_at":"2024-09-02T13:26:58.000Z","size":59,"stargazers_count":52,"open_issues_count":1,"forks_count":8,"subscribers_count":10,"default_branch":"master","last_synced_at":"2025-07-26T20:12:49.133Z","etag":null,"topics":["activerecord","enum","postgresql","rails"],"latest_commit_sha":null,"homepage":"https://github.com/freeletics/array_enum","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/freeletics.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":null,"license":"LICENSE.txt","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}},"created_at":"2019-01-02T10:37:43.000Z","updated_at":"2025-06-05T15:51:53.000Z","dependencies_parsed_at":"2024-06-21T05:43:45.625Z","dependency_job_id":"a9a28448-ef4c-4683-b2ef-6ee887d2026f","html_url":"https://github.com/freeletics/array_enum","commit_stats":{"total_commits":37,"total_committers":9,"mean_commits":4.111111111111111,"dds":0.6486486486486487,"last_synced_commit":"490e1d0d5360cabb3366249699a9cabf1b5e7440"},"previous_names":[],"tags_count":9,"template":false,"template_full_name":null,"purl":"pkg:github/freeletics/array_enum","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/freeletics%2Farray_enum","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/freeletics%2Farray_enum/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/freeletics%2Farray_enum/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/freeletics%2Farray_enum/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/freeletics","download_url":"https://codeload.github.com/freeletics/array_enum/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/freeletics%2Farray_enum/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":270817078,"owners_count":24650925,"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","status":"online","status_checked_at":"2025-08-17T02:00:09.016Z","response_time":129,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"can_crawl_api":true,"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","enum","postgresql","rails"],"created_at":"2024-08-06T08:02:31.310Z","updated_at":"2025-10-08T22:05:17.653Z","avatar_url":"https://github.com/freeletics.png","language":"Ruby","readme":"[![Gem Version](https://badge.fury.io/rb/array_enum.svg)](https://badge.fury.io/rb/array_enum)\n[![BUILD](https://github.com/freeletics/array_enum/actions/workflows/ci-on-merge.yml/badge.svg)](https://github.com/freeletics/array_enum/actions/workflows/ci-on-merge.yml)\n\n\n# ArrayEnum\n\nExtension for `ActiveRecord` that adds support for `PostgreSQL` array columns, mapping string values to integers.\n\n## Installation\n\n`gem install array_enum` or use `Gemfile` with bundler\n\n## Usage\n\n### ActiveRecord extension\n\nDatabase will store integers that after reading will map to string values.\n\n```ruby\nActiveRecord::Schema.define do\n  create_table :users, force: true do |t|\n    t.integer :favourite_colors, array: true, null: false, default: []\n  end\nend\n\nclass User \u003c ActiveRecord::Base\n  extend ArrayEnum\n\n  array_enum favourite_colors: {\"red\" =\u003e 1, \"blue\" =\u003e 2, \"green\" =\u003e 3}\nend\n\nuser = User.create!(favourite_colors: [\"red\", \"green\"])\nuser.favourite_colors # =\u003e [\"red\", \"green\"]\nUser.favourite_colors # =\u003e {\"red\" =\u003e 1, \"blue\" =\u003e 2, \"green\" =\u003e 3}\n```\n\nSeveral scopes are made available on your model to find records based on a value or an array of values:\n\n```ruby\nuser1 = User.create!(favourite_colors: [\"red\", \"green\"])\nuser2 = User.create!(favourite_colors: [\"red\"])\n\n# Find a record that has _all_ the provided values in the array enum attribute\nUser.with_favourite_colors(\"red\") # =\u003e [user1, user2]\nUser.with_favourite_colors(%w[red green]) # =\u003e [user1]\nUser.with_favourite_colors(%w[red blue]) # =\u003e []\nUser.with_favourite_colors(%w[green blue]) # =\u003e []\n\n# Find a record that has the provided values, and _only those values_, in the array enum attribute\nUser.only_with_favourite_colors(\"red\") # =\u003e [user2]\nUser.only_with_favourite_colors(%w[red green]) # =\u003e [user1]\nUser.only_with_favourite_colors(%w[red blue]) # =\u003e []\nUser.only_with_favourite_colors(%w[green blue]) # =\u003e []\n\n# Find a record that has _at least one_ of the provided values in the array enum attribute\nUser.with_any_of_favourite_colors(\"red\") # =\u003e [user1, user2]\nUser.with_any_of_favourite_colors(%w[red green]) # =\u003e [user1, user2]\nUser.with_any_of_favourite_colors(%w[red blue]) # =\u003e [user1, user2]\nUser.with_any_of_favourite_colors(%w[green blue]) # =\u003e [user1]\n```\n\nAttempting to find a record with a value that is not in the enum will fail:\n\n```ruby\nUser.with_favourite_colors(\"yellow\") # =\u003e ArgumentError[\"yellow is not a valid value for favourite_colors\"]\n```\n\n### Subset Validator\n\nAdditionally `subset` validator is provided that can help to ensure correct values are passed during validation.\n\n```ruby\nclass CreateUser\n  include ActiveModel::Model\n\n  attr_accessor :favourite_colors\n\n  validates :favourite_colors, subset: [\"green\", \"blue\"]\n  # or:\n  # validates :favourite_colors, subset: { in: -\u003e(record) { Color.pluck(:name) } }\nend\n\nCreateUser.new(favourite_colors: [\"black\"]).valid? # =\u003e false\n```\n\n## Development\n\nAfter 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.\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 tags, 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/freeletics/array_enum.\n\n## License\n\nThe gem is available as open source under the terms of the [MIT License](https://opensource.org/licenses/MIT).\n","funding_links":[],"categories":["Ruby"],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffreeletics%2Farray_enum","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Ffreeletics%2Farray_enum","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Ffreeletics%2Farray_enum/lists"}