scinote-web/app/controllers/my_module_repositories_controller.rb

193 lines
7.1 KiB
Ruby
Raw Normal View History

2020-04-07 01:52:41 +08:00
# frozen_string_literal: true
class MyModuleRepositoriesController < ApplicationController
include ApplicationHelper
before_action :load_my_module
before_action :load_repository, except: %i(repositories_dropdown_list repositories_list_html)
before_action :check_my_module_view_permissions
before_action :check_repository_view_permissions, except: %i(repositories_dropdown_list repositories_list_html)
before_action :check_assign_repository_records_permissions, only: :update
2020-04-07 01:52:41 +08:00
def index_dt
2020-04-07 01:52:41 +08:00
@draw = params[:draw].to_i
per_page = params[:length].to_i < 1 ? Constants::REPOSITORY_DEFAULT_PAGE_SIZE : params[:length].to_i
2020-04-07 01:52:41 +08:00
page = (params[:start].to_i / per_page) + 1
datatable_service = RepositoryDatatableService.new(@repository, params, current_user, @my_module)
2020-04-07 18:36:01 +08:00
@datatable_params = {
view_mode: params[:view_mode],
my_module: @my_module
2020-04-07 18:36:01 +08:00
}
2020-04-07 01:52:41 +08:00
@all_rows_count = datatable_service.all_count
2020-04-08 03:02:16 +08:00
@columns_mappings = datatable_service.mappings
2020-05-12 22:26:44 +08:00
if params[:simple_view]
repository_rows = datatable_service.repository_rows
rows_view = 'repository_rows/simple_view_index.json'
else
repository_rows = datatable_service.repository_rows.preload(:repository_columns,
:created_by,
repository_cells: @repository.cell_preload_includes)
rows_view = 'repository_rows/index.json'
end
@repository_rows = repository_rows.page(page).per(per_page)
render rows_view
2020-04-07 01:52:41 +08:00
end
def update
2020-05-08 18:28:37 +08:00
service = RepositoryRows::MyModuleAssignUnassignService.call(my_module: @my_module,
repository: @repository,
user: current_user,
params: params)
if service.succeed? &&
(service.assigned_rows_count.positive? ||
service.unassigned_rows_count.positive?)
flash = update_flash_message(service)
status = :ok
else
flash = t('my_modules.repository.flash.update_error')
status = :bad_request
end
respond_to do |format|
format.json do
render json: {
flash: flash,
rows_count: @my_module.repository_rows_count(@repository),
repository_id: @repository.repository_snapshots.find_by(selected: true)&.id || @repository.id
}, status: status
end
end
end
def update_repository_records_modal
modal = render_to_string(
partial: 'my_modules/modals/update_repository_records_modal_content.html.erb',
locals: { my_module: @my_module,
repository: @repository,
2020-04-29 01:12:17 +08:00
selected_rows: params[:selected_rows] }
)
render json: {
html: modal,
2020-04-23 22:36:22 +08:00
update_url: my_module_repository_path(@my_module, @repository)
}, status: :ok
end
def assign_repository_records_modal
modal = render_to_string(
partial: 'my_modules/modals/assign_repository_records_modal_content.html.erb',
locals: { my_module: @my_module,
repository: @repository,
2020-04-29 01:12:17 +08:00
selected_rows: params[:selected_rows] }
)
render json: {
html: modal,
2020-04-23 22:36:22 +08:00
update_url: my_module_repository_path(@my_module, @repository)
}, status: :ok
end
def repositories_list_html
@assigned_repositories = @my_module.live_and_snapshot_repositories_list
2020-05-08 23:01:55 +08:00
render json: {
html: render_to_string(partial: 'my_modules/repositories/repositories_list'),
assigned_rows_count: @assigned_repositories.map(&:assigned_rows_count).sum
2020-05-08 23:01:55 +08:00
}
end
2020-04-08 03:02:16 +08:00
def full_view_table
render json: {
html: render_to_string(partial: 'my_modules/repositories/full_view_table')
}
2020-04-08 03:02:16 +08:00
end
def repositories_dropdown_list
@repositories = Repository.accessible_by_teams(current_team).joins("
LEFT OUTER JOIN repository_rows ON
repository_rows.repository_id = repositories.id
LEFT OUTER JOIN my_module_repository_rows ON
my_module_repository_rows.repository_row_id = repository_rows.id
AND my_module_repository_rows.my_module_id = #{@my_module.id}
").select('COUNT(DISTINCT my_module_repository_rows.id) as rows_count, repositories.*')
.group(:id)
.having('COUNT(my_module_repository_rows.id) > 0 OR repositories.archived = FALSE')
.order(:name)
2020-04-09 21:14:55 +08:00
render json: { html: render_to_string(partial: 'my_modules/repositories/repositories_dropdown_list') }
end
2020-07-17 21:17:09 +08:00
def export_repository
if params[:header_ids]
RepositoryZipExport.generate_zip(params, @repository, current_user)
Activities::CreateActivityService.call(
activity_type: :export_inventory_items_assigned_to_task,
owner: current_user,
subject: @my_module,
team: current_team,
message_items: {
my_module: @my_module.id,
repository: @repository.id
}
)
2020-07-17 21:17:09 +08:00
render json: { message: t('zip_export.export_request_success') }, status: :ok
else
render json: { message: t('zip_export.export_error') }, status: :unprocessable_entity
end
end
2020-04-07 01:52:41 +08:00
private
2020-04-09 21:14:55 +08:00
def load_my_module
2020-04-15 00:17:54 +08:00
@my_module = MyModule.find_by(id: params[:my_module_id])
2020-04-09 21:14:55 +08:00
render_404 unless @my_module
end
def load_repository
@repository = Repository.find_by(id: params[:id])
2020-04-15 00:17:54 +08:00
render_404 unless @repository
2020-04-09 21:14:55 +08:00
end
2020-04-07 01:52:41 +08:00
2020-04-09 21:14:55 +08:00
def check_my_module_view_permissions
render_403 unless can_read_experiment?(@my_module.experiment)
2020-04-07 01:52:41 +08:00
end
2020-04-09 21:14:55 +08:00
def check_repository_view_permissions
render_403 unless can_read_repository?(@repository)
2020-04-07 01:52:41 +08:00
end
def check_assign_repository_records_permissions
2020-04-29 01:12:17 +08:00
render_403 unless can_assign_repository_rows_to_module?(@my_module)
end
2020-05-08 18:28:37 +08:00
def update_flash_message(service)
assigned_count = service.assigned_rows_count
unassigned_count = service.unassigned_rows_count
if params[:downstream] == 'true'
if assigned_count && unassigned_count
t('my_modules.repository.flash.assign_and_unassign_from_task_and_downstream_html',
assigned_items: assigned_count,
2020-04-29 21:17:15 +08:00
unassigned_items: unassigned_count)
elsif assigned_count
t('my_modules.repository.flash.assign_to_task_and_downstream_html',
assigned_items: assigned_count)
elsif unassigned_count
t('my_modules.repository.flash.unassign_from_task_and_downstream_html',
unassigned_items: unassigned_count)
end
elsif assigned_count && unassigned_count
t('my_modules.repository.flash.assign_and_unassign_from_task_html',
assigned_items: assigned_count,
unassigned_items: unassigned_count)
elsif assigned_count
t('my_modules.repository.flash.assign_to_task_html',
assigned_items: assigned_count)
elsif unassigned_count
t('my_modules.repository.flash.unassign_from_task_html',
unassigned_items: unassigned_count)
end
end
2020-04-07 01:52:41 +08:00
end