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
24 25 26 27 28 29 30 31 32 33 34 35 36 37 |
# File 'lib/enum_help/i18n.rb', line 24 def self.define_attr_i18n_method(klass, attr_name) attr_i18n_method_name = "#{attr_name}_i18n" klass.class_eval " def \#{attr_i18n_method_name}\n enum_label = self.send(:\#{attr_name})\n if enum_label\n ::EnumHelp::Helper.translate_enum_label(self.class, :\#{attr_name}, enum_label)\n else\n nil\n end\n end\n METHOD\nend\n", __FILE__, __LINE__ |
.define_collection_i18n_method(klass, attr_name) ⇒ Object
39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/enum_help/i18n.rb', line 39 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 " def \#{collection_i18n_method_name}\n collection_array = \#{collection_method_name}.collect do |label, _|\n [label, ::EnumHelp::Helper.translate_enum_label(self, :\#{attr_name}, label)]\n end\n Hash[collection_array].with_indifferent_access\n end\n METHOD\nend\n", __FILE__, __LINE__ |
.translate_enum_label(klass, attr_name, enum_label) ⇒ Object
53 54 55 |
# File 'lib/enum_help/i18n.rb', line 53 def self.translate_enum_label(klass, attr_name, enum_label) ::I18n.t("enums.#{klass.to_s.underscore}.#{attr_name}.#{enum_label}", default: enum_label) end |