From 8edb6b3d84b74e947868e05a4a63b9cd7925a808 Mon Sep 17 00:00:00 2001 From: byzg Date: Mon, 20 Feb 2017 17:16:32 +0300 Subject: [PATCH] ids as values --- lib/enum_help/i18n.rb | 6 +++--- lib/enum_help/simple_form.rb | 6 ++++-- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/lib/enum_help/i18n.rb b/lib/enum_help/i18n.rb index 6a3fe46..d6ec687 100644 --- a/lib/enum_help/i18n.rb +++ b/lib/enum_help/i18n.rb @@ -41,9 +41,9 @@ def self.define_collection_i18n_method(klass, attr_name) collection_i18n_method_name = "#{collection_method_name}_i18n" klass.instance_eval <<-METHOD, __FILE__, __LINE__ - def #{collection_i18n_method_name} - collection_array = #{collection_method_name}.collect do |label, _| - [label, ::EnumHelp::Helper.translate_enum_label('#{klass}', :#{attr_name}, label)] + def #{collection_i18n_method_name}(ids = :lables) + collection_array = #{collection_method_name}.collect do |label, value| + [(ids == :values ? value : label), ::EnumHelp::Helper.translate_enum_label('#{klass}', :#{attr_name}, label)] end Hash[collection_array].with_indifferent_access end diff --git a/lib/enum_help/simple_form.rb b/lib/enum_help/simple_form.rb index be86a26..f5ebe70 100644 --- a/lib/enum_help/simple_form.rb +++ b/lib/enum_help/simple_form.rb @@ -33,10 +33,12 @@ def initialize(*args) enum = input_options[:collection] || @builder.options[:collection] raise "Attribute '#{attribute_name}' has no enum class" unless enum ||= object.class.send(attribute_name.to_s.pluralize) - enum = enum.keys if enum.is_a? Hash + if enum.is_a? Hash + enum = input_options[:ids] == :values ? enum.values : enum.keys + end collect = begin - collection = object.class.send("#{attribute_name.to_s.pluralize}_i18n") + collection = object.class.send("#{attribute_name.to_s.pluralize}_i18n", input_options[:ids]) collection.slice!(*enum) if enum collection.invert.to_a end