rosa-build/app/models/build_script.rb

64 lines
1.5 KiB
Ruby
Raw Normal View History

2014-05-02 21:38:28 +01:00
class BuildScript < ActiveRecord::Base
include FileStoreClean
STATUSES = [
ACTIVE = 'active',
BLOCKED = 'blocked'
2014-05-02 21:38:28 +01:00
]
FORMAT = 'tar.gz'
belongs_to :project
validates :treeish, presence: true
validates :project_id, presence: true, uniqueness: { scope: :treeish }
scope :by_active, -> { where(status: ACTIVE) }
2014-05-02 21:38:28 +01:00
scope :by_treeish, -> treeish { where(treeish: treeish) }
before_validation :attach_project
attr_writer :project_name
attr_accessible :project_name, :treeish, :commit, :sha1, :status
state_machine :status, initial: :active do
event(:disable) { transition active: :blocked }
event(:enable) { transition blocked: :active }
end
def sha1_of_file_store_files
[sha1].select(&:present?)
end
def project_name
2014-05-02 21:51:57 +01:00
@project_name.presence || project.try(:name_with_owner)
2014-05-02 21:38:28 +01:00
end
def can_update_archive?
last_commit != commit
end
def update_archive
old_sha1, new_commit = sha1, last_commit
archive = project.archive_by_treeish_and_format(treeish, FORMAT)
new_sha1 = FileStoreClean.save_file_to_file_store(archive)
if new_sha1.present?
update_attributes(sha1: new_sha1, commit: new_commit)
destroy_files_from_file_store(old_sha1) if old_sha1.present?
end
end
later :update_archive, queue: :middle
protected
def last_commit
project.repo.commits(treeish, 1).first.try(:id)
end
def attach_project
2014-05-02 21:51:57 +01:00
if @project_name.present?
self.project = Project.find_by_owner_and_name(@project_name)
2014-05-02 21:38:28 +01:00
end
end
end