Created
June 5, 2013 04:43
-
-
Save rainchen/5711657 to your computer and use it in GitHub Desktop.
Clever enums in rails using module style
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Photo < ActiveRecord::Base | |
module Statues | |
QUEUED = 'queued' | |
NEW = 'new' | |
def self.values | |
constants.map(&method(:const_get)) | |
end | |
# you can define more special methods as you wanted for Statues | |
end | |
validates :status, inclusion: {in: Statues.values } | |
def change_status | |
self.status = Statues::NEW | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
for http://rails-bestpractices.com/posts/708-clever-enums-in-rails