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:
@@ -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
|
||||
|
||||
@@ -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)
|
||||
|
||||
Reference in New Issue
Block a user