Coder Social home page Coder Social logo

str_enum's Introduction

str_enum

Don’t like storing enums as integers in your database? Introducing...

String enums for Rails!! 🎉

  • scopes
  • validations
  • accessor methods
  • update methods

Build Status

Getting Started

Add this line to your application’s Gemfile:

gem "str_enum"

Add a string column to your model.

add_column :users, :status, :string

And use:

class User < ActiveRecord::Base
  str_enum :status, [:active, :archived]
end

The first value will be the initial value. This gives you:

Scopes

User.active
User.archived

And negative scopes

User.not_active
User.not_archived

Validations

user = User.new(status: "unknown")
user.valid? # false

Accessor Methods

user.active?
user.archived?

Update Methods

user.active!
user.archived!

Forms

<%= f.select :status, User.statuses.map { |s| [s.titleize, s] } %>

Options

Choose which features you want with (default values shown):

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.

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

Contributing

Everyone is encouraged to help improve this project. Here are a few ways you can help:

To get started with development and testing:

git clone https://github.com/ankane/str_enum.git
cd str_enum
bundle install
bundle exec rake test

str_enum's People

Contributors

ankane avatar joshuapinter avatar dlackty avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.