diff --git a/app/controllers/versions_controller.rb b/app/controllers/versions_controller.rb index 057d3f7ebf6..597afd6e51c 100644 --- a/app/controllers/versions_controller.rb +++ b/app/controllers/versions_controller.rb @@ -3,7 +3,7 @@ class VersionsController < ApplicationController def index set_page - set_oldest_version_date + @oldest_version_date = @rubygem.versions.oldest_authored_at @versions = @rubygem.versions.by_position.page(@page).per(Gemcutter::VERSIONS_PER_PAGE) end @@ -15,12 +15,4 @@ def show @on_version_page = true render "rubygems/show" end - - private - - def set_oldest_version_date - oldest_created_at = @rubygem.versions.order(:created_at).first - oldest_built_at = @rubygem.versions.order(:built_at).first - @oldest_version_date = [oldest_created_at, oldest_built_at].compact.map(&:authored_at).min - end end diff --git a/app/models/version.rb b/app/models/version.rb index 32e61390acc..67f35f878ee 100644 --- a/app/models/version.rb +++ b/app/models/version.rb @@ -53,6 +53,19 @@ class Version < ApplicationRecord # rubocop:disable Metrics/ClassLength validate :metadata_attribute_length validate :no_dashes_in_version_number, on: :create + scope :oldest_authored_at, lambda { + minimum( + <<~SQL.squish + CASE WHEN DATE(created_at) = '#{RUBYGEMS_IMPORT_DATE}' + AND built_at <= created_at THEN + built_at + ELSE + created_at + END + SQL + )&.in_time_zone + } + class AuthorType < ActiveModel::Type::String def cast_value(value) if value.is_a?(Array) diff --git a/test/models/version_test.rb b/test/models/version_test.rb index 11bad71a8bb..3c06ba2b2df 100644 --- a/test/models/version_test.rb +++ b/test/models/version_test.rb @@ -242,6 +242,25 @@ class VersionTest < ActiveSupport::TestCase end end + context ".oldest_authored_at scope" do + setup do + @built_at = Version::RUBYGEMS_IMPORT_DATE - 60.days + @version.update(built_at: @built_at, created_at: Version::RUBYGEMS_IMPORT_DATE) + newer_version = create(:version, rubygem: @version.rubygem) + end + + should "return the built_at of the oldest version when created_at is the same as RUBYGEMS_IMPORT_DATE" do + assert_equal @built_at, Version.oldest_authored_at + end + + should "return the created_at of the oldest version when created_at is not the same as RUBYGEMS_IMPORT_DATE" do + created_at = Version::RUBYGEMS_IMPORT_DATE + 1.day + @version.update(created_at: created_at) + + assert_equal created_at, Version.oldest_authored_at + end + end + should "have a rubygems version" do @version.update(required_rubygems_version: @required_rubygems_version) new_version = Version.find(@version.id)