Allow target and local target comparison

This way we'll be able to sort a mixed collection of targets and local
targets.
This commit is contained in:
Javi Martín
2021-01-20 19:15:43 +01:00
parent e96f45ba39
commit d3f72b100e
4 changed files with 46 additions and 8 deletions

View File

@@ -17,10 +17,12 @@ class SDG::LocalTarget < ApplicationRecord
belongs_to :target
def <=>(local_target)
return unless local_target.class == self.class
[target, numeric_subcode] <=> [local_target.target, local_target.numeric_subcode]
def <=>(any_target)
if any_target.class == self.class
[target, numeric_subcode] <=> [any_target.target, any_target.numeric_subcode]
elsif any_target.class == target.class
-1 * (any_target <=> self)
end
end
protected

View File

@@ -12,10 +12,12 @@ class SDG::Target < ApplicationRecord
I18n.t("sdg.goals.goal_#{goal.code}.targets.target_#{code_key}.title")
end
def <=>(target)
return unless target.class == self.class
[goal.code, numeric_subcode] <=> [target.goal.code, target.numeric_subcode]
def <=>(any_target)
if any_target.class == self.class
[goal.code, numeric_subcode] <=> [any_target.goal.code, any_target.numeric_subcode]
elsif any_target.class.name == "SDG::LocalTarget"
[self, -1] <=> [any_target.target, 1]
end
end
def self.[](code)