module SimpleEnum::ViewHelpers

Public Instance Methods

enum_option_pairs(record, enum, encode_as_value = false) click to toggle source

A helper to build forms with Rails' form builder, built to be used with f.select helper.

f.select :gender, enum_option_pairs(User, :gender), ...

record - The model or Class with the enum enum - The Symbol with the name of the enum to create the options for encode_as_value - The Boolean which defines if either the key or the value

should be used as value attribute for the option,
defaults to using the key (i.e. false)

FIXME: check if the name `enum_option_pairs` is actually good and clear enough…

Returns an Array of pairs, like e.g. `[[“Translated key”, “key”], …]`

# File lib/simple_enum/view_helpers.rb, line 21
def enum_option_pairs(record, enum, encode_as_value = false)
  reader = enum.to_s.pluralize
  record = record.class unless record.respond_to?(reader)

  record.send(reader).map { |key, value|
    name = record.human_enum_name(enum, key) if record.respond_to?(:human_enum_name)
    name ||= translate_enum_key(enum, key)
    [name, encode_as_value ? value : key]
  }
end
te(record, key)
Alias for: translate_enum
translate_enum(record, key) click to toggle source

Helper method to return the translated value of an enum.

translate_enum(user, :gender) # => "Frau"

Has been aliased to `te` as a convenience method as well.

record - The model instance holding the enum key - The Symbol with the name of the enum, i.e. same key as used in the

`as_enum` call

Returns String with translation of enum

# File lib/simple_enum/view_helpers.rb, line 43
def translate_enum(record, key)
  record.class.human_enum_name(key, record.public_send(key))
end
Also aliased as: te

Private Instance Methods

translate_enum_key(enum, key) click to toggle source
# File lib/simple_enum/view_helpers.rb, line 50
def translate_enum_key(enum, key)
  defaults = [:"enums.#{enum}.#{key}", key.to_s.humanize]
  I18n.translate defaults.shift, default: defaults, count: 1
end