A conversão de campos personalizados foi afetada pela atualização recente?

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?

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.

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:

Minha recomendação geral aqui é que, como autor de um plugin, você sempre adicione índices em uma migração para impor corretamente a restrição. Na verdade, a maioria dos plugins que escrevemos atualmente evita campos personalizados, a menos que sejam absolutamente necessários, e prefere usar tabelas personalizadas, que são muito mais fáceis de analisar.

Neste caso específico, você precisa de um índice assim:

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

Não tenho certeza do que mais precisaríamos fazer no núcleo aqui. Já consideramos uma reformulação dos campos personalizados, mas estamos um pouco preocupados com isso. Uma coisa que estou considerando é simplesmente remover o suporte a arrays dos campos personalizados, pois eles têm causado enormes quantidades de problemas ao longo dos anos.

Desculpe por ressuscitar este tópico, mas uma das condições ocorre quando você usa um índice de símbolo, ou seja, custom_fields[:hello], ao atualizar o valor existente. Nesse caso, ele adiciona outro campo em vez de atualizar, resultando em um array. Na minha opinião, essa pode ser a única condição.

Isso deve corrigir o efeito colateral causado com certeza.