about summary refs log tree commit diff
diff options
context:
space:
mode:
authorChristian Schmidt <github@chsc.dk>2023-03-31 07:33:17 +0200
committerGitHub <noreply@github.com>2023-03-31 07:33:17 +0200
commitb4f38edf74e376f6006def9bf9fc22db40586264 (patch)
tree3500de87e7051ce60c4fc10ae1ef9f224648ccd3
parent68a192e7186733885b1d70160170c4772fab7242 (diff)
Wrong type for user setting when default is defined by lambda (#24321)
-rw-r--r--app/models/user_settings/setting.rb3
-rw-r--r--spec/models/user_settings/setting_spec.rb32
2 files changed, 34 insertions, 1 deletions
diff --git a/app/models/user_settings/setting.rb b/app/models/user_settings/setting.rb
index c359c593b..5f5504254 100644
--- a/app/models/user_settings/setting.rb
+++ b/app/models/user_settings/setting.rb
@@ -19,7 +19,8 @@ class UserSettings::Setting
   end
 
   def type
-    if @default_value.is_a?(TrueClass) || @default_value.is_a?(FalseClass)
+    case default_value
+    when TrueClass, FalseClass
       ActiveModel::Type::Boolean.new
     else
       ActiveModel::Type::String.new
diff --git a/spec/models/user_settings/setting_spec.rb b/spec/models/user_settings/setting_spec.rb
index 6e4ec6789..9884ae4f8 100644
--- a/spec/models/user_settings/setting_spec.rb
+++ b/spec/models/user_settings/setting_spec.rb
@@ -30,6 +30,38 @@ RSpec.describe UserSettings::Setting do
     it 'returns a type' do
       expect(subject.type).to be_a ActiveModel::Type::Value
     end
+
+    context 'when default value is a boolean' do
+      let(:default) { false }
+
+      it 'returns boolean' do
+        expect(subject.type).to be_a ActiveModel::Type::Boolean
+      end
+    end
+
+    context 'when default value is a string' do
+      let(:default) { '' }
+
+      it 'returns string' do
+        expect(subject.type).to be_a ActiveModel::Type::String
+      end
+    end
+
+    context 'when default value is a lambda returning a boolean' do
+      let(:default) { -> { false } }
+
+      it 'returns boolean' do
+        expect(subject.type).to be_a ActiveModel::Type::Boolean
+      end
+    end
+
+    context 'when default value is a lambda returning a string' do
+      let(:default) { -> { '' } }
+
+      it 'returns boolean' do
+        expect(subject.type).to be_a ActiveModel::Type::String
+      end
+    end
   end
 
   describe '#type_cast' do