Skip to content

Instantly share code, notes, and snippets.

@amyroi
Created July 20, 2017 00:59
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amyroi/125bdd919a95e4c9c9e7740b695c2c32 to your computer and use it in GitHub Desktop.
Save amyroi/125bdd919a95e4c9c9e7740b695c2c32 to your computer and use it in GitHub Desktop.
Enum I18n
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