scinote-web/app/controllers/repository_rows_controller.rb

329 lines
11 KiB
Ruby
Raw Normal View History

2017-06-06 23:35:29 +08:00
class RepositoryRowsController < ApplicationController
include InputSanitizeHelper
include ActionView::Helpers::TextHelper
include ApplicationHelper
2020-05-08 17:56:29 +08:00
include MyModulesHelper
2017-06-06 23:35:29 +08:00
before_action :load_info_modal_vars, only: %i(show assigned_task_list)
2017-06-06 23:35:29 +08:00
before_action :load_vars, only: %i(edit update)
before_action :load_repository,
only: %i(create
delete_records
index
copy_records
available_rows)
2017-06-06 23:35:29 +08:00
before_action :check_create_permissions, only: :create
2019-11-22 20:23:02 +08:00
before_action :check_delete_permissions, only: :delete_records
before_action :check_manage_permissions,
2019-11-22 20:23:02 +08:00
only: %i(edit update copy_records)
2017-06-06 23:35:29 +08:00
def index
@draw = params[:draw].to_i
per_page = params[:length] == '-1' ? 100 : params[:length].to_i
page = (params[:start].to_i / per_page) + 1
datatable_service = RepositoryDatatableService.new(@repository, params, current_user)
@all_rows_count = datatable_service.all_count
@columns_mappings = datatable_service.mappings
@repository_rows = datatable_service.repository_rows
.preload(:repository_columns,
:created_by,
repository_cells: @repository.cell_preload_includes)
.page(page)
.per(per_page)
end
2017-06-06 23:35:29 +08:00
def create
2019-12-10 15:43:26 +08:00
service = RepositoryRows::CreateRepositoryRowService
.call(repository: @repository, user: current_user, params: update_params)
2017-06-22 23:26:11 +08:00
2019-12-10 15:43:26 +08:00
if service.succeed?
repository_row = service.repository_row
log_activity(:create_item_inventory, repository_row)
repository_row.repository_cells.where(value_type: 'RepositoryTextValue').each do |repository_cell|
record_annotation_notification(repository_row, repository_cell)
end
2019-12-10 15:43:26 +08:00
render json: { id: service.repository_row.id, flash: t('repositories.create.success_flash',
record: escape_input(repository_row.name),
2019-12-10 15:43:26 +08:00
repository: escape_input(@repository.name)) },
status: :ok
else
render json: service.errors, status: :bad_request
2017-06-06 23:35:29 +08:00
end
end
def show
respond_to do |format|
format.json do
render json: {
html: render_to_string(
partial: 'repositories/repository_row_info_modal.html.erb'
)
}
end
end
end
2017-06-06 23:35:29 +08:00
def edit
json = {
repository_row: {
name: escape_input(@record.name),
repository_cells: {},
repository_column_items: fetch_columns_list_items
2017-06-06 23:35:29 +08:00
}
}
# Add custom cells ids as key (easier lookup on js side)
@record.repository_cells.each do |cell|
if cell.value_type == 'RepositoryAssetValue'
cell_value = cell.value.asset
else
cell_value = escape_input(cell.value.data)
end
2017-06-06 23:35:29 +08:00
json[:repository_row][:repository_cells][cell.repository_column_id] = {
repository_cell_id: cell.id,
cell_column_id: cell.repository_column.id, # needed for mappings
value: cell_value,
type: cell.value_type,
list_items: fetch_list_items(cell)
2017-06-06 23:35:29 +08:00
}
end
respond_to do |format|
format.html
format.json { render json: json }
end
end
def update
2019-12-10 15:43:26 +08:00
row_update = RepositoryRows::UpdateRepositoryRowService
.call(repository_row: @record, user: current_user, params: update_params)
2017-06-06 23:35:29 +08:00
2019-12-10 15:43:26 +08:00
if row_update.succeed?
if row_update.record_updated
log_activity(:edit_item_inventory, @record)
@record.repository_cells.where(value_type: 'RepositoryTextValue').each do |repository_cell|
record_annotation_notification(@record, repository_cell)
end
end
2019-12-10 15:43:26 +08:00
render json: { id: @record.id, flash: t('repositories.update.success_flash',
record: escape_input(@record.name),
repository: escape_input(@repository.name)) },
status: :ok
2018-04-16 20:10:49 +08:00
else
2019-12-10 15:43:26 +08:00
render json: row_update.errors, status: :bad_request
2018-04-16 20:10:49 +08:00
end
end
2017-06-06 23:35:29 +08:00
def delete_records
deleted_count = 0
2017-12-20 00:53:45 +08:00
if selected_params
selected_params.each do |row_id|
2017-06-06 23:35:29 +08:00
row = @repository.repository_rows.find_by_id(row_id)
2019-07-12 22:43:54 +08:00
next unless row && can_manage_repository_rows?(@repository)
2019-03-07 14:38:34 +08:00
2019-07-12 22:43:54 +08:00
log_activity(:delete_item_inventory, row)
row.destroy && deleted_count += 1
2017-06-06 23:35:29 +08:00
end
if deleted_count.zero?
flash = t('repositories.destroy.no_deleted_records_flash',
2017-12-20 00:53:45 +08:00
other_records_number: selected_params.count)
elsif deleted_count != selected_params.count
not_deleted_count = selected_params.count - deleted_count
2017-06-06 23:35:29 +08:00
flash = t('repositories.destroy.contains_other_records_flash',
records_number: deleted_count,
other_records_number: not_deleted_count)
else
flash = t('repositories.destroy.success_flash',
records_number: deleted_count)
end
respond_to do |format|
color = deleted_count.zero? ? 'info' : 'success'
format.json { render json: { flash: flash, color: color }, status: :ok }
2017-06-06 23:35:29 +08:00
end
else
respond_to do |format|
format.json do
render json: {
flash: t('repositories.destroy.no_records_selected_flash')
}, status: :bad_request
end
end
end
end
def copy_records
2018-04-13 17:47:33 +08:00
duplicate_service = RepositoryActions::DuplicateRows.new(
current_user, @repository, copy_records_params
2018-04-13 17:47:33 +08:00
)
duplicate_service.call
render json: {
flash: t('repositories.copy_records_report',
number: duplicate_service.number_of_duplicated_items)
}, status: :ok
end
def available_rows
if @repository.repository_rows.empty?
no_items_string =
"#{t('projects.reports.new.save_PDF_to_inventory_modal.no_items')} " \
"#{link_to(t('projects.reports.new.save_PDF_to_inventory_modal.here'),
repository_path(@repository),
data: { 'no-turbolink' => true })}"
render json: { no_items: no_items_string },
status: :ok
else
render json: { results: load_available_rows(search_params[:q]) },
status: :ok
end
end
def assigned_task_list
2020-04-28 18:41:59 +08:00
my_modules = @repository_row.my_modules.joins(experiment: :project)
.search_by_name(current_user, current_team, params[:query])
render json: {
2020-05-07 18:51:18 +08:00
html: render_to_string(partial: 'shared/my_modules_list_partial.html.erb', locals: {
2020-05-08 17:56:29 +08:00
my_modules: my_modules
2020-05-07 18:51:18 +08:00
})
2020-04-28 18:41:59 +08:00
}
end
2017-06-06 23:35:29 +08:00
private
include StringUtility
AvailableRepositoryRow = Struct.new(:id, :name, :has_file_attached)
def load_info_modal_vars
2018-03-02 21:14:27 +08:00
@repository_row = RepositoryRow.eager_load(:created_by, repository: [:team])
2018-03-02 21:16:31 +08:00
.find_by_id(params[:id])
2018-03-02 21:14:27 +08:00
@assigned_modules = MyModuleRepositoryRow.eager_load(
my_module: [{ experiment: :project }]
).where(repository_row: @repository_row)
render_404 and return unless @repository_row
render_403 unless can_read_repository?(@repository_row.repository)
end
2017-06-06 23:35:29 +08:00
def load_vars
@repository = Repository.accessible_by_teams(current_team)
.eager_load(:repository_columns)
.find_by_id(params[:repository_id])
2019-07-12 23:03:09 +08:00
@record = @repository.repository_rows
.eager_load(:repository_columns)
.find_by_id(params[:id])
2017-06-06 23:35:29 +08:00
render_404 unless @repository && @record
end
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
render_403 unless can_read_repository?(@repository)
end
2017-06-06 23:35:29 +08:00
def check_create_permissions
2019-07-12 22:43:54 +08:00
render_403 unless can_create_repository_rows?(@repository)
2017-06-06 23:35:29 +08:00
end
def check_manage_permissions
2019-07-12 22:43:54 +08:00
render_403 unless can_manage_repository_rows?(@repository)
2017-06-06 23:35:29 +08:00
end
2019-11-22 20:23:02 +08:00
def check_delete_permissions
render_403 unless can_delete_repository_rows?(@repository)
end
def remove_file_columns_params
JSON.parse(params.fetch(:remove_file_columns) { '[]' })
end
2017-12-20 00:53:45 +08:00
def selected_params
params.permit(selected_rows: []).to_h[:selected_rows]
2017-06-06 23:35:29 +08:00
end
def copy_records_params
process_ids = params[:selected_rows].map(&:to_i).uniq
@repository.repository_rows.where(id: process_ids).pluck(:id)
end
def load_available_rows(query)
@repository.repository_rows
.includes(:repository_cells)
.name_like(search_params[:q])
.limit(Constants::SEARCH_LIMIT)
.select(:id, :name)
.collect do |row|
with_asset_cell = row.repository_cells.where(
'repository_cells.repository_column_id = ?',
search_params[:repository_column_id]
)
AvailableRepositoryRow.new(row.id,
2018-05-17 17:21:34 +08:00
ellipsize(row.name, 75, 50),
with_asset_cell.present?)
end
end
def search_params
params.permit(:q, :repository_id, :repository_column_id)
end
2017-06-06 23:35:29 +08:00
def record_annotation_notification(record, cell, old_text = nil)
smart_annotation_notification(
old_text: old_text,
2017-06-06 23:35:29 +08:00
new_text: cell.value.data,
title: t('notifications.repository_annotation_title',
user: current_user.full_name,
column: cell.repository_column.name,
record: record.name,
repository: record.repository.name),
2017-06-06 23:35:29 +08:00
message: t('notifications.repository_annotation_message_html',
record: link_to(record.name, repository_url(@repository)),
column: link_to(cell.repository_column.name, repository_url(@repository)))
2017-06-06 23:35:29 +08:00
)
end
def fetch_list_items(cell)
return [] if cell.value_type != 'RepositoryListValue'
RepositoryListItem.where(repository: @repository)
.where(repository_column: cell.repository_column)
.limit(Constants::SEARCH_LIMIT)
.pluck(:id, :data)
.map { |li| [li[0], escape_input(li[1])] }
end
def fetch_columns_list_items
collection = []
@repository.repository_columns
.list_type
.preload(:repository_list_items)
.each do |column|
collection << {
column_id: column.id,
list_items: column.repository_list_items
.limit(Constants::SEARCH_LIMIT)
.pluck(:id, :data)
.map { |li| [li[0], escape_input(li[1])] }
}
end
collection
end
2019-03-07 14:38:34 +08:00
2019-12-10 15:43:26 +08:00
def update_params
params.permit(repository_row: {}, repository_cells: {}).to_h
end
2019-03-07 14:38:34 +08:00
def log_activity(type_of, repository_row)
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
subject: @repository,
team: current_team,
message_items: {
repository_row: repository_row.id,
repository: @repository.id
})
end
2017-06-06 23:35:29 +08:00
end