Module: EnumHelp::Helper
- Defined in:
- lib/enum_help/i18n.rb
Class Method Summary collapse
- .define_attr_i18n_method(klass, attr_name) ⇒ Object
- .define_collection_i18n_method(klass, attr_name) ⇒ Object
- .translate_enum_label(klass, attr_name, enum_label) ⇒ Object
Class Method Details
.define_attr_i18n_method(klass, attr_name) ⇒ Object
41 42 43 44 45 46 47 48 49 50 51 52 53 54 |
# File 'lib/enum_help/i18n.rb', line 41 def self.define_attr_i18n_method(klass, attr_name) attr_i18n_method_name = "#{attr_name}_i18n" klass.class_eval <<-METHOD, __FILE__, __LINE__ def #{attr_i18n_method_name} enum_label = self.send(:#{attr_name}) if enum_label ::EnumHelp::Helper.translate_enum_label('#{klass}', :#{attr_name}, enum_label) else nil end end METHOD end |
.define_collection_i18n_method(klass, attr_name) ⇒ Object
56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/enum_help/i18n.rb', line 56 def self.define_collection_i18n_method(klass, attr_name) collection_method_name = "#{attr_name.to_s.pluralize}" collection_i18n_method_name = "#{collection_method_name}_i18n" klass.instance_eval <<-METHOD, __FILE__, __LINE__ def #{collection_i18n_method_name} collection_array = #{collection_method_name}.collect do |label, _| [label, ::EnumHelp::Helper.translate_enum_label('#{klass}', :#{attr_name}, label)] end Hash[collection_array].with_indifferent_access end METHOD end |
.translate_enum_label(klass, attr_name, enum_label) ⇒ Object
70 71 72 |
# File 'lib/enum_help/i18n.rb', line 70 def self.translate_enum_label(klass, attr_name, enum_label) ::I18n.t("enums.#{klass.to_s.underscore.gsub('/', '.')}.#{attr_name}.#{enum_label}", default: enum_label.humanize) end |