Giter Club home page Giter Club logo

native_enum's Introduction

Native ENUM types in ActiveRecord

Provides ActiveRecord (and thus Rails) support for the nonstandard ENUM and SET data types.

Build Status

How now?

It sucks to have ActiveRecord not understand your ENUM columns and constantly write the wrong thing to your schema.rb. It also sucks to work with a database that uses ENUM in production when you'd prefer sqlite in development. Wait no longer...

create_table :balloons, :force => true do |t|
  t.enum "color", :limit => ['red', 'gold'], :default => 'gold', :null => false
  # or...
  t.column "size", :enum, :limit => ['small', 'medium', 'large']
end

Your schema<->db coupling will work again, and it will fall back to a VARCHAR column on any adapters that don't support ENUM.

Installation

gem 'native_enum'

Make sure to put this line after the line for your database adapter gem, like so:

gem 'mysql2', '~> 0.3.20'
gem 'native_enum'

That's it!

Hypothetically asked questions

Y U NO WORK?!

It currently works with:

  • ActiveRecord 4.0, 4.1, 4.2, 5.0, and 5.1.
  • The mysql2 and sqlite adapters.
  • Ruby 2.0 and above.

If you'd like to support other adapters, pull requests are welcome!

I thought Rails 4.1+ did enums?

This gem provides compatibility for native ENUM types in the DB. ActiveRecord::Enum is actually a layer that takes an integer column in the DB and presents it as an enum type. These are fundamentally different approaches, with different motivations, costs and benefits.

Why doesn't it validate anything?

Following ActiveRecord's lead, this plugin doesn't do any validation work itself.

For ENUM columns, you may be satisfied with something simple:

validates_inclusion_of :attr, :in => [ :possible, :values ]

Or if you prefer more bells and whistles, try brainspec/enumerize.

For SET columns, you may be interested in iangreenleaf/active_set.

Nonstandard SQL?! What's your problem, jerkweed?

This isn't a plugin everyone should use. There are a number of plugins to simulate enum behavior backed by standard data types. Personally, I like nofxx/symbolize.

However, sometimes we can't or won't avoid working with these data types. When that happens, I got you covered.

Contributing

Pull requests welcome! Join this lovely bunch of people.

Running the tests

To run the tests for all adapters and all versions of ActiveRecord:

appraisal rake spec:all

To run the tests for a specific adapter:

DB=mysql rake spec

To run the tests for a specific version of ActiveRecord:

appraisal activerecord-5.2 rake spec:all

native_enum's People

Contributors

iangreenleaf avatar mcls avatar enriquevidal avatar stumathews avatar fruwe avatar

Watchers

James Cloos avatar  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.