Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/ankane/str_enum
String enums for Rails
https://github.com/ankane/str_enum
Last synced: about 1 month ago
JSON representation
String enums for Rails
- Host: GitHub
- URL: https://github.com/ankane/str_enum
- Owner: ankane
- License: mit
- Created: 2016-10-24T01:13:20.000Z (almost 8 years ago)
- Default Branch: master
- Last Pushed: 2024-06-10T03:18:35.000Z (3 months ago)
- Last Synced: 2024-07-21T06:24:24.756Z (about 2 months ago)
- Language: Ruby
- Size: 50.8 KB
- Stars: 81
- Watchers: 3
- Forks: 9
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# str_enum
Don’t like storing enums as integers in your database? Introducing...
String enums for Rails!! :tada:
- scopes
- validations
- accessor methods
- update methods[![Build Status](https://github.com/ankane/str_enum/actions/workflows/build.yml/badge.svg)](https://github.com/ankane/str_enum/actions)
## Getting Started
Add this line to your application’s Gemfile:
```ruby
gem "str_enum"
```Add a string column to your model.
```ruby
add_column :users, :status, :string
```And use:
```ruby
class User < ActiveRecord::Base
str_enum :status, [:active, :archived]
end
```The first value will be the initial value. This gives you:
#### Scopes
```ruby
User.active
User.archived
```And negative scopes
```ruby
User.not_active
User.not_archived
```#### Validations
```ruby
user = User.new(status: "unknown")
user.valid? # false
```#### Accessor Methods
```ruby
user.active?
user.archived?
```#### Update Methods
```ruby
user.active!
user.archived!
```#### Forms
```erb
<%= f.select :status, User.statuses.map { |s| [s.titleize, s] } %>
```## Options
Choose which features you want with (default values shown):
```ruby
class User < ActiveRecord::Base
str_enum :status, [:active, :archived],
accessor_methods: true,
allow_nil: false,
default: true,
prefix: false,
scopes: true,
suffix: false,
update_methods: true,
validate: true
end
```Prevent method name collisions with the `prefix` and `suffix` options.
```ruby
class User < ActiveRecord::Base
str_enum :address_status, [:active, :archived], suffix: :address
end# scopes
User.active_address
User.archived_address# accessor methods
user.active_address?
user.archived_address?# update methods
user.active_address!
user.archived_address!
```## History
View the [changelog](https://github.com/ankane/str_enum/blob/master/CHANGELOG.md)
## Contributing
Everyone is encouraged to help improve this project. Here are a few ways you can help:
- [Report bugs](https://github.com/ankane/str_enum/issues)
- Fix bugs and [submit pull requests](https://github.com/ankane/str_enum/pulls)
- Write, clarify, or fix documentation
- Suggest or add new featuresTo get started with development and testing:
```sh
git clone https://github.com/ankane/str_enum.git
cd str_enum
bundle install
bundle exec rake test
```