diff --git a/lib/foodsoft_config.rb b/lib/foodsoft_config.rb index eb238a7a..1975f2c7 100644 --- a/lib/foodsoft_config.rb +++ b/lib/foodsoft_config.rb @@ -109,15 +109,7 @@ class FoodsoftConfig # @return [Boolean] Whether storing succeeded (fails when key is not allowed to be set in database). def []=(key, value) return false unless allowed_key?(key) - # try to figure out type ... - value = case value - when 'true' then true - when 'false' then false - when /^[-+0-9]+$/ then value.to_i - when /^[-+0-9.]+([eE][-+0-9]+)?$/ then value.to_f - when '' then nil - else value - end + value = normalize_value value # then update database if config[key] == value or (config[key].nil? and value == false) # delete (ok if it was already deleted) @@ -254,5 +246,20 @@ class FoodsoftConfig cfg end + # Normalize value recursively (which can be entered as strings, but we want to store it properly) + def normalize_value(value) + value = value.map(&:normalize_value) if value.is_a? Array + value = Hash[ value.to_a.map{|a| [a[0], normalize_value(a[1])]} ] if value.is_a? Hash + case value + when 'true' then true + when 'false' then false + when /^[-+0-9]+$/ then value.to_i + when /^[-+0-9.]+([eE][-+0-9]+)?$/ then value.to_f + when '' then nil + else value + end + end + + end end diff --git a/spec/integration/config_spec.rb b/spec/integration/config_spec.rb index d62f0894..ee7b2956 100644 --- a/spec/integration/config_spec.rb +++ b/spec/integration/config_spec.rb @@ -38,6 +38,7 @@ describe 'admin/configs', type: :feature do def get_full_config cfg = FoodsoftConfig.to_hash.deep_dup cfg.each {|k,v| v.reject! {|k,v| v.blank?} if v.is_a? Hash} + cfg.reject! {|k,v| v.blank?} cfg end