require 'active_record'
require 'debci'
require 'debci/package_status'
module Debci
class Package < ::ActiveRecord::Base
has_many :jobs, class_name: 'Debci::Job'
has_many :package_status, class_name: 'Debci::PackageStatus'
validates_format_of :name, with: /\A[a-z0-9][a-z0-9+.-]+\z/
validates :backend, inclusion: { in: ->(_) { Debci.config.backend_list } }, allow_nil: true
scope :by_prefix, lambda { |p|
if p == 'l'
where("name LIKE :prefix AND name NOT LIKE 'lib%'", prefix: "#{p}%")
else
where("name LIKE :prefix", prefix: "#{p}%")
end
}
def status
@status ||=
begin
map = package_status.includes(:job).each_with_object({}) do |st, memo|
memo[st.arch] ||= {}
memo[st.arch][st.suite] = st.job
end
Debci.config.arch_list.map do |arch|
Debci.config.suite_list.map do |suite|
map[arch] && map[arch][suite]
end
end
end
end
def history(suite, architecture)
Debci::Job.history(self, suite, architecture)
end
def news
jobs.newsworthy.order('date DESC').first(10)
end
def fail_or_neutral
status.flatten.compact.select { |p| (p.status.to_sym == :fail) || (p.status.to_sym == :neutral) }
end
def to_s
"<Package #{name}>"
end
def to_str
name
end
def self.prefixes
select(:name).distinct.pluck(:name).map { |n| prefix(n) }.sort.uniq
end
def self.prefix(name)
name =~ /^((lib)?.)/
Regexp.last_match(1)
end
def prefix
self.class.prefix(name)
end
def reject_listed?(params = {})
Debci.reject_list.include?(name, params)
end
def (params = {})
Debci.reject_list.(name, params)
end
def last_updated_at(suite = nil)
statuses = status.flatten.compact.select { |s| s.suite == suite || !suite }
statuses.map(&:date).compact.max
end
end
end