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-25 18:47:58 +08:00
|
|
|
after_save :refresh_report_references, if: :saved_change_to_selected
|
2020-05-27 18:15:19 +08:00
|
|
|
before_destroy :refresh_report_references_on_destroy, prepend: true
|
2020-04-28 18:03:23 +08:00
|
|
|
|
2020-06-22 20:57:35 +08:00
|
|
|
belongs_to :original_repository,
|
2020-06-16 20:28:18 +08:00
|
|
|
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
|
2022-01-26 00:48:45 +08:00
|
|
|
has_one :repository_stock_consumption_column,
|
|
|
|
-> { where(data_type: 'RepositoryStockConsumptionValue') },
|
|
|
|
class_name: 'RepositoryColumn',
|
|
|
|
foreign_key: :repository_id,
|
|
|
|
inverse_of: :repository_snapshot,
|
|
|
|
dependent: :destroy
|
2020-04-09 18:33:04 +08:00
|
|
|
|
|
|
|
validates :name, presence: true, length: { maximum: Constants::NAME_MAX_LENGTH }
|
2020-04-28 18:03:23 +08:00
|
|
|
validates :status, presence: true
|
2020-06-22 13:30:49 +08:00
|
|
|
validate :only_one_selected_for_my_module, if: ->(obj) { obj.changed.include? 'selected' }
|
2020-04-21 20:49:36 +08:00
|
|
|
|
2020-07-09 20:50:01 +08:00
|
|
|
scope :of_unassigned_from_project, lambda { |project|
|
2020-06-18 05:21:56 +08:00
|
|
|
joins(my_module: { experiment: :project })
|
2020-07-09 20:50:01 +08:00
|
|
|
.where(my_modules: { experiments: { project: project } })
|
2020-06-18 05:21:56 +08:00
|
|
|
.left_outer_joins(:original_repository)
|
2020-07-09 20:50:01 +08:00
|
|
|
.where.not(original_repository: Repository.assigned_to_project(project))
|
2020-06-18 05:21:56 +08:00
|
|
|
.select('DISTINCT ON ("repositories"."parent_id") "repositories".*')
|
|
|
|
.order(:parent_id, updated_at: :desc)
|
2020-06-29 17:43:23 +08:00
|
|
|
}
|
2020-06-18 05:21:56 +08:00
|
|
|
|
2021-06-08 20:21:34 +08:00
|
|
|
scope :assigned_to_project, lambda { |project|
|
|
|
|
where(team: project.team)
|
2021-06-16 22:17:12 +08:00
|
|
|
.joins(my_module: { experiment: :project })
|
|
|
|
.where(my_module: { experiments: { project: project } })
|
2021-06-08 20:21:34 +08:00
|
|
|
}
|
|
|
|
|
2020-09-11 22:08:03 +08:00
|
|
|
def self.create_preliminary(repository, my_module, created_by = nil)
|
|
|
|
created_by ||= repository.created_by
|
|
|
|
repository_snapshot = repository.dup.becomes(RepositorySnapshot)
|
|
|
|
repository_snapshot.assign_attributes(type: RepositorySnapshot.name,
|
|
|
|
original_repository: repository,
|
|
|
|
my_module: my_module,
|
|
|
|
created_by: created_by,
|
|
|
|
team: my_module.experiment.project.team,
|
|
|
|
permission_level: Extends::SHARED_INVENTORIES_PERMISSION_LEVELS[:not_shared])
|
|
|
|
repository_snapshot.provisioning!
|
|
|
|
repository_snapshot.reload
|
|
|
|
end
|
|
|
|
|
2021-08-13 20:07:01 +08:00
|
|
|
def default_table_state
|
|
|
|
Constants::REPOSITORY_SNAPSHOT_TABLE_DEFAULT_STATE
|
2020-04-21 20:49:36 +08:00
|
|
|
end
|
2020-05-12 22:26:44 +08:00
|
|
|
|
2020-06-04 15:42:22 +08:00
|
|
|
def assigned_rows(my_module)
|
|
|
|
return RepositoryRow.none if my_module != self.my_module
|
|
|
|
|
2020-05-18 18:56:10 +08:00
|
|
|
repository_rows
|
|
|
|
end
|
|
|
|
|
2022-02-23 16:24:38 +08:00
|
|
|
def default_search_fileds
|
|
|
|
['repository_rows.name', "('#{RepositoryRow::ID_PREFIX}' || repository_rows.parent_id)", 'users.full_name']
|
|
|
|
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-05-25 18:47:58 +08:00
|
|
|
|
|
|
|
def refresh_report_references
|
|
|
|
if selected
|
2020-05-27 18:15:19 +08:00
|
|
|
repositories = RepositoryBase.where(id: parent_id).or(RepositoryBase.where(parent_id: parent_id))
|
|
|
|
my_module.report_elements.where(repository: repositories).update(repository: self)
|
|
|
|
elsif original_repository
|
2020-05-25 18:47:58 +08:00
|
|
|
my_module.update_report_repository_references(original_repository)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-05-27 18:15:19 +08:00
|
|
|
def refresh_report_references_on_destroy
|
|
|
|
return my_module.update_report_repository_references(original_repository) if original_repository
|
|
|
|
|
|
|
|
repository_snapshot = my_module.repository_snapshots.where(parent_id: parent_id).order(created_at: :desc).first
|
|
|
|
my_module.update_report_repository_references(repository_snapshot) if repository_snapshot
|
2020-05-25 18:47:58 +08:00
|
|
|
end
|
2020-04-09 18:33:04 +08:00
|
|
|
end
|