Skip to content

Commit

Permalink
Wrong type for user setting when default is defined by lambda (mastod…
Browse files Browse the repository at this point in the history
  • Loading branch information
c960657 authored and arachnist committed Apr 4, 2023
1 parent 79e4d3e commit 061553d
Show file tree
Hide file tree
Showing 2 changed files with 34 additions and 1 deletion.
3 changes: 2 additions & 1 deletion app/models/user_settings/setting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@ def default_value
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
Expand Down
32 changes: 32 additions & 0 deletions spec/models/user_settings/setting_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,38 @@
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
Expand Down

0 comments on commit 061553d

Please sign in to comment.