From 1c6250bff8ca4f0027dcf1b576881de1036a1d1d Mon Sep 17 00:00:00 2001 From: Sam Date: Wed, 26 Aug 2015 16:39:09 +1000 Subject: [PATCH] FIX: if an enum is Fixnum do not allow strings to live in it --- lib/site_setting_extension.rb | 1 + .../components/site_setting_extension_spec.rb | 29 +++++++++++++++++++ 2 files changed, 30 insertions(+) diff --git a/lib/site_setting_extension.rb b/lib/site_setting_extension.rb index 19ad366f06e..276ec908184 100644 --- a/lib/site_setting_extension.rb +++ b/lib/site_setting_extension.rb @@ -291,6 +291,7 @@ module SiteSettingExtension end if type == types[:enum] + val = val.to_i if Fixnum === defaults[name.to_sym] if enum_class(name) raise Discourse::InvalidParameters.new(:value) unless enum_class(name).valid_value?(val) else diff --git a/spec/components/site_setting_extension_spec.rb b/spec/components/site_setting_extension_spec.rb index d9509e7147e..0f938a6af74 100644 --- a/spec/components/site_setting_extension_spec.rb +++ b/spec/components/site_setting_extension_spec.rb @@ -224,6 +224,35 @@ describe SiteSettingExtension do end end + describe 'int enum' do + class TestIntEnumClass + def self.valid_value?(v) + true + end + def self.values + [1,2,3] + end + def self.translate_names? + true + end + end + + let :test_enum_class do + TestEnumClass + end + + before do + settings.setting(:test_enum, 1, enum: TestEnumClass) + settings.refresh! + end + + it 'should coerce correctly' do + settings.test_enum = "2" + expect(settings.test_enum).to eq(2) + end + + end + describe 'enum setting' do class TestEnumClass