DEV: uses `in: {}` with lambda to work with `eager_load` (#25039)

When validating with a dynamic set of values, especially one that might change during runtime, we should use a lambda or a proc to ensure that the validation uses the most up-to-date set of values. This is particularly important when using config.eager_load = true, which can cause some elements to be loaded only once at startup, thus not reflecting changes made at runtime.

This was the root cause of the issues here, as we were adding more ReviewableScore types after initial load through: `register_reviewable_type Chat::ReviewableMessage`
This commit is contained in:
Joffrey JAFFEUX 2023-12-29 05:45:07 +01:00 committed by GitHub
parent 6e8c2bb4ab
commit 40ce619edd
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 1 additions and 2 deletions

View File

@ -40,7 +40,7 @@ module Chat
validates :channel_id, presence: true validates :channel_id, presence: true
attribute :flag_type_id, :integer attribute :flag_type_id, :integer
validates :flag_type_id, inclusion: { in: ::ReviewableScore.types.values } validates :flag_type_id, inclusion: { in: ->(_flag_type) { ::ReviewableScore.types.values } }
attribute :message, :string attribute :message, :string
attribute :is_warning, :boolean attribute :is_warning, :boolean

View File

@ -8,7 +8,6 @@ RSpec.describe Chat::FlagMessage do
it { is_expected.to validate_presence_of(:message_id) } it { is_expected.to validate_presence_of(:message_id) }
it do it do
skip("TODO: This does not work in CI when eager load is set to true") if ENV["CI"]
is_expected.to validate_inclusion_of(:flag_type_id).in_array(ReviewableScore.types.values) is_expected.to validate_inclusion_of(:flag_type_id).in_array(ReviewableScore.types.values)
end end
end end