2019-07-11 21:40:20 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2020-04-09 18:33:04 +08:00
|
|
|
class Repository < RepositoryBase
|
2017-06-23 22:06:11 +08:00
|
|
|
include SearchableModel
|
2019-02-26 18:01:15 +08:00
|
|
|
include SearchableByNameModel
|
2018-03-02 20:39:22 +08:00
|
|
|
include RepositoryImportParser
|
2020-06-17 20:05:23 +08:00
|
|
|
include ArchivableModel
|
2018-05-29 21:33:02 +08:00
|
|
|
|
2019-08-12 21:49:08 +08:00
|
|
|
enum permission_level: Extends::SHARED_INVENTORIES_PERMISSION_LEVELS
|
|
|
|
|
2020-06-19 00:07:23 +08:00
|
|
|
belongs_to :archived_by,
|
|
|
|
foreign_key: :archived_by_id,
|
|
|
|
class_name: 'User',
|
|
|
|
inverse_of: :archived_repositories,
|
|
|
|
optional: true
|
|
|
|
belongs_to :restored_by,
|
|
|
|
foreign_key: :restored_by_id,
|
|
|
|
class_name: 'User',
|
|
|
|
inverse_of: :restored_repositories,
|
|
|
|
optional: true
|
2019-07-11 21:40:20 +08:00
|
|
|
has_many :team_repositories, inverse_of: :repository, dependent: :destroy
|
2019-07-17 22:00:49 +08:00
|
|
|
has_many :teams_shared_with, through: :team_repositories, source: :team
|
2020-04-07 23:59:46 +08:00
|
|
|
has_many :repository_snapshots,
|
2020-04-09 18:33:04 +08:00
|
|
|
class_name: 'RepositorySnapshot',
|
2020-04-07 23:59:46 +08:00
|
|
|
foreign_key: :parent_id,
|
2020-05-13 18:58:31 +08:00
|
|
|
inverse_of: :original_repository
|
2017-05-16 21:27:36 +08:00
|
|
|
|
2020-05-21 17:51:37 +08:00
|
|
|
before_save :sync_name_with_snapshots, if: :name_changed?
|
2020-06-01 23:50:31 +08:00
|
|
|
after_save :unassign_unshared_items, if: :saved_change_to_permission_level
|
2020-05-27 18:15:19 +08:00
|
|
|
before_destroy :refresh_report_references_on_destroy, prepend: true
|
2020-05-21 17:51:37 +08:00
|
|
|
|
2017-05-16 21:27:36 +08:00
|
|
|
validates :name,
|
|
|
|
presence: true,
|
2019-05-07 19:47:50 +08:00
|
|
|
uniqueness: { scope: :team_id, case_sensitive: false },
|
2020-04-09 18:33:04 +08:00
|
|
|
length: { maximum: Constants::NAME_MAX_LENGTH }
|
2020-04-07 23:59:46 +08:00
|
|
|
|
2020-06-22 20:57:35 +08:00
|
|
|
scope :active, -> { where(archived: false) }
|
|
|
|
scope :archived, -> { where(archived: true) }
|
|
|
|
|
2019-07-24 22:01:02 +08:00
|
|
|
scope :accessible_by_teams, lambda { |teams|
|
2020-11-05 23:16:35 +08:00
|
|
|
accessible_repositories = left_outer_joins(:team_repositories)
|
|
|
|
accessible_repositories =
|
|
|
|
accessible_repositories
|
|
|
|
.where(team: teams)
|
|
|
|
.or(accessible_repositories.where(team_repositories: { team: teams }))
|
|
|
|
.or(accessible_repositories
|
|
|
|
.where(permission_level: [Extends::SHARED_INVENTORIES_PERMISSION_LEVELS[:shared_read],
|
|
|
|
Extends::SHARED_INVENTORIES_PERMISSION_LEVELS[:shared_write]]))
|
|
|
|
accessible_repositories.distinct
|
2019-07-24 22:01:02 +08:00
|
|
|
}
|
2018-05-24 20:39:25 +08:00
|
|
|
|
2020-06-29 17:43:23 +08:00
|
|
|
scope :assigned_to_project, lambda { |project|
|
|
|
|
accessible_by_teams(project.team)
|
|
|
|
.joins(repository_rows: { my_module_repository_rows: { my_module: { experiment: :project } } })
|
|
|
|
.where(repository_rows: { my_module_repository_rows: { my_module: { experiments: { project: project } } } })
|
|
|
|
}
|
|
|
|
|
2020-03-05 22:26:25 +08:00
|
|
|
def self.within_global_limits?
|
|
|
|
return true unless Rails.configuration.x.global_repositories_limit.positive?
|
|
|
|
|
|
|
|
count < Rails.configuration.x.global_repositories_limit
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.within_team_limits?(team)
|
|
|
|
return true unless Rails.configuration.x.team_repositories_limit.positive?
|
|
|
|
|
|
|
|
team.repositories.count < Rails.configuration.x.team_repositories_limit
|
|
|
|
end
|
|
|
|
|
2017-06-23 22:06:11 +08:00
|
|
|
def self.search(
|
|
|
|
user,
|
|
|
|
query = nil,
|
|
|
|
page = 1,
|
2018-03-09 18:05:43 +08:00
|
|
|
repository = nil,
|
2017-06-23 22:06:11 +08:00
|
|
|
options = {}
|
|
|
|
)
|
2020-11-05 23:16:35 +08:00
|
|
|
repositories = repository&.id || Repository.accessible_by_teams(user.teams).pluck(:id)
|
2017-06-23 22:06:11 +08:00
|
|
|
|
2020-01-16 22:30:19 +08:00
|
|
|
readable_rows = RepositoryRow.joins(:repository).where(repository_id: repositories)
|
2017-06-23 22:06:11 +08:00
|
|
|
|
2020-01-16 22:30:19 +08:00
|
|
|
matched_by_user = readable_rows.joins(:created_by).where_attributes_like('users.full_name', query, options)
|
2018-09-17 21:46:56 +08:00
|
|
|
|
2020-01-16 22:30:19 +08:00
|
|
|
repository_row_matches =
|
|
|
|
readable_rows.where_attributes_like(['repository_rows.name', 'repository_rows.id'], query, options)
|
|
|
|
|
|
|
|
repository_rows = readable_rows.where(id: repository_row_matches)
|
|
|
|
repository_rows = repository_rows.or(readable_rows.where(id: matched_by_user))
|
|
|
|
|
|
|
|
Extends::REPOSITORY_EXTRA_SEARCH_ATTR.each do |field, include_hash|
|
|
|
|
custom_cell_matches = readable_rows.joins(repository_cells: include_hash)
|
|
|
|
.where_attributes_like(field, query, options)
|
|
|
|
repository_rows = repository_rows.or(readable_rows.where(id: custom_cell_matches))
|
|
|
|
end
|
2017-06-23 22:06:11 +08:00
|
|
|
|
|
|
|
# Show all results if needed
|
|
|
|
if page == Constants::SEARCH_NO_LIMIT
|
2020-01-16 22:30:19 +08:00
|
|
|
repository_rows.select('repositories.id AS id, COUNT(DISTINCT repository_rows.id) AS counter')
|
|
|
|
.group('repositories.id')
|
2017-06-23 22:06:11 +08:00
|
|
|
else
|
2020-01-16 22:30:19 +08:00
|
|
|
repository_rows.limit(Constants::SEARCH_LIMIT)
|
|
|
|
.offset((page - 1) * Constants::SEARCH_LIMIT)
|
2017-06-23 22:06:11 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-04-21 20:49:36 +08:00
|
|
|
def default_columns_count
|
2020-11-11 00:24:04 +08:00
|
|
|
Constants::REPOSITORY_TABLE_DEFAULT_STATE['columns'].length
|
2020-04-21 20:49:36 +08:00
|
|
|
end
|
|
|
|
|
2019-08-19 21:43:55 +08:00
|
|
|
def i_shared?(team)
|
2019-08-26 14:56:46 +08:00
|
|
|
shared_with_anybody? && self.team == team
|
|
|
|
end
|
|
|
|
|
|
|
|
def shared_with_anybody?
|
2019-08-29 23:17:38 +08:00
|
|
|
(!not_shared? || team_repositories.any?)
|
2019-08-19 21:43:55 +08:00
|
|
|
end
|
|
|
|
|
2019-07-24 22:01:02 +08:00
|
|
|
def shared_with?(team)
|
2019-08-12 21:49:08 +08:00
|
|
|
return false if self.team == team
|
|
|
|
|
2019-08-29 23:17:38 +08:00
|
|
|
!not_shared? || private_shared_with?(team)
|
2019-07-24 22:01:02 +08:00
|
|
|
end
|
|
|
|
|
2019-08-06 21:17:13 +08:00
|
|
|
def shared_with_write?(team)
|
2019-08-12 21:49:08 +08:00
|
|
|
return false if self.team == team
|
|
|
|
|
2019-08-29 23:17:38 +08:00
|
|
|
shared_write? || private_shared_with_write?(team)
|
2019-08-12 21:49:08 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def shared_with_read?(team)
|
|
|
|
return false if self.team == team
|
|
|
|
|
2019-08-29 23:17:38 +08:00
|
|
|
shared_read? || team_repositories.where(team: team, permission_level: :shared_read).any?
|
2019-08-06 21:17:13 +08:00
|
|
|
end
|
|
|
|
|
2019-08-13 20:05:55 +08:00
|
|
|
def private_shared_with?(team)
|
|
|
|
team_repositories.where(team: team).any?
|
|
|
|
end
|
|
|
|
|
|
|
|
def private_shared_with_write?(team)
|
2019-08-29 23:17:38 +08:00
|
|
|
team_repositories.where(team: team, permission_level: :shared_write).any?
|
2019-08-13 20:05:55 +08:00
|
|
|
end
|
|
|
|
|
2019-02-26 18:01:15 +08:00
|
|
|
def self.viewable_by_user(_user, teams)
|
2020-09-21 20:36:34 +08:00
|
|
|
accessible_by_teams(teams)
|
2019-02-26 18:01:15 +08:00
|
|
|
end
|
|
|
|
|
2018-05-16 15:31:19 +08:00
|
|
|
def self.name_like(query)
|
|
|
|
where('repositories.name ILIKE ?', "%#{query}%")
|
|
|
|
end
|
2018-05-18 19:57:52 +08:00
|
|
|
|
2018-03-13 18:36:05 +08:00
|
|
|
def importable_repository_fields
|
2017-06-13 14:10:10 +08:00
|
|
|
fields = {}
|
2017-06-21 23:39:20 +08:00
|
|
|
# First and foremost add record name
|
2017-06-21 20:45:50 +08:00
|
|
|
fields['-1'] = I18n.t('repositories.default_column')
|
2017-06-13 14:10:10 +08:00
|
|
|
# Add all other custom columns
|
|
|
|
repository_columns.order(:created_at).each do |rc|
|
2018-03-13 18:36:05 +08:00
|
|
|
next unless rc.importable?
|
2019-08-23 16:57:02 +08:00
|
|
|
|
2017-06-13 14:10:10 +08:00
|
|
|
fields[rc.id] = rc.name
|
|
|
|
end
|
|
|
|
fields
|
|
|
|
end
|
|
|
|
|
2017-05-30 14:57:00 +08:00
|
|
|
def copy(created_by, name)
|
|
|
|
new_repo = nil
|
|
|
|
|
|
|
|
begin
|
|
|
|
Repository.transaction do
|
|
|
|
# Clone the repository object
|
|
|
|
new_repo = dup
|
|
|
|
new_repo.created_by = created_by
|
|
|
|
new_repo.name = name
|
|
|
|
new_repo.save!
|
|
|
|
|
|
|
|
# Clone columns (only if new_repo was saved)
|
|
|
|
repository_columns.find_each do |col|
|
|
|
|
new_col = col.dup
|
|
|
|
new_col.repository = new_repo
|
|
|
|
new_col.created_by = created_by
|
|
|
|
new_col.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
return false
|
|
|
|
end
|
|
|
|
|
|
|
|
# If everything is okay, return new_repo
|
2019-03-29 18:24:22 +08:00
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: :copy_inventory,
|
|
|
|
owner: created_by,
|
|
|
|
subject: new_repo,
|
|
|
|
team: new_repo.team,
|
|
|
|
message_items: { repository_new: new_repo.id, repository_original: id })
|
|
|
|
|
2017-05-30 14:57:00 +08:00
|
|
|
new_repo
|
|
|
|
end
|
2017-06-13 14:10:10 +08:00
|
|
|
|
2020-02-03 22:20:01 +08:00
|
|
|
def cell_preload_includes
|
|
|
|
cell_includes = []
|
|
|
|
repository_columns.pluck(:data_type).each do |data_type|
|
|
|
|
cell_includes << data_type.constantize::PRELOAD_INCLUDE
|
|
|
|
end
|
|
|
|
cell_includes
|
|
|
|
end
|
|
|
|
|
2017-06-19 20:05:37 +08:00
|
|
|
def import_records(sheet, mappings, user)
|
2018-03-02 20:39:22 +08:00
|
|
|
importer = RepositoryImportParser::Importer.new(sheet, mappings, user, self)
|
|
|
|
importer.run
|
2017-06-19 20:05:37 +08:00
|
|
|
end
|
2020-05-21 06:36:24 +08:00
|
|
|
|
2020-05-22 01:25:28 +08:00
|
|
|
def assigned_rows(my_module)
|
|
|
|
repository_rows.joins(:my_module_repository_rows).where(my_module_repository_rows: { my_module_id: my_module.id })
|
|
|
|
end
|
|
|
|
|
2020-06-01 23:50:31 +08:00
|
|
|
def unassign_unshared_items
|
|
|
|
return if shared_read? || shared_write?
|
|
|
|
|
|
|
|
MyModuleRepositoryRow.joins(my_module: { experiment: { project: :team } })
|
|
|
|
.joins(repository_row: :repository)
|
|
|
|
.where(repository_rows: { repository: self })
|
|
|
|
.where.not(my_module: { experiment: { projects: { team: team } } })
|
|
|
|
.where.not(my_module: { experiment: { projects: { team: teams_shared_with } } })
|
|
|
|
.destroy_all
|
|
|
|
end
|
|
|
|
|
2020-05-21 17:51:37 +08:00
|
|
|
private
|
|
|
|
|
|
|
|
def sync_name_with_snapshots
|
|
|
|
repository_snapshots.update(name: name)
|
|
|
|
end
|
2020-05-27 18:15:19 +08:00
|
|
|
|
|
|
|
def refresh_report_references_on_destroy
|
|
|
|
report_elements.find_each do |report_element|
|
|
|
|
repository_snapshot = report_element.my_module
|
|
|
|
.repository_snapshots
|
|
|
|
.where(original_repository: self)
|
|
|
|
.order(:selected, created_at: :desc)
|
|
|
|
.first
|
|
|
|
report_element.update(repository: repository_snapshot) if repository_snapshot
|
|
|
|
end
|
|
|
|
end
|
2017-05-16 21:27:36 +08:00
|
|
|
end
|