Wrong type for user setting when default is defined by lambda (#24321)
This commit is contained in:
		
					parent
					
						
							
								68a192e718
							
						
					
				
			
			
				commit
				
					
						b4f38edf74
					
				
			
		
					 2 changed files with 34 additions and 1 deletions
				
			
		|  | @ -19,7 +19,8 @@ class UserSettings::Setting | ||||||
|   end |   end | ||||||
| 
 | 
 | ||||||
|   def type |   def type | ||||||
|     if @default_value.is_a?(TrueClass) || @default_value.is_a?(FalseClass) |     case default_value | ||||||
|  |     when TrueClass, FalseClass | ||||||
|       ActiveModel::Type::Boolean.new |       ActiveModel::Type::Boolean.new | ||||||
|     else |     else | ||||||
|       ActiveModel::Type::String.new |       ActiveModel::Type::String.new | ||||||
|  |  | ||||||
|  | @ -30,6 +30,38 @@ RSpec.describe UserSettings::Setting do | ||||||
|     it 'returns a type' do |     it 'returns a type' do | ||||||
|       expect(subject.type).to be_a ActiveModel::Type::Value |       expect(subject.type).to be_a ActiveModel::Type::Value | ||||||
|     end |     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 |   end | ||||||
| 
 | 
 | ||||||
|   describe '#type_cast' do |   describe '#type_cast' do | ||||||
|  |  | ||||||
		Loading…
	
	Add table
		Add a link
		
	
		Reference in a new issue