Created
December 7, 2015 09:26
-
-
Save sunaot/c2a391fa9531f50367bb to your computer and use it in GitHub Desktop.
こんなかんじの作りたい。ばーっとイメージで書いてしまってるので Hash のときと Array のときで対応変える必要ありそうな気がする。Hash は invert で。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
この方式はいまいちだなーとなったので、やめた。
AR::Base.human_attribute_name('attribute_name.value')
を活かすほうがI18n.translate
をちまちまとやる必要が少なくてよさそう。を
User.human_attribute_name("gender.female")
で解決できるのと同じ形で enum も解消してやり、そこから collection へつなげる。