Created
December 10, 2020 07:43
-
-
Save vilusa/d4320a2bac5a0a3f1402f4bf2c6044db to your computer and use it in GitHub Desktop.
Enum Presenter
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
# frozen_string_literal: true | |
module EnumPresenter | |
class << self | |
def collect(model_class, enum_field, **options) | |
i18ns = model_class.send("#{enum_field}_i18n") | |
keys = if options[:except].present? | |
model_class.send(enum_field).except(*options[:except]) | |
else | |
model_class.send(enum_field) | |
end | |
keys.map do |key, value| | |
if options[:as_keys] | |
[i18ns[key], key] | |
else | |
[i18ns[key], value] | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment