Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 14 additions & 0 deletions lib/enum_help/i18n.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ def enum( definitions )
definitions.each do |name, _|
Helper.define_attr_i18n_method(self, name)
Helper.define_collection_i18n_method(self, name)
Helper.define_collection_i18n_method_before_type_cast(self, name)
end
end

Expand Down Expand Up @@ -50,6 +51,19 @@ def #{collection_i18n_method_name}
METHOD
end

def self.define_collection_i18n_method_before_type_cast(klass, attr_name)
collection_method_name = "#{attr_name.to_s.pluralize}"
collection_i18n_method_name = "#{collection_method_name}_i18n_before_type_cast"
klass.instance_eval <<-METHOD, __FILE__, __LINE__
def #{collection_i18n_method_name}
collection_array = #{collection_method_name}.collect do |label, _|
[#{klass.public_send(collection_method_name)}[label], ::EnumHelp::Helper.translate_enum_label('#{klass}', :#{attr_name}, label)]
end
Hash[collection_array].with_indifferent_access
end
METHOD
end

def self.translate_enum_label(klass, attr_name, enum_label)
::I18n.t("enums.#{klass.to_s.underscore.gsub('/', '.')}.#{attr_name}.#{enum_label}", default: enum_label.humanize)
end
Expand Down