This commit is contained in:
Christoph Haas 2025-04-11 09:39:33 +02:00
parent 7cd9c68a10
commit 36984ff1d4
20 changed files with 321 additions and 391 deletions

View file

@ -1,4 +1,4 @@
require 'open-uri' # allows to load URLs using open()
require 'open-uri' # allows to load URLs using open()
require 'json'
require 'deb_importer'
@ -8,26 +8,26 @@ class Package < ApplicationRecord
include PgSearch::Model
# TODO: Make search weighted on users' rating
pg_search_scope :general_search,
# :against => [:name, :description, :long_description],
:against => [
[:name, 'A'],
[:description, 'B'],
[:long_description, 'C']
],
:using => {
:tsearch => {:dictionary => "english"}
}
# :against => [:name, :description, :long_description],
against: [
[:name, 'A'],
[:description, 'B'],
[:long_description, 'C']
],
using: {
tsearch: { dictionary: 'english' }
}
# I am using "destroy_all" here so that when a package gets destroys the
# callbacks for all dependent screenshots are executed - thus removing the
# screenshot files from disk.
has_many :screenshots,
-> { order(id: :desc) },
:inverse_of=>:package,
:dependent => :destroy
-> { order(id: :desc) },
inverse_of: :package,
dependent: :destroy
# default_scope {
# order('name ASC')
# order('name ASC')
# }
# Define the parameter(s) used for a /package/:name URL to an instance of this model
@ -37,11 +37,7 @@ class Package < ApplicationRecord
# Return the first paragraph of the long description.
def long_description_first_paragraph
if self.long_description
self.long_description.split(/\n\.\n/).first
else
nil
end
long_description.split(/\n\.\n/).first if long_description
end
# Return a query of all packages that have screenshots
@ -72,7 +68,7 @@ class Package < ApplicationRecord
# Get all screenshots and have them sorted descendingly by their version number (Debian style).
def screenshots_sorted_by_version
self.screenshots.approved.to_a.sort { |x,y| version_compare(x.version,y.version) }
screenshots.approved.to_a.sort { |x, y| version_compare(x.version, y.version) }
end
# Return the newest screenshot that is not newer than the given version.
@ -83,29 +79,29 @@ class Package < ApplicationRecord
# This way the user does not see a screenshot of version 2.0 because
# 2.0 might contain features that were not there in version 1.5.
def best_screenshot_for_version(version)
sorted_screenshots = self.screenshots_sorted_by_version
sorted_screenshots = screenshots_sorted_by_version
sorted_screenshots.each do |ss|
logger.debug { "Comparing version #{version} against #{ss.version}" }
return ss if version_compare(version, ss.version)<=0
return ss if version_compare(version, ss.version) <= 0
end
return sorted_screenshots.last
sorted_screenshots.last
end
# Return the part of the version up to the first - or +
def upstream_version
self.version.split(/[\-\+]/).first
version.split(/[-+]/).first
end
private
def version_compare(x,y)
def version_compare(x, y)
x = '0' unless x.present?
y = '0' unless y.present?
version_x = DebImporter::Version.new(x)
version_y = DebImporter::Version.new(y)
if version_x<version_y
if version_x < version_y
1
elsif version_x>version_y
elsif version_x > version_y
-1
else
0