Last active
February 28, 2017 16:12
-
-
Save sangkyoonnam/fbcf652f31410db852a6671dcc0bbab4 to your computer and use it in GitHub Desktop.
This file contains 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
require 'active_support' | |
module CustomEnum | |
extend ActiveSupport::Concern | |
def enum(name, values, attr_name:) | |
attr = name | |
attr = attr_name if attr_name.present? | |
define_method("#{name}") { values.key(read_attribute(attr)) } | |
define_method("#{name}=") { |s| write_attribute(attr, values[s]) } | |
end | |
end | |
ActiveRecord::Base.send :extend, CustomEnum |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment