Skip to content

ActiveRecord association with a class-based tableless enumerators

Notifications You must be signed in to change notification settings

aloon/has_dictionary

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

17 Commits

Repository files navigation

has_dictionary API Documentation

has_dictionary Gem

ActiveRecord association with a class-based tableless enumerators.

install

in Gemfile add:

gem 'has_dictionary' 

and execute:

$ bundle install 

how it works

You must create a class with the definitions of Enums

For example:

models/status.rb:

classStatus < Dictionaryself.add_item:NEW,0self.add_item:GENERATING,1self.add_item:RUN,2self.add_item:DELETING,3end

In your ActiveRecord class

classMyClass < ActiveRecord::Baseattr_accessible:name,:status_idhas_dictionary:dic_class=>Status,:field=>:status_id,:method=>'status'end

now you can:

MyClass.find(1).status# returns #<Status:0x00 @id=0, @description="NEW">MyClass.find(1).status.description# returns "NEW"element=MyClass.find(1)element.status_id=Status::GENERATINGelements.save# this save element.status_id with the value 1Status.get#returns [#<Status:0x00 @id=0, @description="NEW">, #<Status:0x00 @id=1, @description="GENERATING">, #<Status:0x00 @id=2, @description="RUN">, #<Status:0x00 @id=3, @description="DELETING">] 

About

ActiveRecord association with a class-based tableless enumerators

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages