diff --git a/lib/enumerated_attribute/attribute/attribute_descriptor.rb b/lib/enumerated_attribute/attribute/attribute_descriptor.rb index 3ea330c..fd8d2a9 100644 --- a/lib/enumerated_attribute/attribute/attribute_descriptor.rb +++ b/lib/enumerated_attribute/attribute/attribute_descriptor.rb @@ -9,7 +9,11 @@ def initialize(name, enums=[], opts={}) super enums @name = name @options = opts - @labels_hash = Hash[*self.collect{|e| [e, e.to_s.gsub(/_/, ' ').squeeze(' ').capitalize]}.flatten] + if @options.key?(:localize) && @options[:localize] + @labels_hash = Hash[*self.collect{|e| [e, e.to_s]}.flatten] + else + @labels_hash = Hash[*self.collect{|e| [e, e.to_s.gsub(/_/, ' ').squeeze(' ').capitalize]}.flatten] + end end def allows_nil? @@ -23,18 +27,21 @@ def enums self end def label(value) - @labels_hash[value] + if @options.key?(:localize) && @options[:localize] + I18n.t(@labels_hash[value]) + else + @labels_hash[value] + end end def labels - @labels_array ||= self.map{|e| @labels_hash[e]} + @labels_array ||= self.map{|e| label(e)} end def hash @labels_hash end def select_options - @select_options ||= self.map{|e| [@labels_hash[e], e.to_s]} + @select_options ||= self.map{|e| [label(e), e.to_s]} end - def set_label(enum_value, label_string) reset_labels @labels_hash[enum_value.to_sym] = label_string