{"id":19008371,"url":"https://github.com/mechanicles/set_as_primary","last_synced_at":"2025-06-26T10:04:15.657Z","repository":{"id":56895159,"uuid":"220389125","full_name":"mechanicles/set_as_primary","owner":"mechanicles","description":"The simplest way to handle the primary or default boolean flag to your Rails model records","archived":false,"fork":false,"pushed_at":"2023-08-10T14:32:07.000Z","size":92,"stargazers_count":15,"open_issues_count":2,"forks_count":5,"subscribers_count":3,"default_branch":"master","last_synced_at":"2025-06-21T00:38:40.752Z","etag":null,"topics":["activerecord","boolean","boolean-flag","column","default","default-flag","flag","has-many","primary","rails","ruby","rubygem","set-as-default","set-as-primary"],"latest_commit_sha":null,"homepage":"http://blog.mechanicles.com/2019/12/26/primary-or-default-flag-to-the-rails-models.html","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/mechanicles.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-11-08T04:51:25.000Z","updated_at":"2023-08-10T12:38:38.000Z","dependencies_parsed_at":"2024-11-08T18:46:55.649Z","dependency_job_id":"1282d32e-b99a-4867-bd61-47f9e2b1f5b6","html_url":"https://github.com/mechanicles/set_as_primary","commit_stats":null,"previous_names":[],"tags_count":5,"template":false,"template_full_name":null,"purl":"pkg:github/mechanicles/set_as_primary","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mechanicles%2Fset_as_primary","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mechanicles%2Fset_as_primary/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mechanicles%2Fset_as_primary/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mechanicles%2Fset_as_primary/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/mechanicles","download_url":"https://codeload.github.com/mechanicles/set_as_primary/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/mechanicles%2Fset_as_primary/sbom","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":262044446,"owners_count":23249749,"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","boolean","boolean-flag","column","default","default-flag","flag","has-many","primary","rails","ruby","rubygem","set-as-default","set-as-primary"],"created_at":"2024-11-08T18:42:20.948Z","updated_at":"2025-06-26T10:04:15.641Z","avatar_url":"https://github.com/mechanicles.png","language":"Ruby","readme":"# SetAsPrimary\n\nThe simplest way to handle primary or default boolean flag to your Rails\nmodel records.\n\nFeatures:\n\n* Supports single model (without association), model with (`belongs_to`) association, and even polymorphic associations\n* Force primary\n* Supports PostgreSQL, MySQL, and SQLite\n* Supports PostgreSQL's unique partial index (constraint)\n\nYou can find source code of demo Rails application [here](https://github.com/mechanicles/set_as_primary_rails_app).\n\n[![Build Status](https://github.com/mechanicles/set_as_primary/actions/workflows/build.yml/badge.svg?branch=master)](https://github.com/mechanicles/set_as_primary/actions)\n[![Maintainability](https://api.codeclimate.com/v1/badges/9aa764138b14b87c8fe1/maintainability)](https://codeclimate.com/github/mechanicles/set_as_primary/maintainability)\n[![Test Coverage](https://api.codeclimate.com/v1/badges/9aa764138b14b87c8fe1/test_coverage)](https://codeclimate.com/github/mechanicles/set_as_primary/test_coverage)\n\n## A lovely quote from a user :heart:\n\n\u003e Thanks for this gem! \u003cbr\u003e\n\u003e This gem solves a small, relatively obscure need but it does so really, really well. Thank you!\n\n***- [Veeral Patel](https://github.com/mechanicles/set_as_primary/issues/8)*** \n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'set_as_primary'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install set_as_primary\n\n## Usage\n\nIn your Rails application, you might have models like EmailAddress, PhoneNumber,\nAddress, etc., which belong to the User/Person model or polymorphic model. There,\nyou might need to set a primary email address, primary phone number, or default\naddress for a user, and this gem helps you to do that.\n\nIt also supports a single model with no association context. \n\nExamples:\n\n```ruby\nclass User \u003c ApplicationRecord\n  has_many :email_addresses\n  has_many :addresses, as: :owner\nend\n\nclass EmailAddress \u003c ApplicationRecord\n  include SetAsPrimary\n  belongs_to :user\n\n  set_as_primary :primary, owner_key: :user\n end\n\nclass Address \u003c ApplicationRecord\n  include SetAsPrimary\n  belongs_to :owner, polymorphic: true\n\n  set_as_primary :primary, owner_key: :owner\nend\n\n# Single model with no owner/association context.\nclass Post \u003c ApplicationRecord\n  include SetAsPrimary\n  \n  set_as_primary :primary\nend\n``` \n\nYou need to include `SetAsPrimary` module in your model where you want to handle the primary flag.\nThen to `set_as_primary` class helper method, pass your primary flag attribute. You might need to pass\n association key `owner_key` if you want to consider owner (association) context.\n\n**Note:**  Default primary flag attribute is `primary`, and you can use another one too like `default` but\nmake sure that flag should be present in the table and should be a boolean data type column.\n\n#### Migration\n\nIf your table does not have the primary flag column, then you can add it by running \nfollowing command in your rails project:\n\n```ssh\nrails generate set_as_primary your_table_name flag_name\n```\n\nExample:\n\nIf you want to add a `primary` column to your `posts` table, then you can run command like this:\n\n```shell\nrails generate set_as_primary posts primary\n```\n\nThen migration gets created like this:\n\n```ruby\nclass AddPrimaryColumnToPosts \u003c ActiveRecord::Migration[6.0]\n  def change\n    add_column :posts, :primary, :boolean, default: false, null: false\n    # NOTE: Please uncomment following line if you want only one 'true' (constraint) in the table.\n    # add_index :posts, :primary, unique: true, where: \"(posts.primary IS TRUE)\"\n  end\nend\n```\n\nIf you want to create a primary column to `email_addresses` table, then you can run command like this:\n\n```shell\nrails generate set_as_primary email_addresses primary user\n```\n\nThen it creates migration like this:\n\n```ruby\nclass AddPrimaryColumnToEmailAddresses \u003c ActiveRecord::Migration[6.0]\n  def change\n    add_column :email_addresses, :primary, :boolean, default: false, null: false\n    # NOTE: Please uncomment following line if you want only one 'true' (constraint) in the table.\n    # add_index :email_addresses, %i[user_id primary], unique: true, where: \"(email_addresses.primary IS TRUE)\"\n  end\nend\n```\nYou might have seen extra commented lines there. These lines are there for handling the unique constraint. Currently, these lines get created only for `PostgreSQL` adapter as it supports partial index.\n\nPlease note that here we have passed an extra option `user` in the command that is nothing but the owner/association name. This extra option helps to handle the unique partial index.\n\n**Note:** Partial indexes are only supported for PostgreSQL and SQLite 3.8.0+. But I also found that SQLite gives an error so currently this gem only supports PostgreSQL's unique partial index constraint.\n\n**Even if we don't have constraint (only one 'true' constraint in the table), this gem takes care of it so don't worry about the constraint.**\n\nOnce migration file gets created, don't forget to run `rails db:migrate` to create an actual column in the table.\n\n#### force_primary\n\n```ruby\nclass Address \u003c ApplicationRecord\n  include SetAsPrimary\n  belongs_to :user\n\n  set_as_primary :default, owner_key: :user, force_primary: false\n end\n```\n\nBy default `force_primary` option is set to `true`. If this option is `true`,\nthen it automatically sets record as primary when there is only one record in\nthe table. If you don't want this flow, then set it as `false`.\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run\n`rake test` to run the tests. You can also run `bin/console` for an interactive\nprompt that will allow you to experiment.\n\n## Contributing\n\nBug reports and pull requests are welcome on GitHub at \nhttps://github.com/mechanicles/set_as_primary. This project is intended to be a\nsafe, welcoming space for collaboration, and contributors are expected to adhere\nto the [Contributor Covenant](http://contributor-covenant.org) code of conduct.\n\n## License\n\nThe gem is available as open source under the terms of the \n[MIT License](https://opensource.org/licenses/MIT).","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmechanicles%2Fset_as_primary","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fmechanicles%2Fset_as_primary","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fmechanicles%2Fset_as_primary/lists"}