phlex icon indicating copy to clipboard operation
phlex copied to clipboard

Helper mix() raises error when a key ends with ! bang

Open darrenterhune opened this issue 2 months ago • 1 comments

I was reading the mix() method and noticed something strange. The ! bang in a key. I'm not sure if that was left over from previous versions, as I seem to remember some reason for having a ! in the key. Regardless it caught my eye because I didn't think .name was a valid method on String in either Ruby or Rails so I did a quick check:

helper.mix({ "a!" => 1, { "b" => 2 })
NoMethodError: undefined method `name' for an instance of String (NoMethodError)
key.end_with?('!') ? key.name.chop.to_sym : key

Though newer ruby non-stabby hash syntax passes since it's a symbol:

helper.mix({ "a!": 1, { "b": 2 })

Thought maybe it should be fixed as the code is misleading to read. Let me know if you'd like me to submit a PR for this, happy to help.

darrenterhune avatar Dec 03 '25 15:12 darrenterhune

I think it’s making an assumption that if there’s a bang at the end of the key then the key must be a symbol, which is not a correct assumption.

If you use a symbol with a bang, it’s considered an instruction to override instead of merge.

So this

mix(class: "a", { class: "b" })

Will produce

{ class: "a b" }

But this

mix(class: "a", { class!: "b" })

Will produce

{ class: "b" }

joeldrapper avatar Dec 03 '25 16:12 joeldrapper