Skip to content

Avoid 500 when event type length is blank - #3791

Open
officialasishkumar wants to merge 1 commit into
openSUSE:masterfrom
officialasishkumar:bugfix/event-type-length-nil
Open

Avoid 500 when event type length is blank#3791
officialasishkumar wants to merge 1 commit into
openSUSE:masterfrom
officialasishkumar:bugfix/event-type-length-nil

Conversation

@officialasishkumar

Copy link
Copy Markdown

Checklist

  • I have read the Contribution & Best practices Guide.
  • My branch is up-to-date with the upstream `master` branch.
  • The tests pass locally with my changes.
  • I have added tests that prove my fix is effective or that my feature works(if appropriate).
  • I have added necessary documentation (if appropriate).

Short description of what this resolves/which issues does this fix?:

Changes proposed in this pull request:

  • Guard the `EventType#length_step` validator so that the divisor check is only performed once the value is numeric. With a blank length the `numericality` validator now produces the usual "is not a number" error and the form renders normally instead of raising `NoMethodError`.
  • Add a model spec covering the blank length case.

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.
Comment thread app/models/event_type.rb
#
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)

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a validation for Program and possibly a data migration making sure existing data is Numeric.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Error during validation of event type length

2 participants