Skip to content

Preserve false values in traits_for_enum Hash mappings - #1855

Open
afurm wants to merge 1 commit into
thoughtbot:mainfrom
afurm:fix-traits-for-enum-false-values
Open

afurm wants to merge 1 commit into
thoughtbot:mainfrom
afurm:fix-traits-for-enum-false-values

Conversation

@afurm

@afurm afurm commented Oct 16, 2026

Copy link
Copy Markdown

Problem

traits_for_enum builds traits from a Hash with:

build_trait(trait_name, @attribute_name, value || trait_name)

Because false is falsy in Ruby, a mapping like { draft: false, published: true } set the attribute to the trait name (:draft) instead of false.

Array-style values still need the nil fallback so a bare list of names keeps using the name as the attribute value.

Fix

Use an explicit nil? check:

value.nil? ? trait_name : value

Test

Added an acceptance example that uses { draft: false, published: true } and asserts each trait assigns the exact boolean.

traits_for_enum used `value || trait_name` when building traits from a
Hash, so a false value was replaced by the trait name. Use an explicit
nil check so Array-style entries still fall back to the trait name while
false is kept.

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant