2019-07-11 21:40:20 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-06-23 21:19:08 +08:00
|
|
|
class Repository < ApplicationRecord
|
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
|
2018-05-24 20:39:25 +08:00
|
|
|
include Discard::Model
|
2018-05-29 21:33:02 +08:00
|
|
|
|
2019-08-12 21:49:08 +08:00
|
|
|
enum permission_level: Extends::SHARED_INVENTORIES_PERMISSION_LEVELS
|
|
|
|
|
2018-05-29 21:33:02 +08:00
|
|
|
attribute :discarded_by_id, :integer
|
|
|
|
|
2019-07-12 22:43:54 +08:00
|
|
|
belongs_to :team
|
2019-05-07 19:47:50 +08:00
|
|
|
belongs_to :created_by, foreign_key: :created_by_id, class_name: 'User'
|
2018-05-18 19:57:52 +08:00
|
|
|
has_many :repository_columns, dependent: :destroy
|
|
|
|
has_many :repository_rows, dependent: :destroy
|
2017-06-07 19:36:39 +08:00
|
|
|
has_many :repository_table_states,
|
|
|
|
inverse_of: :repository, dependent: :destroy
|
2017-06-05 18:24:06 +08:00
|
|
|
has_many :report_elements, inverse_of: :repository, dependent: :destroy
|
2018-02-09 21:11:56 +08:00
|
|
|
has_many :repository_list_items, inverse_of: :repository, dependent: :destroy
|
2019-12-06 20:18:35 +08:00
|
|
|
has_many :repository_checklist_items, inverse_of: :repository, dependent: :destroy
|
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
|
2017-05-16 21:27:36 +08:00
|
|
|
|
|
|
|
auto_strip_attributes :name, nullify: false
|
|
|
|
validates :name,
|
|
|
|
presence: true,
|
2019-05-07 19:47:50 +08:00
|
|
|
uniqueness: { scope: :team_id, case_sensitive: false },
|
2017-05-16 21:27:36 +08:00
|
|
|
length: { maximum: Constants::NAME_MAX_LENGTH }
|
|
|
|
validates :team, presence: true
|
|
|
|
validates :created_by, presence: true
|
2017-05-30 14:57:00 +08:00
|
|
|
|
2018-05-24 20:39:25 +08:00
|
|
|
default_scope -> { kept }
|
2019-07-24 22:01:02 +08:00
|
|
|
scope :accessible_by_teams, lambda { |teams|
|
|
|
|
left_outer_joins(:team_repositories)
|
2019-08-12 21:49:08 +08:00
|
|
|
.where('repositories.team_id IN (?) '\
|
|
|
|
'OR team_repositories.team_id IN (?) '\
|
2019-08-29 23:17:38 +08:00
|
|
|
'OR repositories.permission_level = ? '\
|
|
|
|
'OR repositories.permission_level = ? ',
|
|
|
|
teams,
|
|
|
|
teams,
|
|
|
|
Extends::SHARED_INVENTORIES_PERMISSION_LEVELS[:shared_read],
|
|
|
|
Extends::SHARED_INVENTORIES_PERMISSION_LEVELS[:shared_write])
|
2019-08-12 16:55:30 +08:00
|
|
|
.distinct
|
2019-07-24 22:01:02 +08:00
|
|
|
}
|
2018-05-24 20:39:25 +08:00
|
|
|
|
2019-08-26 14:56:46 +08:00
|
|
|
scope :used_on_task_but_unshared, lambda { |task, team|
|
|
|
|
where(id: task.repository_rows
|
|
|
|
.select(:repository_id))
|
|
|
|
.where.not(id: accessible_by_teams(team.id).select(:id)).distinct
|
|
|
|
}
|
|
|
|
|
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-01-16 22:30:19 +08:00
|
|
|
repositories = repository&.id || Repository.accessible_by_teams(user.teams.pluck(:id)).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
|
|
|
|
|
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)
|
|
|
|
where(team: teams)
|
|
|
|
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-06-12 21:28:03 +08:00
|
|
|
def available_columns_ids
|
|
|
|
repository_columns.pluck(:id)
|
|
|
|
end
|
|
|
|
|
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
|
|
|
|
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
|
2018-05-29 21:33:02 +08:00
|
|
|
|
|
|
|
def destroy_discarded(discarded_by_id = nil)
|
|
|
|
self.discarded_by_id = discarded_by_id
|
|
|
|
destroy
|
|
|
|
end
|
|
|
|
handle_asynchronously :destroy_discarded,
|
|
|
|
queue: :clear_discarded_repository,
|
|
|
|
priority: 20
|
2017-05-16 21:27:36 +08:00
|
|
|
end
|