Browse Source

<=> 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
tags/5.1.0
Go MAEDA 11 months ago
parent
commit
945a82b5c0

+ 2
- 0
app/models/custom_field.rb View File

@@ -263,6 +263,8 @@ class CustomField < ActiveRecord::Base
end

def <=>(field)
return nil unless field.is_a?(CustomField)

position <=> field.position
end


+ 2
- 0
app/models/enumeration.rb View File

@@ -91,6 +91,8 @@ class Enumeration < ActiveRecord::Base
end

def <=>(enumeration)
return nil unless enumeration.is_a?(Enumeration)

position <=> enumeration.position
end


+ 3
- 3
app/models/issue.rb 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)

+ 2
- 0
app/models/issue_category.rb View File

@@ -43,6 +43,8 @@ class IssueCategory < ActiveRecord::Base
end

def <=>(category)
return nil unless category.is_a?(IssueCategory)

name <=> category.name
end


+ 2
- 0
app/models/issue_relation.rb 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

+ 2
- 0
app/models/issue_status.rb View File

@@ -82,6 +82,8 @@ class IssueStatus < ActiveRecord::Base
end

def <=>(status)
return nil unless status.is_a?(IssueStatus)

position <=> status.position
end


+ 2
- 0
app/models/member.rb 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

+ 5
- 3
app/models/principal.rb 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

+ 2
- 0
app/models/project.rb 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


+ 2
- 0
app/models/repository.rb 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?

+ 7
- 7
app/models/role.rb 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


+ 2
- 0
app/models/tracker.rb 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


+ 2
- 0
app/models/version.rb 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

+ 2
- 0
lib/redmine/plugin.rb 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


+ 2
- 0
lib/redmine/themes.rb View File

@@ -61,6 +61,8 @@ module Redmine
end

def <=>(theme)
return nil unless theme.is_a?(Theme)

name <=> theme.name
end


+ 6
- 0
test/unit/enumeration_test.rb 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

Loading…
Cancel
Save