2019-05-07 19:47:50 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-23 21:19:08 +08:00
|
|
|
class RepositoryRow < ApplicationRecord
|
2017-06-23 22:06:11 +08:00
|
|
|
include SearchableModel
|
2019-08-08 21:51:08 +08:00
|
|
|
include SearchableByNameModel
|
2020-06-10 00:43:22 +08:00
|
|
|
include ArchivableModel
|
2017-06-23 22:06:11 +08:00
|
|
|
|
2020-04-09 18:33:04 +08:00
|
|
|
belongs_to :repository, class_name: 'RepositoryBase'
|
2019-05-07 19:47:50 +08:00
|
|
|
belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User'
|
|
|
|
belongs_to :last_modified_by, foreign_key: :last_modified_by_id, class_name: 'User'
|
2020-06-08 22:22:37 +08:00
|
|
|
belongs_to :archived_by,
|
|
|
|
foreign_key: :archived_by_id,
|
|
|
|
class_name: 'User',
|
|
|
|
inverse_of: :archived_repository_rows,
|
|
|
|
optional: true
|
2020-06-10 00:43:22 +08:00
|
|
|
belongs_to :restored_by,
|
2020-06-23 14:24:52 +08:00
|
|
|
foreign_key: :restored_by_id,
|
2020-06-10 00:43:22 +08:00
|
|
|
class_name: 'User',
|
|
|
|
inverse_of: :restored_repository_rows,
|
|
|
|
optional: true
|
2018-08-07 20:19:49 +08:00
|
|
|
has_many :repository_cells, -> { order(:id) }, dependent: :destroy
|
2017-05-16 21:27:36 +08:00
|
|
|
has_many :repository_columns, through: :repository_cells
|
2017-06-07 19:36:39 +08:00
|
|
|
has_many :my_module_repository_rows,
|
2017-06-06 23:35:29 +08:00
|
|
|
inverse_of: :repository_row, dependent: :destroy
|
2017-06-07 19:36:39 +08:00
|
|
|
has_many :my_modules, through: :my_module_repository_rows
|
2017-05-16 21:27:36 +08:00
|
|
|
|
|
|
|
auto_strip_attributes :name, nullify: false
|
|
|
|
validates :name,
|
|
|
|
presence: true,
|
|
|
|
length: { maximum: Constants::NAME_MAX_LENGTH }
|
|
|
|
validates :created_by, presence: true
|
2020-06-05 05:29:37 +08:00
|
|
|
|
2020-06-24 17:22:28 +08:00
|
|
|
scope :active, -> { where(archived: false) }
|
|
|
|
scope :archived, -> { where(archived: true) }
|
2018-04-26 20:13:46 +08:00
|
|
|
|
2019-08-08 21:51:08 +08:00
|
|
|
def self.viewable_by_user(user, teams)
|
|
|
|
where(repository: Repository.viewable_by_user(user, teams))
|
|
|
|
end
|
|
|
|
|
2018-05-16 15:31:19 +08:00
|
|
|
def self.name_like(query)
|
|
|
|
where('repository_rows.name ILIKE ?', "%#{query}%")
|
|
|
|
end
|
2019-04-29 20:02:02 +08:00
|
|
|
|
|
|
|
def self.change_owner(team, user, new_owner)
|
|
|
|
joins(:repository)
|
|
|
|
.where('repositories.team_id = ? and repository_rows.created_by_id = ?', team, user)
|
|
|
|
.update_all(created_by_id: new_owner.id)
|
|
|
|
end
|
2020-01-25 16:43:05 +08:00
|
|
|
|
|
|
|
def editable?
|
|
|
|
true
|
|
|
|
end
|
2020-04-09 18:33:04 +08:00
|
|
|
|
2020-06-23 23:12:31 +08:00
|
|
|
def row_archived?
|
|
|
|
self[:archived]
|
|
|
|
end
|
|
|
|
|
|
|
|
def archived
|
|
|
|
row_archived? || repository&.archived?
|
|
|
|
end
|
|
|
|
|
|
|
|
def archived?
|
|
|
|
row_archived? ? super : repository.archived?
|
|
|
|
end
|
|
|
|
|
|
|
|
def archived_by
|
|
|
|
row_archived? ? super : repository.archived_by
|
|
|
|
end
|
|
|
|
|
|
|
|
def archived_on
|
|
|
|
row_archived? ? super : repository.archived_on
|
|
|
|
end
|
|
|
|
|
2020-04-09 18:33:04 +08:00
|
|
|
def snapshot!(repository_snapshot)
|
|
|
|
row_snapshot = dup
|
|
|
|
row_snapshot.assign_attributes(
|
|
|
|
repository: repository_snapshot,
|
|
|
|
parent_id: id,
|
|
|
|
created_at: created_at,
|
|
|
|
updated_at: updated_at
|
|
|
|
)
|
|
|
|
row_snapshot.save!
|
|
|
|
|
|
|
|
repository_cells.each { |cell| cell.snapshot!(row_snapshot) }
|
|
|
|
end
|
2017-05-16 21:27:36 +08:00
|
|
|
end
|