Created
July 20, 2017 00:59
-
-
Save amyroi/125bdd919a95e4c9c9e7740b695c2c32 to your computer and use it in GitHub Desktop.
Enum I18n
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
module EnumI18n | |
extend ActiveSupport::Concern | |
included do | |
after_initialize :set_enum_text | |
end | |
def set_enum_text | |
name = (self.class == ::User) ? 'user' : model_name&.element | |
defined_enums.each do |enum| | |
enum_column = enum.first | |
class_eval <<-METHOD | |
def #{enum_column}_text(scope=nil) | |
scope ||= 'activerecord.enum.#{name}.#{enum_column}' | |
I18n.t(#{enum_column}, scope: scope) | |
end | |
METHOD | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment