<=> operator should return nil when invoked with an incomparable object (#38772).

Patch by Go MAEDA.


git-svn-id: https://svn.redmine.org/redmine/trunk@22269 e93f8b46-1217-0410-a6f0-8f06a7374b81
This commit is contained in:
Go MAEDA
2023-06-29 14:42:54 +00:00
parent ebf3fb3b4f
commit 945a82b5c0
16 changed files with 45 additions and 13 deletions

View File

@@ -263,6 +263,8 @@ class CustomField < ActiveRecord::Base
end
def <=>(field)
return nil unless field.is_a?(CustomField)
position <=> field.position
end

View File

@@ -91,6 +91,8 @@ class Enumeration < ActiveRecord::Base
end
def <=>(enumeration)
return nil unless enumeration.is_a?(Enumeration)
position <=> enumeration.position
end

View File

@@ -1435,9 +1435,9 @@ class Issue < ActiveRecord::Base
end
def <=>(issue)
if issue.nil?
-1
elsif root_id != issue.root_id
return nil unless issue.is_a?(Issue)
if root_id != issue.root_id
(root_id || 0) <=> (issue.root_id || 0)
else
(lft || 0) <=> (issue.lft || 0)

View File

@@ -43,6 +43,8 @@ class IssueCategory < ActiveRecord::Base
end
def <=>(category)
return nil unless category.is_a?(IssueCategory)
name <=> category.name
end

View File

@@ -198,6 +198,8 @@ class IssueRelation < ActiveRecord::Base
end
def <=>(relation)
return nil unless relation.is_a?(IssueRelation)
r = TYPES[self.relation_type][:order] <=> TYPES[relation.relation_type][:order]
r == 0 ? id <=> relation.id : r
end

View File

@@ -82,6 +82,8 @@ class IssueStatus < ActiveRecord::Base
end
def <=>(status)
return nil unless status.is_a?(IssueStatus)
position <=> status.position
end

View File

@@ -81,6 +81,8 @@ class Member < ActiveRecord::Base
end
def <=>(member)
return nil unless member.is_a?(Member)
a, b = roles.sort, member.roles.sort
if a == b
if principal

View File

@@ -151,9 +151,11 @@ class Principal < ActiveRecord::Base
end
def <=>(principal)
if principal.nil?
-1
elsif self.class.name == principal.class.name
# avoid an error when sorting members without roles (#10053)
return -1 if principal.nil?
return nil unless principal.is_a?(Principal)
if self.class.name == principal.class.name
self.to_s.casecmp(principal.to_s)
else
# groups after users

View File

@@ -671,6 +671,8 @@ class Project < ActiveRecord::Base
end
def <=>(project)
return nil unless project.is_a?(Project)
name.casecmp(project.name)
end

View File

@@ -141,6 +141,8 @@ class Repository < ActiveRecord::Base
end
def <=>(repository)
return nil unless repository.is_a?(Repository)
if is_default?
-1
elsif repository.is_default?

View File

@@ -155,14 +155,14 @@ class Role < ActiveRecord::Base
end
def <=>(role)
if role
if builtin == role.builtin
position <=> role.position
else
builtin <=> role.builtin
end
# returns -1 for nil since r2726
return -1 if role.nil?
return nil unless role.is_a?(Role)
if builtin == role.builtin
position <=> role.position
else
-1
builtin <=> role.builtin
end
end

View File

@@ -93,6 +93,8 @@ class Tracker < ActiveRecord::Base
def to_s; name end
def <=>(tracker)
return nil unless tracker.is_a?(Tracker)
position <=> tracker.position
end

View File

@@ -316,6 +316,8 @@ class Version < ActiveRecord::Base
# Versions are sorted by effective_date and name
# Those with no effective_date are at the end, sorted by name
def <=>(version)
return nil unless version.is_a?(Version)
if self.effective_date
if version.effective_date
if self.effective_date == version.effective_date

View File

@@ -187,6 +187,8 @@ module Redmine
end
def <=>(plugin)
return nil unless plugin.is_a?(Plugin)
self.id.to_s <=> plugin.id.to_s
end

View File

@@ -61,6 +61,8 @@ module Redmine
end
def <=>(theme)
return nil unless theme.is_a?(Theme)
name <=> theme.name
end

View File

@@ -179,4 +179,10 @@ class EnumerationTest < ActiveSupport::TestCase
override.destroy
assert_equal [1, 2, 3], [a, b, c].map(&:reload).map(&:position)
end
def test_spaceship_operator_with_incomparable_value_should_return_nil
e = Enumeration.first
assert_nil e <=> nil
assert_nil e <=> 'foo'
end
end