2017-06-06 23:35:29 +08:00
|
|
|
class RepositoryColumnsController < ApplicationController
|
|
|
|
include InputSanitizeHelper
|
2019-11-06 18:29:00 +08:00
|
|
|
include RepositoryColumnsHelper
|
|
|
|
|
2020-05-28 18:18:46 +08:00
|
|
|
before_action :load_repository
|
2020-06-30 19:44:49 +08:00
|
|
|
before_action :load_column, only: %i(edit update destroy_html destroy items)
|
|
|
|
before_action :check_create_permissions, only: %i(new create)
|
|
|
|
before_action :check_manage_permissions, only: %i(edit update destroy_html destroy)
|
2018-05-17 17:21:34 +08:00
|
|
|
before_action :load_asset_type_columns, only: :available_asset_type_columns
|
2018-03-22 16:35:36 +08:00
|
|
|
|
2020-05-28 18:18:46 +08:00
|
|
|
def index
|
2020-01-14 18:13:19 +08:00
|
|
|
render json: {
|
|
|
|
id: @repository.id,
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'repository_columns/manage_column_modal_index.html.erb'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
2018-03-22 16:35:36 +08:00
|
|
|
|
2020-05-28 18:18:46 +08:00
|
|
|
def new
|
2018-03-23 23:00:33 +08:00
|
|
|
@repository_column = RepositoryColumn.new
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
2019-10-16 18:52:46 +08:00
|
|
|
partial: 'repository_columns/manage_column_modal_content.html.erb'
|
2018-03-23 23:00:33 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2018-03-22 16:35:36 +08:00
|
|
|
end
|
2017-06-06 23:35:29 +08:00
|
|
|
|
2020-06-30 19:44:49 +08:00
|
|
|
def create
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2021-11-23 05:48:36 +08:00
|
|
|
def describe_all
|
2022-02-01 21:39:04 +08:00
|
|
|
response_json = @repository.repository_columns
|
|
|
|
.where(data_type: Extends::REPOSITORY_ADVANCED_SEARCHABLE_COLUMNS)
|
|
|
|
.map do |column|
|
2021-11-23 05:48:36 +08:00
|
|
|
{
|
|
|
|
id: column.id,
|
|
|
|
name: escape_input(column.name),
|
|
|
|
data_type: column.data_type,
|
2021-12-08 03:49:48 +08:00
|
|
|
items: column.items&.map { |item| { value: item.id, label: escape_input(item.data) } }
|
2021-11-23 05:48:36 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
render json: { response: response_json }
|
|
|
|
end
|
|
|
|
|
2020-05-28 18:18:46 +08:00
|
|
|
def edit
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'repository_columns/manage_column_modal_content.html.erb'
|
|
|
|
)
|
|
|
|
}
|
2017-06-06 23:35:29 +08:00
|
|
|
end
|
|
|
|
|
2020-06-30 19:44:49 +08:00
|
|
|
def update
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
|
|
|
|
2017-06-06 23:35:29 +08:00
|
|
|
def destroy_html
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
2018-04-20 17:44:43 +08:00
|
|
|
partial: 'repository_columns/delete_column_modal_body.html.erb'
|
2017-06-06 23:35:29 +08:00
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def destroy
|
2018-04-05 15:56:09 +08:00
|
|
|
column_id = @repository_column.id
|
2018-04-20 17:44:43 +08:00
|
|
|
column_name = @repository_column.name
|
2019-03-07 16:05:35 +08:00
|
|
|
|
|
|
|
log_activity(:delete_column_inventory) # Should we move this call somewhere?
|
2017-06-06 23:35:29 +08:00
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
if @repository_column.destroy
|
2018-03-22 16:35:36 +08:00
|
|
|
render json: {
|
|
|
|
message: t('libraries.repository_columns.destroy.success_flash',
|
2019-05-08 23:38:24 +08:00
|
|
|
name: escape_input(column_name)),
|
2018-03-22 16:35:36 +08:00
|
|
|
id: column_id,
|
|
|
|
status: :ok
|
|
|
|
}
|
2017-06-06 23:35:29 +08:00
|
|
|
else
|
2018-03-22 16:35:36 +08:00
|
|
|
render json: {
|
|
|
|
message: t('libraries.repository_columns.destroy.error_flash'),
|
|
|
|
status: :unprocessable_entity
|
|
|
|
}
|
2017-06-06 23:35:29 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-07-08 19:28:12 +08:00
|
|
|
def items
|
|
|
|
raise NotImplementedError
|
|
|
|
end
|
2020-06-30 19:44:49 +08:00
|
|
|
|
2018-05-17 17:21:34 +08:00
|
|
|
def available_asset_type_columns
|
2021-07-23 17:56:28 +08:00
|
|
|
if @asset_columns.blank?
|
2018-05-16 15:31:19 +08:00
|
|
|
render json: {
|
|
|
|
no_items: t(
|
|
|
|
'projects.reports.new.save_PDF_to_inventory_modal.no_columns'
|
|
|
|
)
|
|
|
|
}, status: :ok
|
|
|
|
else
|
|
|
|
render json: { results: @asset_columns }, status: :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2018-06-12 21:28:03 +08:00
|
|
|
def available_columns
|
2020-04-09 18:33:04 +08:00
|
|
|
render json: { columns: @repository.repository_columns.pluck(:id) }, status: :ok
|
2018-06-12 21:28:03 +08:00
|
|
|
end
|
|
|
|
|
2017-06-06 23:35:29 +08:00
|
|
|
private
|
|
|
|
|
2018-05-16 15:31:19 +08:00
|
|
|
include StringUtility
|
|
|
|
AvailableRepositoryColumn = Struct.new(:id, :name)
|
|
|
|
|
2020-05-28 18:18:46 +08:00
|
|
|
def load_repository
|
|
|
|
@repository = Repository.accessible_by_teams(current_team).find_by(id: params[:repository_id])
|
2017-06-06 23:35:29 +08:00
|
|
|
render_404 unless @repository
|
|
|
|
end
|
|
|
|
|
2020-05-28 18:18:46 +08:00
|
|
|
def load_column
|
|
|
|
@repository_column = @repository.repository_columns.find_by(id: params[:id])
|
|
|
|
render_404 unless @repository_column
|
2017-06-06 23:35:29 +08:00
|
|
|
end
|
|
|
|
|
2018-05-16 15:31:19 +08:00
|
|
|
def load_asset_type_columns
|
2021-11-23 05:48:36 +08:00
|
|
|
render_403 && return unless can_read_repository?(@repository)
|
|
|
|
|
2018-05-16 15:31:19 +08:00
|
|
|
@asset_columns = load_asset_columns(search_params[:q])
|
|
|
|
end
|
|
|
|
|
2020-05-28 18:18:46 +08:00
|
|
|
def check_create_permissions
|
|
|
|
render_403 unless can_create_repository_columns?(@repository)
|
|
|
|
end
|
|
|
|
|
2018-02-16 01:46:29 +08:00
|
|
|
def check_manage_permissions
|
|
|
|
render_403 unless can_manage_repository_column?(@repository_column)
|
2018-01-05 17:43:59 +08:00
|
|
|
end
|
|
|
|
|
2018-05-16 15:31:19 +08:00
|
|
|
def search_params
|
|
|
|
params.permit(:q, :repository_id)
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_asset_columns(query)
|
|
|
|
@repository.repository_columns
|
|
|
|
.asset_type.name_like(query)
|
|
|
|
.limit(Constants::SEARCH_LIMIT)
|
|
|
|
.select(:id, :name)
|
|
|
|
.collect do |column|
|
|
|
|
AvailableRepositoryColumn.new(
|
|
|
|
column.id,
|
2018-05-17 17:21:34 +08:00
|
|
|
ellipsize(column.name, 75, 50)
|
2018-05-16 15:31:19 +08:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-03-07 16:05:35 +08:00
|
|
|
def log_activity(type_of)
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @repository,
|
|
|
|
team: current_team,
|
|
|
|
message_items: {
|
|
|
|
repository_column: @repository_column.id,
|
|
|
|
repository: @repository.id
|
|
|
|
})
|
|
|
|
end
|
2017-06-06 23:35:29 +08:00
|
|
|
end
|