2012-05-14 20:08:31 +01:00
|
|
|
class BuildList::Package < ActiveRecord::Base
|
|
|
|
PACKAGE_TYPES = %w(source binary)
|
|
|
|
|
2014-06-06 23:07:54 +01:00
|
|
|
belongs_to :build_list, touch: true
|
2012-05-14 20:08:31 +01:00
|
|
|
belongs_to :project
|
|
|
|
belongs_to :platform
|
|
|
|
|
2014-06-06 23:07:54 +01:00
|
|
|
serialize :dependent_packages, Array
|
|
|
|
|
|
|
|
attr_accessible :fullname, :name, :release, :version, :sha1, :epoch, :dependent_packages
|
2012-05-14 20:08:31 +01:00
|
|
|
|
2012-08-24 16:19:26 +01:00
|
|
|
validates :build_list_id, :project_id, :platform_id, :fullname,
|
|
|
|
:package_type, :name, :release, :version,
|
2014-01-21 04:51:49 +00:00
|
|
|
presence: true
|
|
|
|
validates :package_type, inclusion: PACKAGE_TYPES
|
|
|
|
validates :sha1, presence: true, if: Proc.new { |p| p.build_list.new_core? }
|
2012-05-29 13:40:25 +01:00
|
|
|
|
2014-03-12 13:20:32 +00:00
|
|
|
default_scope { order("lower(#{table_name}.name) ASC, length(#{table_name}.name) ASC") }
|
2012-11-28 06:04:40 +00:00
|
|
|
|
2012-08-24 16:19:26 +01:00
|
|
|
# Fetches only actual (last publised) packages.
|
2014-03-11 07:39:25 +00:00
|
|
|
scope :actual, -> { where(actual: true) }
|
|
|
|
scope :by_platform, ->(platform) { where(platform_id: platform) }
|
|
|
|
scope :by_name, ->(name) { where(name: name) }
|
|
|
|
scope :by_package_type, ->(type) { where(package_type: type) }
|
|
|
|
scope :like_name, ->(name) { where("#{table_name}.name ILIKE ?", "%#{name}%") if name.present? }
|
2012-05-29 13:40:25 +01:00
|
|
|
|
2013-09-12 15:00:41 +01:00
|
|
|
before_create :set_epoch
|
2014-06-06 23:07:54 +01:00
|
|
|
before_create :normalize_dependent_packages
|
2013-09-12 15:00:41 +01:00
|
|
|
|
2012-05-29 13:40:25 +01:00
|
|
|
def assignee
|
2012-08-24 16:19:26 +01:00
|
|
|
project.maintainer
|
|
|
|
end
|
2013-09-11 16:16:18 +01:00
|
|
|
|
2013-09-11 16:35:43 +01:00
|
|
|
# Comparison between versions
|
|
|
|
# @param [BuildList::Package] other
|
|
|
|
# @return [Number] -1 if +other+ is greater than, 0 if +other+ is equal to,
|
|
|
|
# and +1 if other is less than version.
|
2013-09-11 16:16:18 +01:00
|
|
|
def rpmvercmp(other)
|
|
|
|
RPM::C.rpmvercmp to_vre_epoch_zero, other.to_vre_epoch_zero
|
|
|
|
end
|
|
|
|
|
2014-01-20 21:24:22 +00:00
|
|
|
def self.by_repository(repository, &block)
|
|
|
|
# find_each will batch the results instead of getting all in one go
|
|
|
|
actual.where(
|
|
|
|
build_lists: {save_to_repository_id: repository}
|
|
|
|
).joins(build_list: :save_to_repository).includes(project: :maintainer).find_each do |package|
|
|
|
|
yield package
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-09-11 16:16:18 +01:00
|
|
|
protected
|
|
|
|
|
2014-06-06 23:07:54 +01:00
|
|
|
def normalize_dependent_packages
|
2014-06-09 21:06:21 +01:00
|
|
|
self.dependent_packages = dependent_packages.to_s.split(/\s/).select(&:present?)
|
2014-06-06 23:07:54 +01:00
|
|
|
end
|
|
|
|
|
2013-09-12 15:00:41 +01:00
|
|
|
def set_epoch
|
|
|
|
self.epoch = nil if epoch.blank? || epoch == 0
|
|
|
|
end
|
|
|
|
|
2013-09-11 16:16:18 +01:00
|
|
|
# String representation in the form "e:v-r"
|
|
|
|
# @return [String]
|
|
|
|
# @note The epoch is included always. As 0 if not present
|
|
|
|
def to_vre_epoch_zero
|
2013-09-12 15:00:41 +01:00
|
|
|
evr = epoch.present? ? "#{epoch}:#{version}" : "0:#{version}"
|
2013-09-11 16:16:18 +01:00
|
|
|
evr << "-#{release}" if release.present?
|
|
|
|
evr
|
|
|
|
end
|
|
|
|
|
2012-05-14 20:08:31 +01:00
|
|
|
end
|