Giter Club home page Giter Club logo

enum_simulator's Introduction

EnumSimulator

A simple plugin for abstracting out standard ActiveRecord enumerated attributes

Installation

Let bundler do all the work for you by adding this to your Gemfile:

gem 'enum_simulator'

And then execute:

bundle install

Or install it manually:

sudo gem install enum_simulator

Example

In order to mark an AR attribute as enumerated, make sure the column in question is a string and large enough to accommodate the string representation of your largest possible value. Then, in the model file, just call “enum”, passing it the symbol of the attribute and an array of symbols you’d like to describe as the possible values:

class IceCream < ActiveRecord::Base
  enum :flavor, [:chocolate, :vanilla, :strawberry, :rockyroad]
end

You can also pass a hash to enum, which will automatically validate the attribute’s inclusion in the keys of that hash. This is useful for attributes that may need longer human-readable descriptions on display. An example of this:

class PickupTruck < ActiveRecord::Base
  TRUCK_TYPES = {
    :compact_sb => "Compact Short Bed",
    :compact_lb => "Compact Long Bed",
    :compact_ext_sb => "Compact Extended Cab Short Bed",
    :fullsize_sb => "Full-sized Short Bed",
    :fullsize_lb => "Full-sized Long Bed",
    :fullsize_ext_sb => "Full-sized Extended Cab Short Bed"
  }
  enum :truck_type, TRUCK_TYPES
end

And then in a view you can do something like this:

<%= select_tag :truck_type, options_for_select(PickupTruck.enumerated_attributes[:truck_type].map { |k, v| [ v, k ] }) %>

You could also have used the PickupTruck::TRUCK_TYPES constant in this example, but the enumerated_attributes hash is a handy way to access whatever values were passed into the enum definition, especially if you haven’t put them in a constant somewhere.

enum_simulator will handle validation of inclusion and conversion of the attribute values from symbols to strings and back internally. Honestly, that’s really all it does - it’s just such a common pattern that I figured we might as well have a more succinct way of using it. It will infer whether or not to allow assignment of nil to your attribute from the column definition. If your column allows null, then the enumerated_attributes hash for that column will also include nil. This means you do not need an :include_blank in any form tags related to the attribute in question.

Copyright © 2012 Centresource, released under the MIT license

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.