Module: SimpleEnum::ViewHelpers

Defined in:
lib/simple_enum/view_helpers.rb

Instance Method Summary collapse

Instance Method Details

#enum_option_pairs(record, enum, encode_as_value = false) ⇒ Object

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”], …]`


21
22
23
24
25
26
27
28
29
30
# 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

#translate_enum(record, key) ⇒ Object Also known as: te

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


43
44
45
# 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