2020-04-09 18:33:04 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class RepositorySnapshot < RepositoryBase
|
2020-04-28 18:03:23 +08:00
|
|
|
enum status: { provisioning: 0, ready: 1, failed: 2 }
|
|
|
|
|
2020-05-13 18:58:31 +08:00
|
|
|
belongs_to :original_repository, foreign_key: :parent_id,
|
|
|
|
class_name: 'Repository',
|
|
|
|
inverse_of: :repository_snapshots,
|
|
|
|
optional: true
|
2020-04-09 18:33:04 +08:00
|
|
|
belongs_to :my_module, optional: true
|
|
|
|
|
|
|
|
validates :name, presence: true, length: { maximum: Constants::NAME_MAX_LENGTH }
|
2020-04-28 18:03:23 +08:00
|
|
|
validates :status, presence: true
|
2020-05-21 06:36:24 +08:00
|
|
|
validate :only_one_selected_for_my_module, if: ->(obj) { obj.changed.include? :selected }
|
2020-04-21 20:49:36 +08:00
|
|
|
|
|
|
|
def default_columns_count
|
|
|
|
Constants::REPOSITORY_SNAPSHOT_TABLE_DEFAULT_STATE['length']
|
|
|
|
end
|
2020-05-12 22:26:44 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def only_one_selected_for_my_module
|
|
|
|
return unless selected
|
|
|
|
|
|
|
|
if my_module.repository_snapshots.where(original_repository: original_repository, selected: true).any?
|
|
|
|
errors.add(:selected, I18n.t('activerecord.errors.models.repository_snapshot.attributes.selected.already_taken'))
|
|
|
|
end
|
|
|
|
end
|
2020-04-09 18:33:04 +08:00
|
|
|
end
|