Order expired polls by ends date

This commit is contained in:
decabeza
2023-07-07 19:48:23 +02:00
committed by taitus
parent e0aee199e4
commit bfb02093a6
3 changed files with 69 additions and 31 deletions

View File

@@ -57,8 +57,25 @@ class Poll < ApplicationRecord
def self.sort_for_list(user = nil)
all.sort do |poll, another_poll|
[poll.weight(user), poll.starts_at, poll.name] <=>
[another_poll.weight(user), another_poll.starts_at, another_poll.name]
compare_polls(poll, another_poll, user)
end
end
def self.compare_polls(poll, another_poll, user)
weight_comparison = poll.weight(user) <=> another_poll.weight(user)
return weight_comparison unless weight_comparison.zero?
time_comparison = compare_times(poll, another_poll)
return time_comparison unless time_comparison.zero?
poll.name <=> another_poll.name
end
def self.compare_times(poll, another_poll)
if poll.expired? && another_poll.expired?
another_poll.ends_at <=> poll.ends_at
else
poll.starts_at <=> another_poll.starts_at
end
end