Module: TranslateEnum::ClassMethods
- Defined in:
- lib/translate_enum.rb
Overview
include TranslateEnum in ActiveModel or ActiveRecord
Instance Method Summary collapse
Instance Method Details
#translate_enum(attribute, &block) ⇒ Object
21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/translate_enum.rb', line 21 def translate_enum(attribute, &block) builder = Builder.new(self, attribute, &block) # User.translated_status(:active) define_singleton_method(builder.method_name_singular) do |key, throw: false, raise: false, locale: nil, **| opts = { default: builder.i18n_default_location(key) }.merge() I18n.translate("#{builder.i18n_scope}.#{builder.i18n_location(key)}", throw: throw, raise: raise, locale: locale, **opts) end # @return [Array] # @example # f.select_field :gender, f.object.class.translated_genders define_singleton_method(builder.method_name_plural) do |throw: false, raise: false, locale: nil, **| = { count: 1 }.merge() public_send(builder.enum_klass_method_name).map do |key, value| translation = public_send(builder.method_name_singular, key, throw: throw, raise: raise, locale: locale, **) [translation, key, value] end end # @return [String] # @example # @user.translated_gender define_method(builder.method_name_singular) do |throw: false, raise: false, locale: nil, **| if (key = public_send(builder.enum_instance_method_name)).present? = { count: 1 }.merge() self.class.public_send(builder.method_name_singular, key, throw: throw, raise: raise, locale: locale, **) end end end |