Skip to content

Instantly share code, notes, and snippets.

@rainchen
Created June 5, 2013 04:43
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save rainchen/5711657 to your computer and use it in GitHub Desktop.
Save rainchen/5711657 to your computer and use it in GitHub Desktop.
Clever enums in rails using module style
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
@rainchen
Copy link
Author

rainchen commented Jun 5, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment