From 2bfa58aa1a699380a71f4041e6d6afd070eb8926 Mon Sep 17 00:00:00 2001 From: Matt Jankowski Date: Thu, 14 Nov 2024 10:57:25 -0500 Subject: [PATCH] Add `by_version` method to `SoftwareUpdate` model --- app/controllers/admin/software_updates_controller.rb | 2 +- app/mailers/admin_mailer.rb | 4 ++-- app/models/software_update.rb | 4 ++++ spec/models/software_update_spec.rb | 10 ++++++++++ 4 files changed, 17 insertions(+), 3 deletions(-) diff --git a/app/controllers/admin/software_updates_controller.rb b/app/controllers/admin/software_updates_controller.rb index 52d8cb41e6..d7b114a100 100644 --- a/app/controllers/admin/software_updates_controller.rb +++ b/app/controllers/admin/software_updates_controller.rb @@ -6,7 +6,7 @@ module Admin def index authorize :software_update, :index? - @software_updates = SoftwareUpdate.all.sort_by(&:gem_version) + @software_updates = SoftwareUpdate.by_version end private diff --git a/app/mailers/admin_mailer.rb b/app/mailers/admin_mailer.rb index d6523d9f4b..cc2a537b3c 100644 --- a/app/mailers/admin_mailer.rb +++ b/app/mailers/admin_mailer.rb @@ -48,7 +48,7 @@ class AdminMailer < ApplicationMailer end def new_software_updates - @software_updates = SoftwareUpdate.all.to_a.sort_by(&:gem_version) + @software_updates = SoftwareUpdate.by_version locale_for_account(@me) do mail subject: default_i18n_subject(instance: @instance) @@ -56,7 +56,7 @@ class AdminMailer < ApplicationMailer end def new_critical_software_updates - @software_updates = SoftwareUpdate.urgent.to_a.sort_by(&:gem_version) + @software_updates = SoftwareUpdate.urgent.by_version locale_for_account(@me) do mail subject: default_i18n_subject(instance: @instance) diff --git a/app/models/software_update.rb b/app/models/software_update.rb index 9746772a6a..4c868f6f59 100644 --- a/app/models/software_update.rb +++ b/app/models/software_update.rb @@ -37,6 +37,10 @@ class SoftwareUpdate < ApplicationRecord Rails.configuration.x.mastodon.software_update_url.present? end + def by_version + all.sort_by(&:gem_version) + end + def pending_to_a return [] unless check_enabled? diff --git a/spec/models/software_update_spec.rb b/spec/models/software_update_spec.rb index fc3f29be75..8c698e51b3 100644 --- a/spec/models/software_update_spec.rb +++ b/spec/models/software_update_spec.rb @@ -16,6 +16,16 @@ RSpec.describe SoftwareUpdate do end end + describe '.by_version' do + let!(:latest_update) { Fabricate :software_update, version: '4.0.0' } + let!(:older_update) { Fabricate :software_update, version: '3.0.0' } + + it 'returns record in gem version order' do + expect(described_class.by_version) + .to eq([older_update, latest_update]) + end + end + describe '#pending?' do subject { described_class.new(version: update_version) }