From 7f624172941c1e410545c25894b480294ebab3b9 Mon Sep 17 00:00:00 2001 From: Emi Matchu Date: Sun, 29 Dec 2024 16:11:00 -0800 Subject: [PATCH] Fix bug sorting pet styles by creation date MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Before this change, the sort order when searching for "Prismatic Pine: Nostalgic" showed: - [Added Dec 18, 2024] Prismatic Pine: Nostalgic Christmas Flotsam - [Added Dec 19, 2024] Prismatic Pine: Nostalgic Christmas Gelert - [Added Dec 18, 2024] Prismatic Pine: Nostalgic Christmas Bruce - [Added Dec 17, 2024] Prismatic Pine: Nostalgic Christmas Scorchio - This is because the Gelert was created at 11:37 NST on Dec 19, whereas the Flotsam was created at 18:11 NST on Dec 18—but in UTC, which is how timestamps are stored in the database, these are both Dec 19, so the Flotsam was sorting first alphabetically. In this change, we do a hacky transform from UTC to NST-ish. I didn't want to set up the deploy process to pull named time zones into MySQL, and then have this as a potential gotcha for the dev environment later—so instead, I pretend `-08:00` is a good-enough specification of NST. This will sometimes create slightly incorrect sort ordering when it *is* Daylight Savings, and a record was created around midnight. I'm okay with that! --- app/models/alt_style.rb | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/models/alt_style.rb b/app/models/alt_style.rb index 583ea0ac..7ca0d921 100644 --- a/app/models/alt_style.rb +++ b/app/models/alt_style.rb @@ -20,7 +20,11 @@ class AltStyle < ApplicationRecord where(series_name:, color_id: color.id, species_id: species.id) } scope :by_creation_date, -> { - order("DATE(created_at) DESC") + # HACK: Setting up named time zones in MySQL takes effort, so we assume + # it's not Daylight Savings. This will produce slightly incorrect + # sorting when it *is* Daylight Savings, and records happen to be + # created around midnight. + order(Arel.sql("DATE(CONVERT_TZ(created_at, '+00:00', '-08:00')) DESC")) } scope :unlabeled, -> { where(series_name: nil) } scope :newest, -> { order(created_at: :desc) }