Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
こんなかんじの作りたい。ばーっとイメージで書いてしまってるので Hash のときと Array のときで対応変える必要ありそうな気がする。Hash は invert で。
module EnumHashSelector
DEFAULT_RESOLVER = ->(model_class, method_name, key) {
"activerecord.enums.#{model_class.name.underscore}.#{method_name.to_s}.#{key}"
}
def self.i18n_path_resolver=(resolver = DEFAULT_RESOLVER)
resolver.call(model_class, method_name, key)
end
def enum_selector(model_class, method_name)
model_class.send(method_name).keys.map do |key|
i18n_path = self.class.i18n_path_resolver.call(model_class, method_name, key)
[I18n.t(i18n_path), key]
end
end
end
# Usage
module ActiveAdmin::ViewHelpers
include EnumHashSelector # include, extend
end
@sunaot

This comment has been minimized.

Copy link
Owner Author

sunaot commented Feb 8, 2016

この方式はいまいちだなーとなったので、やめた。AR::Base.human_attribute_name('attribute_name.value') を活かすほうが I18n.translate をちまちまとやる必要が少なくてよさそう。

ja:
  activerecord:
    attributes:
      user/gender:
        male: "Male"
        female: "Female"

User.human_attribute_name("gender.female") で解決できるのと同じ形で enum も解消してやり、そこから collection へつなげる。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.