Project

General

Profile

Actions

Patch #38772

closed

<=> should return nil when invoked with an incomparable object

Added by Go MAEDA over 1 year ago. Updated over 1 year ago.

Status:
Closed
Priority:
Normal
Assignee:
Category:
Code cleanup/refactoring
Target version:
Start date:
Due date:
% Done:

0%

Estimated time:

Description

`<=>` method of Ruby's standard classes returns nil if it is invoked with an incomparable object. For example, `1 <=> nil` returns nil. But in Redmine, `<=>` methods in most classes raises an exception.

One of the problems that this can cause is the following indirect error messages when comparisons are made between incomparable objects.

irb(main):002:0> [IssuePriority.first, nil].sort
Traceback (most recent call last):
        1: from app/models/enumeration.rb:94:in `<=>'
NoMethodError (undefined method `position' for nil:NilClass)

The attached patch adds a type check and fixes the behavior to return nil.

Before:

irb(main):001:0> IssuePriority.first <=> nil
Traceback (most recent call last):
        1: from app/models/enumeration.rb:94:in `<=>'
NoMethodError (undefined method `position' for nil:NilClass)
irb(main):002:0> [IssuePriority.first, nil].sort
Traceback (most recent call last):
        1: from app/models/enumeration.rb:94:in `<=>'
NoMethodError (undefined method `position' for nil:NilClass)
irb(main):003:0> IssuePriority.first <=> Tracker.last
=> -1

After:

irb(main):001:0> IssuePriority.first <=> nil
=> nil
irb(main):002:0> [IssuePriority.first, nil].sort
(irb):2:in `sort': comparison of IssuePriority with nil failed (ArgumentError)
irb(main):003:0> IssuePriority.first <=> Tracker.last
=> nil


Files


Related issues

Related to Redmine - Defect #32704: ActionView::Template::Error (undefined method `position' for nil:NilClass):Closed

Actions
Actions

Also available in: Atom PDF