{"id":15288792,"url":"https://github.com/suusan2go/activerecord-enum_sti","last_synced_at":"2026-01-05T10:31:02.538Z","repository":{"id":59150361,"uuid":"290513015","full_name":"suusan2go/activerecord-enum_sti","owner":"suusan2go","description":"A library allows you to split the class using the Single Table Inheritance (STI) in Rails depending on the value of the enum.TI ","archived":false,"fork":false,"pushed_at":"2020-08-28T12:55:30.000Z","size":27,"stargazers_count":1,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-01T21:25:29.594Z","etag":null,"topics":["activerecord","rails","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/suusan2go.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":"CODE_OF_CONDUCT.md","threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2020-08-26T14:04:31.000Z","updated_at":"2023-03-08T17:57:31.000Z","dependencies_parsed_at":"2022-09-14T04:01:24.144Z","dependency_job_id":null,"html_url":"https://github.com/suusan2go/activerecord-enum_sti","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suusan2go%2Factiverecord-enum_sti","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suusan2go%2Factiverecord-enum_sti/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suusan2go%2Factiverecord-enum_sti/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/suusan2go%2Factiverecord-enum_sti/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/suusan2go","download_url":"https://codeload.github.com/suusan2go/activerecord-enum_sti/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245217791,"owners_count":20579297,"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"],"created_at":"2024-09-30T15:53:11.498Z","updated_at":"2026-01-05T10:31:02.473Z","avatar_url":"https://github.com/suusan2go.png","language":"Ruby","funding_links":[],"categories":[],"sub_categories":[],"readme":"# ActiveaRecord::EnumSti ![CI](https://github.com/suusan2go/activerecord-enum_sti/workflows/CI/badge.svg)\n\nActiveRecord::EnumSti is a library to use enum values as STI type.  \n\nWhen you use enum, you might see the code like following.\n\n```ruby\nclass Payment \u003c ApplicationRecord\n  enum kind: { credit_card: 0, bank_transfer: 10, paypal: 20 }\n\n  def complete!\n    if credit_card?\n    # do something\n    elsif bank_transfer?\n    # do something\n    elsif paypal?\n    # do something\n    end\n  end\n\n  def cancel!\n    if credit_card?\n    # do something\n    elsif bank_transfer?\n    # do something\n    elsif paypal?\n    # do something\n    end\n  end\nend\n```\n\nIn this code, we change the behavior according to the value of enum.\nBut if we can change the class by the value of enum, this code will be more clear.\n\nThis gem allows you to split the class using the Single Table Inheritance (STI) in Rails depending on the value of the enum.\n\n```ruby\nclass Payment \u003c ApplicationRecord\n  include ActiveRecord::EnumSti\n  enum type: { credit_card: 0, bank_transfer: 10, paypal: 20 }\n\n  def complete!\n    raise NotImplementedError\n  end\n\n  def cancel!\n    raise NotImplementedError\n  end\nend\n\nclass Payment::CreditCard \u003c Payment\n  def complete!\n    # do something for creditcard!\n  end\n\n  def cancel!\n    # do something for creditcard!\n  end\nend\n```\n\n## Installation\n\nAdd this line to your application's Gemfile:\n\n```ruby\ngem 'activerecord-enum_sti'\n```\n\nAnd then execute:\n\n    $ bundle\n\nOr install it yourself as:\n\n    $ gem install activerecord-enum_sti\n\n## Usage\nFirst of all, include `ActiveRecord::EnumSti` in your enum defined class.\n\n```ruby\nclass Payment \u003c ApplicationRecord\n  include ActiveRecord::EnumSti\n  enum type: { credit_card: 0, bank_transfer: 10, paypal: 20 }\n  # if you want to use the another column, you can use another enum column by below.\n  # self.inheritance_column = :your_enum_column\nend\n```\n\nThen, please define a class for each enum values. The class name should be `\u003cSuperClass\u003e::\u003ccamelized enum value\u003e`\n\n```ruby\nclass Payment::CreditCard \u003c Payment\nend\n```\n\nNow, you can use the value of enum for STI like below!\n\n```\n@credit_card = Payment::CreditCard.create\n@credit_card.type\n=\u003e \"credit_card\"\nPayment::CreditCard.all.to_sql\n=\u003e \"SELECT \\\"payments\\\".* FROM \\\"payments\\\" WHERE \\\"payments\\\".\\\"type\\\" = 0\"\n```\n\n\n## Development\n\nAfter checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` 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/[USERNAME]/activerecord-enum_sti. This project is intended to be a safe, welcoming space for collaboration, and contributors are expected to adhere to 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 [MIT License](https://opensource.org/licenses/MIT).\n\n## Code of Conduct\n\nEveryone interacting in the Activerecord::EnumSti project’s codebases, issue trackers, chat rooms and mailing lists is expected to follow the [code of conduct](https://github.com/[USERNAME]/activerecord-enum_sti/blob/master/CODE_OF_CONDUCT.md).\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuusan2go%2Factiverecord-enum_sti","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fsuusan2go%2Factiverecord-enum_sti","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fsuusan2go%2Factiverecord-enum_sti/lists"}