Project

General

Profile

Actions

Patch #38772

closed

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

Added by Go MAEDA 10 months ago. Updated 10 months 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 #1

Updated by Go MAEDA 10 months ago

  • Related to Defect #32704: ActionView::Template::Error (undefined method `position' for nil:NilClass): added
Actions #2

Updated by Holger Just 10 months ago

LGTM!

Actions #3

Updated by Go MAEDA 10 months ago

  • Description updated (diff)
  • Target version set to 5.1.0
Actions #4

Updated by Go MAEDA 10 months ago

  • Status changed from New to Closed
  • Assignee set to Go MAEDA

Committed the patch.

Actions

Also available in: Atom PDF