Giter VIP home page Giter VIP logo

enumerate's Introduction

Enumerate Build Status Gem Version Code Climate

Enumerate adds an enum command to all ActiveRecord models which enables you to work with integer and string attributes as if they were enums

Installing

Just add the enumerate gem to your GemFile

gem 'enumerate'

How to use

Just call the enum function in any ActiveRecord object, the function accepts the field name as the first variable and the possible values as an array

class Event < ActiveRecord::Base
    enum :status, [:available, :canceled, :completed]
end

If your status column is of type integer, Enumerate will assign the indices of the array as the values stored in the database.

:available will be 0 :canceled will be 1 :completed will be 2

If you wish to specify yourself, simply use a hash

class Event < ActiveRecord::Base
    enum :status, {:available => 0, :canceled => 2, :completed => 5}
end

After that you get several autogenerated commands to use with the enum

# Access through field name

event.status                # returns the enum's current value as a symbol
event.status = :canceled    # sets the enum's value to canceled (can also get a string)


# Shorthand methods, access through the possible values

event.available?            # returns true if enum's current status is available
event.canceled!             # changes the enum's value to canceled

# Get all the possible values

Event::STATUSES             # returns all available status of the enum

Options

:allow_nil

By default the enum field does not support a nil value. In order to allow nil values add the allow_nil option (similar to the Rails validation option).

class Event < ActiveRecord::Base
    enum :status, [:available, :canceled, :completed], :allow_nil => true
end

Event.create! # Is valid and does not throw an exception.

Scopes

One last thing that the enumerate gem does is created scope (formerly nested_scopes) so you can easly query by the enum

For example if you want to count all the events that are canceled you can just run

Event.canceled.count

Copyright (c) 2013 Dale Stevens, released under the MIT license

enumerate's People

Contributors

voltechs avatar

Watchers

 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.