2017-06-06 23:35:29 +08:00
|
|
|
class RepositoryColumnsController < ApplicationController
|
|
|
|
include InputSanitizeHelper
|
2018-06-12 21:28:03 +08:00
|
|
|
ACTIONS = %i(
|
|
|
|
create index create_html available_asset_type_columns available_columns
|
|
|
|
).freeze
|
2018-05-17 17:21:34 +08:00
|
|
|
before_action :load_vars,
|
|
|
|
except: ACTIONS
|
2018-05-16 15:31:19 +08:00
|
|
|
before_action :load_vars_nested,
|
2018-05-17 17:21:34 +08:00
|
|
|
only: ACTIONS
|
2018-01-05 17:43:59 +08:00
|
|
|
before_action :check_create_permissions, only: :create
|
2018-05-16 15:31:19 +08:00
|
|
|
before_action :check_manage_permissions,
|
2018-05-17 17:21:34 +08:00
|
|
|
except: ACTIONS
|
2018-03-22 16:35:36 +08:00
|
|
|
before_action :load_repository_columns, only: :index
|
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
|
|
|
|
2018-03-23 23:00:33 +08:00
|
|
|
def index; end
|
2018-03-22 16:35:36 +08:00
|
|
|
|
2018-03-23 23:00:33 +08:00
|
|
|
def create_html
|
|
|
|
@repository_column = RepositoryColumn.new
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'repository_columns/manage_column_modal.html.erb'
|
|
|
|
)
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
2018-03-22 16:35:36 +08:00
|
|
|
end
|
2017-06-06 23:35:29 +08:00
|
|
|
|
|
|
|
def create
|
|
|
|
@repository_column = RepositoryColumn.new(repository_column_params)
|
|
|
|
@repository_column.repository = @repository
|
|
|
|
@repository_column.created_by = current_user
|
|
|
|
|
|
|
|
respond_to do |format|
|
2018-04-19 20:02:42 +08:00
|
|
|
format.json do
|
|
|
|
if @repository_column.save
|
2019-03-07 16:05:35 +08:00
|
|
|
log_activity(:create_column_inventory)
|
|
|
|
|
2018-04-19 20:02:42 +08:00
|
|
|
if generate_repository_list_items(params[:list_items])
|
|
|
|
render json: {
|
|
|
|
id: @repository_column.id,
|
|
|
|
name: escape_input(@repository_column.name),
|
|
|
|
message: t('libraries.repository_columns.create.success_flash',
|
|
|
|
name: @repository_column.name),
|
|
|
|
edit_url:
|
|
|
|
edit_repository_repository_column_path(@repository,
|
|
|
|
@repository_column),
|
|
|
|
update_url:
|
|
|
|
repository_repository_column_path(@repository,
|
|
|
|
@repository_column),
|
|
|
|
destroy_html_url:
|
|
|
|
repository_columns_destroy_html_path(@repository,
|
|
|
|
@repository_column)
|
|
|
|
},
|
|
|
|
status: :ok
|
|
|
|
else
|
|
|
|
render json: {
|
|
|
|
message: {
|
|
|
|
repository_list_items:
|
|
|
|
t('libraries.repository_columns.repository_list_items_limit',
|
|
|
|
limit: Constants::REPOSITORY_LIST_ITEMS_PER_COLUMN)
|
|
|
|
}
|
|
|
|
}, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
else
|
2018-03-23 23:00:33 +08:00
|
|
|
render json: { message: @repository_column.errors.full_messages },
|
2017-06-06 23:35:29 +08:00
|
|
|
status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2018-03-23 23:00:33 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string(
|
|
|
|
partial: 'repository_columns/manage_column_modal.html.erb'
|
|
|
|
)
|
|
|
|
}
|
2017-06-06 23:35:29 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
2019-09-12 23:21:48 +08:00
|
|
|
@repository_column.update(repository_column_params)
|
2017-06-06 23:35:29 +08:00
|
|
|
if @repository_column.save
|
2019-03-07 16:05:35 +08:00
|
|
|
log_activity(:edit_column_inventory)
|
|
|
|
|
2018-04-19 20:02:42 +08:00
|
|
|
if update_repository_list_items(params[:list_items])
|
|
|
|
render json: {
|
|
|
|
id: @repository_column.id,
|
|
|
|
name: escape_input(@repository_column.name),
|
|
|
|
message: t('libraries.repository_columns.update.success_flash',
|
2019-05-08 23:38:24 +08:00
|
|
|
name: escape_input(@repository_column.name))
|
2018-04-19 20:02:42 +08:00
|
|
|
}, status: :ok
|
|
|
|
else
|
|
|
|
render json: {
|
|
|
|
message: {
|
|
|
|
repository_list_items:
|
|
|
|
t('libraries.repository_columns.repository_list_items_limit',
|
|
|
|
limit: Constants::REPOSITORY_LIST_ITEMS_PER_COLUMN)
|
|
|
|
}
|
|
|
|
}, status: :unprocessable_entity
|
|
|
|
end
|
2017-06-06 23:35:29 +08:00
|
|
|
else
|
2018-03-23 23:00:33 +08:00
|
|
|
render json: { message: @repository_column.errors.full_messages },
|
2017-06-06 23:35:29 +08:00
|
|
|
status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
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
|
|
|
|
|
2018-05-17 17:21:34 +08:00
|
|
|
def available_asset_type_columns
|
2018-05-16 15:31:19 +08:00
|
|
|
if @asset_columns.empty?
|
|
|
|
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
|
|
|
|
render json: { columns: @repository.available_columns_ids }, status: :ok
|
|
|
|
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)
|
|
|
|
|
2017-06-06 23:35:29 +08:00
|
|
|
def load_vars
|
2019-08-12 16:55:30 +08:00
|
|
|
@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
|
2019-07-12 22:44:15 +08:00
|
|
|
@repository_column = @repository.repository_columns.find_by_id(params[:id])
|
2017-06-06 23:35:29 +08:00
|
|
|
render_404 unless @repository_column
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_vars_nested
|
2019-08-12 16:55:30 +08:00
|
|
|
@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
|
|
|
|
|
2018-03-22 16:35:36 +08:00
|
|
|
def load_repository_columns
|
2018-03-23 23:00:33 +08:00
|
|
|
@repository_columns = @repository.repository_columns
|
|
|
|
.order(created_at: :desc)
|
2018-03-22 16:35:36 +08:00
|
|
|
end
|
|
|
|
|
2018-05-16 15:31:19 +08:00
|
|
|
def load_asset_type_columns
|
2019-07-17 22:00:49 +08:00
|
|
|
render_403 unless can_read_repository?(@repository)
|
2018-05-16 15:31:19 +08:00
|
|
|
@asset_columns = load_asset_columns(search_params[:q])
|
|
|
|
end
|
|
|
|
|
2018-01-05 17:43:59 +08:00
|
|
|
def check_create_permissions
|
2019-07-12 22:43:54 +08:00
|
|
|
render_403 unless can_create_repository_columns?(@repository)
|
2017-06-06 23:35:29 +08:00
|
|
|
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
|
|
|
|
|
2017-06-06 23:35:29 +08:00
|
|
|
def repository_column_params
|
2018-03-23 23:00:33 +08:00
|
|
|
params.require(:repository_column).permit(:name, :data_type)
|
|
|
|
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
|
|
|
|
|
2018-03-23 23:00:33 +08:00
|
|
|
def generate_repository_list_items(item_names)
|
2018-04-20 15:53:15 +08:00
|
|
|
return true unless @repository_column.data_type == 'RepositoryListValue'
|
2018-04-10 21:45:51 +08:00
|
|
|
column_items = @repository_column.repository_list_items.size
|
2018-04-19 20:02:42 +08:00
|
|
|
success = true
|
2018-03-23 23:00:33 +08:00
|
|
|
item_names.split(',').uniq.each do |name|
|
2018-04-19 20:02:42 +08:00
|
|
|
if column_items >= Constants::REPOSITORY_LIST_ITEMS_PER_COLUMN
|
|
|
|
success = false
|
|
|
|
next
|
|
|
|
end
|
2018-03-23 23:00:33 +08:00
|
|
|
RepositoryListItem.create(
|
|
|
|
repository: @repository,
|
|
|
|
repository_column: @repository_column,
|
|
|
|
data: name,
|
|
|
|
created_by: current_user,
|
|
|
|
last_modified_by: current_user
|
|
|
|
)
|
2018-04-10 21:45:51 +08:00
|
|
|
column_items += 1
|
2018-03-23 23:00:33 +08:00
|
|
|
end
|
2018-04-19 20:02:42 +08:00
|
|
|
success
|
2018-03-23 23:00:33 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update_repository_list_items(item_names)
|
2018-04-20 15:53:15 +08:00
|
|
|
return true unless @repository_column.data_type == 'RepositoryListValue'
|
2018-04-10 21:45:51 +08:00
|
|
|
column_items = @repository_column.repository_list_items.size
|
2018-03-23 23:00:33 +08:00
|
|
|
items_list = item_names.split(',').uniq
|
|
|
|
existing = @repository_column.repository_list_items.pluck(:data)
|
|
|
|
existing.each do |name|
|
|
|
|
next if items_list.include? name
|
|
|
|
list_item_id = @repository_column.repository_list_items
|
|
|
|
.find_by_data(name)
|
|
|
|
.destroy
|
|
|
|
.id
|
|
|
|
RepositoryCell.where(
|
|
|
|
'value_type = ? AND value_id = ?',
|
|
|
|
'RepositoryListValue',
|
|
|
|
list_item_id
|
|
|
|
).destroy_all
|
|
|
|
end
|
2018-04-19 20:02:42 +08:00
|
|
|
success = true
|
2018-03-23 23:00:33 +08:00
|
|
|
items_list.each do |name|
|
|
|
|
next if @repository_column.repository_list_items.find_by_data(name)
|
2018-04-19 20:02:42 +08:00
|
|
|
if column_items >= Constants::REPOSITORY_LIST_ITEMS_PER_COLUMN
|
|
|
|
success = false
|
|
|
|
next
|
|
|
|
end
|
2018-03-23 23:00:33 +08:00
|
|
|
RepositoryListItem.create(
|
|
|
|
repository: @repository,
|
|
|
|
repository_column: @repository_column,
|
|
|
|
data: name,
|
|
|
|
created_by: current_user,
|
|
|
|
last_modified_by: current_user
|
|
|
|
)
|
2018-04-10 21:45:51 +08:00
|
|
|
column_items += 1
|
2018-03-23 23:00:33 +08:00
|
|
|
end
|
2018-04-19 20:02:42 +08:00
|
|
|
success
|
2017-06-06 23:35:29 +08:00
|
|
|
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
|