Giter VIP home page Giter VIP logo

enum_fu's Introduction

!!! IMPORTANT NOTICE 
 - Rails supports enum from version 4.1
 - This project is not being maintained now

With enum_fu, you can use integer fields in DB as enum typed in ActiveRecord.
 - faster operation in DB
 - easy coding 



Usage 
======

# db schema
create_table 'users' do |t|
  t.column 'role', :integer, :limit => 2
end

# model
class User < ActiveRecord::Base
  acts_as_enum :role, [:customer, :admin]
end

# usage


# create new record
u = User.new(:role => :customer) 
u.save

# get value
u.role				# :customer
u.role_before_type_cast		# 0

# set value
u.role = :user


# get the integer value for an enum value
User.role(:user)		# 1


# list of all enum values  
User::ROLE			# [:customer, :admin]


# in a view
<% form_for :customer, :url => {:action => 'create'} do |f| %>
    <%= f.select :role, Customer::ROLE %>
<% end %>

# in a controller
u = User.new(params[:customer])


#list of all enum values with humanized names, useful for selects when enum has a composed name as:

#model
class User < ActiveRecord::Base
  acts_as_enum :role, [:customer, :admin, :special_user]
end

User::ROLE_HUMAN		# [['Customer',:customer], ['Admin',:admin], ['Special user',:special_user]]


#hash with all enum values with humanized names associated with the integer value, useful for plugin like filter_table:

User::ROLE_VALUES		# {'Customer' => 0, 'Admin' => 1, 'Special user' => 2}



History 
=======

Version 0.3 
-----------
    2008.09.04 
    Applied patches from Hongli Lia(www.phusion.nl)

    - Fixed a type in Readme 

    - The singleton enum value getter method (e.g. "Car.status(:broken)   =>  1") is now defined on the metaclass instead of on 'SomeModel.class'. This is because 'SomeModel.class' always returns 'Class', so you actually ended up defining this singleton method on every class in the system.

    - The value assignment method (e.g. "@car.status = ...") can now also accept numbers (e.g. 123) and number strings (e.g. "123"). This allows one to call "Car.new(:car => { :broken => '1' })" (There was a similiar patch also from Georg Ledermann long before.)

    - Unit tests have been added. (Wow! Big applause to Hongli Lai)

Version 0.2 
-----------
    2007.12.28 
    Applied patches from Norman Clarke and Georg Ledermann 

    - accpet nil as input
	user.role = nil

Version 0.1 
-----------
    Someday in 2007 
    First Release

enum_fu's People

Contributors

flaviogranero avatar ikspres avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

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