¿El casting de campos personalizados se vio afectado por la actualización reciente?

There have been multiple bug reports for the events plugin in the last two days that all seem to be type-casting issues with custom fields. The events plugin hasn’t been substantively updated in a few months.

The plugin.rb file casts event_start as an integer:

Topic.register_custom_field_type('event_start', :integer)

The error is being thrown here:

def has_event?
  self.custom_fields['event_start']&.nonzero?
end

The error itself follows this format:

NoMethodError (undefined method `nonzero?' for [1563127206, 1563127206]:Array)

As far as I understand register_custom_field_type it should ensure that the custom_field always returns as the defined type (perhaps I’m misunderstanding it).

Looking at the has_custom_fields.rb concern, there have been a few changes in the past week that could have affected this, in particular

@eviltrout Any thoughts on this?

6 Me gusta

In case this is indeed the bug causing problems, here I explain the consequences (it may make your site unusable):

This was a known bug with custom fields.

In very specific conditions, it would save the value multiple times, thus creating an Array, when you only want an Integer.

You can fix this by making sure there’s only 1 row per custom field in the database.

5 Me gusta

Yes, it’s cropped up a few times in the past. This latest rash seems to coincide with recent work on the has_custom_fields.rb concern, so there may be something to review there.

This doesn’t fix it, but helps to address it if it arises:

Mi recomendación general aquí es que, como autor de un plugin, siempre debes agregar índices en una migración para hacer cumplir correctamente la restricción. De hecho, la mayoría de los plugins que escribimos hoy en día evitan los campos personalizados a menos que sean absolutamente necesarios y prefieren usar tablas personalizadas, que son mucho más fáciles de entender.

En este caso específico, necesitas un índice de:

create unique index idxStartEvent on topic_custom_fields(topic_id) where name = 'start_event'

No estoy seguro de qué más necesitamos hacer en el núcleo aquí. Hemos considerado una revisión de los campos personalizados, pero estamos algo preocupados al respecto. Una cosa que estoy considerando es simplemente eliminar la compatibilidad con arrays de los campos personalizados, ya que han estado causando una enorme cantidad de problemas a lo largo de los años.

3 Me gusta

Perdón por reactivar este tema, pero una de las condiciones ocurre cuando se utiliza un índice de símbolo, es decir, custom_fields[:hello], al actualizar un valor existente; en lugar de actualizarlo, se añadía otro campo, lo que resultaba en un array. Esta podría ser la única condición, en mi opinión.

Esto debería corregir el efecto secundario causado, casi con total seguridad.

2 Me gusta