Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Wrong type for user setting when default is defined by lambda #24321

Merged
merged 1 commit into from
Mar 31, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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