2012-05-04 18:12:51 +01:00
|
|
|
class Advisory < ActiveRecord::Base
|
|
|
|
has_and_belongs_to_many :platforms
|
2012-06-18 23:49:17 +01:00
|
|
|
has_and_belongs_to_many :projects
|
2012-05-04 18:12:51 +01:00
|
|
|
has_many :build_lists
|
|
|
|
|
|
|
|
validates :description, :update_type, :presence => true
|
|
|
|
|
|
|
|
after_create :generate_advisory_id
|
2012-06-04 20:49:20 +01:00
|
|
|
before_save :normalize_references, :if => :references_changed?
|
2012-05-04 18:12:51 +01:00
|
|
|
|
2012-07-03 23:52:13 +01:00
|
|
|
ID_TEMPLATE = 'ROSA-%<type>s-%<year>d:%<id>04d'
|
|
|
|
ID_STRING_TEMPLATE = 'ROSA-%<type>s-%<year>04s:%<id>04s'
|
2012-05-04 18:12:51 +01:00
|
|
|
TYPES = {'security' => 'SA', 'bugfix' => 'A'}
|
|
|
|
|
2012-06-20 18:51:08 +01:00
|
|
|
scope :search_by_id, lambda { |aid| where('advisory_id ILIKE ?', "%#{aid.to_s.strip}%") }
|
2012-07-03 23:52:13 +01:00
|
|
|
scope :by_update_type, lambda { |ut| where(:update_type => ut) }
|
2012-06-20 15:07:07 +01:00
|
|
|
default_scope order('created_at DESC')
|
2012-06-04 20:49:20 +01:00
|
|
|
|
2012-05-04 18:12:51 +01:00
|
|
|
def to_param
|
|
|
|
advisory_id
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def generate_advisory_id
|
|
|
|
self.advisory_id = sprintf(ID_TEMPLATE, :type => TYPES[self.update_type], :year => Time.now.utc.year, :id => self.id)
|
|
|
|
self.save
|
|
|
|
end
|
2012-06-04 20:49:20 +01:00
|
|
|
|
|
|
|
def normalize_references
|
|
|
|
self.references.gsub!(/\r| /, '')
|
|
|
|
self.references = self.references.split('\n').map do |ref|
|
|
|
|
ref = CGI::escapeHTML(ref)
|
|
|
|
ref = "http://#{ref}" unless ref =~ %r[^http(s?)://*]
|
|
|
|
ref
|
|
|
|
end.join("\n")
|
|
|
|
end
|
|
|
|
|
2012-05-04 18:12:51 +01:00
|
|
|
end
|
2012-06-04 20:49:20 +01:00
|
|
|
Advisory.include_root_in_json = false
|