FIX: if an enum is Fixnum do not allow strings to live in it

This commit is contained in:
Sam 2015-08-26 16:39:09 +10:00
parent ea9bc68ee5
commit 1c6250bff8
2 changed files with 30 additions and 0 deletions

View File

@ -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

View File

@ -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