lib/mongoid/enum.rb in mongoid-enum-i18n-0.3.3 vs lib/mongoid/enum.rb in mongoid-enum-i18n-0.5.0
- old
+ new
@@ -26,12 +26,15 @@
create_i18n_helper field_name, options
create_values_helper field_name, options
create_validations field_name, values, options
define_value_scopes_and_accessors field_name, values, options
- return unless options[:multiple]
- define_array_field_writer field_name
+ if options[:multiple]
+ define_array_field_writer field_name
+ else
+ define_field_writer field_name
+ end
end
private
def default_options
@@ -48,20 +51,27 @@
end
def create_field(field_name, options, values)
type = options[:multiple] && Array || Symbol
default = \
- if options.key?(:default)
- options[:default]
- else
- options[:multiple] ? [] : values.first
- end
+ if options.key?(:default)
+ options[:default]
+ else
+ options[:multiple] ? [] : values.first
+ end
field field_name, type: type, default: default
end
def define_array_field_writer(field_name)
define_method("#{field_name}=") do |vals|
write_attribute(field_name, Array(vals).compact.map(&:to_sym))
+ end
+ end
+
+ def define_field_writer(field_name)
+ define_method("#{field_name}=") do |val|
+ val = nil if val.respond_to?(:empty?) && val.empty?
+ write_attribute(field_name, val)
end
end
def create_validations(field_name, values, options)
return unless options[:validate]