From 11e7c277599cda19f3ecb55de88910b8f06bfa89 Mon Sep 17 00:00:00 2001 From: Asish Kumar Date: Thu, 14 May 2026 09:18:39 +0530 Subject: [PATCH] Avoid crash validating event type with blank length The `length_step` validator runs unconditionally for every record and calls `length % program.schedule_interval`. When the length field is left blank in the admin form, `length` is nil and `length %` raises NoMethodError, returning a 500 Internal Server Error instead of a regular validation failure. Skip the divisor check unless `length` is numeric so that the `numericality` validator on `:length` can produce a proper error message, restoring the usual validation feedback. --- app/models/event_type.rb | 6 +++++- spec/models/event_type_spec.rb | 7 +++++++ 2 files changed, 12 insertions(+), 1 deletion(-) diff --git a/app/models/event_type.rb b/app/models/event_type.rb index 691744c4..559be7cb 100644 --- a/app/models/event_type.rb +++ b/app/models/event_type.rb @@ -22,9 +22,13 @@ class EventType < ApplicationRecord ## # Check if length is a divisor of program schedule cell size. Used as validation. + # Skipped when length is missing or non-numeric so that the numericality + # validator can produce a proper error instead of crashing. # def length_step - errors.add(:length, "must be a divisor of #{program.schedule_interval}") if program && length % program.schedule_interval != 0 + return unless program && length.is_a?(Numeric) + + errors.add(:length, "must be a divisor of #{program.schedule_interval}") if length % program.schedule_interval != 0 end def capitalize_color diff --git a/spec/models/event_type_spec.rb b/spec/models/event_type_spec.rb index 2b696ead..d39ba05d 100644 --- a/spec/models/event_type_spec.rb +++ b/spec/models/event_type_spec.rb @@ -44,6 +44,13 @@ describe EventType do it 'is not valid when length is not multiple of LENGTH_STEP' do expect(build(:event_type, program: conference.program, length: 37)).not_to be_valid end + + it 'is not valid when length is blank and does not raise during validation' do + event_type = build(:event_type, program: conference.program, length: nil) + expect { event_type.valid? }.not_to raise_error + expect(event_type).not_to be_valid + expect(event_type.errors[:length]).to be_present + end end end end