2011-10-13 16:55:03 +01:00
|
|
|
class ProjectToRepository < ActiveRecord::Base
|
2014-02-17 21:02:48 +00:00
|
|
|
AUTOSTART_OPTIONS = %w(auto_publish user_id enabled)
|
|
|
|
|
2011-10-13 16:55:03 +01:00
|
|
|
belongs_to :project
|
|
|
|
belongs_to :repository
|
2011-10-19 14:14:53 +01:00
|
|
|
|
2014-01-21 04:51:49 +00:00
|
|
|
delegate :path, to: :project
|
2011-12-23 17:06:47 +00:00
|
|
|
|
2014-03-11 07:39:25 +00:00
|
|
|
scope :autostart_enabled, -> { where("autostart_options -> 'enabled' = 'true'") }
|
2014-02-17 21:02:48 +00:00
|
|
|
|
2014-06-18 21:40:22 +01:00
|
|
|
after_destroy :destroy_project_from_repository, unless: -> { Thread.current[:skip] }
|
2014-01-21 04:51:49 +00:00
|
|
|
|
|
|
|
validate :one_project_in_platform_repositories, on: :create
|
2011-12-23 17:06:47 +00:00
|
|
|
|
2014-02-17 21:02:48 +00:00
|
|
|
AUTOSTART_OPTIONS.each do |field|
|
|
|
|
store_accessor :autostart_options, field
|
|
|
|
end
|
|
|
|
|
2014-02-18 19:16:23 +00:00
|
|
|
def enabled?
|
|
|
|
['true', true].include?(enabled)
|
|
|
|
end
|
|
|
|
|
|
|
|
def auto_publish?
|
|
|
|
['true', true].include?(auto_publish)
|
|
|
|
end
|
|
|
|
|
2011-12-23 17:06:47 +00:00
|
|
|
protected
|
|
|
|
|
2014-06-18 21:40:22 +01:00
|
|
|
def destroy_project_from_repository
|
2016-05-28 19:21:02 +01:00
|
|
|
DestroyProjectFromRepositoryJob.perform_async(project_id, repository_id)
|
2014-06-18 21:40:22 +01:00
|
|
|
end
|
|
|
|
|
2011-12-23 17:06:47 +00:00
|
|
|
def one_project_in_platform_repositories
|
2016-02-17 07:30:44 +00:00
|
|
|
if Project.joins(repositories: :platform).where('platforms.id = ?', repository.platform_id).where(:name => project.name).exists?
|
2014-03-11 07:39:25 +00:00
|
|
|
errors.add(:base, I18n.t('activerecord.errors.project_to_repository.project'))
|
|
|
|
end
|
2011-12-23 17:06:47 +00:00
|
|
|
end
|
2011-10-13 16:55:03 +01:00
|
|
|
end
|