Skip to content

Instantly share code, notes, and snippets.

@vilusa
Created December 10, 2020 07:43
Show Gist options
  • Save vilusa/d4320a2bac5a0a3f1402f4bf2c6044db to your computer and use it in GitHub Desktop.
Save vilusa/d4320a2bac5a0a3f1402f4bf2c6044db to your computer and use it in GitHub Desktop.
Enum Presenter
# 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