enumerize icon indicating copy to clipboard operation
enumerize copied to clipboard

Incompatible with ActiveRecord `normalizes`

Open Timmitry opened this issue 1 year ago • 2 comments

I tried to use enumerize (love it!) together with the relatively new normalizes-Method from ActiveRecord. This does not work - the normalization does not happen. Pseudo example:

class MyModel < ApplicationRecord
    normalizes :locale, with: ->(value) { value.downcase.strip.presence }
    enumerize :locale, in: %w[de en pl]
end

MyModel.new(locale: " DE ").valid? # => false

Without enumerize, the " DE " would be converted to de, and thus be valid. With enumerize, this is skipped, and enumerize reports it as invalid.

My guess is that this is due to enumerize overwriting the getter and setter for the attribute here.

Rails, on the other hand, decorates the existing attribute here (not sure yet what exactly happens there).

I could try to create a minimal reproduction, but first wanted to ask if this would be out of scope anyway.

Timmitry avatar Mar 12 '25 09:03 Timmitry

@Timmitry hey! It looks like something I'd like to be fixed! So a failing test or even a fix would be much appreciated.

nashby avatar Mar 12 '25 09:03 nashby

@nashby Thanks for looking into this! I added two failing test cases in #460. Commenting out enumerize :locale, ... will make the tests green.

Timmitry avatar Mar 12 '25 12:03 Timmitry