2012-11-06 14:13:16 +00:00
|
|
|
# -*- encoding : utf-8 -*-
|
|
|
|
module Modules
|
|
|
|
module Models
|
|
|
|
module CommitAndVersion
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
2012-12-03 17:43:24 +00:00
|
|
|
|
2012-11-06 14:13:16 +00:00
|
|
|
validate lambda {
|
2012-11-06 17:17:44 +00:00
|
|
|
if project && (commit_hash.blank? || project.repo.commit(commit_hash).blank?)
|
2012-11-06 14:13:16 +00:00
|
|
|
errors.add :commit_hash, I18n.t('flash.build_list.wrong_commit_hash', :commit_hash => commit_hash)
|
|
|
|
end
|
|
|
|
}
|
|
|
|
|
|
|
|
before_validation :set_commit_and_version
|
2012-12-03 17:43:24 +00:00
|
|
|
before_create :set_last_published_commit
|
2012-11-06 14:13:16 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def set_commit_and_version
|
2012-11-06 18:01:54 +00:00
|
|
|
if project && project_version.present? && commit_hash.blank?
|
2013-01-21 17:08:19 +00:00
|
|
|
self.commit_hash = project.repo.commits(project_version).try(:first).try(:id)
|
2012-11-06 14:13:16 +00:00
|
|
|
elsif project_version.blank? && commit_hash.present?
|
|
|
|
self.project_version = commit_hash
|
|
|
|
end
|
|
|
|
end
|
2012-12-03 17:43:24 +00:00
|
|
|
|
|
|
|
def set_last_published_commit
|
2012-12-04 09:59:16 +00:00
|
|
|
return unless self.respond_to? :last_published_commit_hash # product?
|
|
|
|
last_commit = self.last_published.first.try :commit_hash
|
|
|
|
if last_commit && self.project.repo.commit(last_commit).present? # commit(nil) is not nil!
|
|
|
|
self.last_published_commit_hash = last_commit
|
2012-12-04 03:07:18 +00:00
|
|
|
end
|
2012-12-03 17:43:24 +00:00
|
|
|
end
|
2012-11-06 14:13:16 +00:00
|
|
|
end
|
|
|
|
end
|
2012-12-03 17:43:24 +00:00
|
|
|
end
|