2016-02-12 23:52:43 +08:00
|
|
|
class StepsController < ApplicationController
|
2017-01-19 21:13:57 +08:00
|
|
|
include ActionView::Helpers::TextHelper
|
|
|
|
include ApplicationHelper
|
2017-04-06 19:03:13 +08:00
|
|
|
include StepsActions
|
2019-08-09 15:47:07 +08:00
|
|
|
include MarvinJsActions
|
2017-01-19 21:13:57 +08:00
|
|
|
|
2020-07-02 23:03:29 +08:00
|
|
|
before_action :load_vars, only: %i(edit update destroy show toggle_step_state checklistitem_state update_view_state
|
2020-10-28 18:45:47 +08:00
|
|
|
move_up move_down update_asset_view_mode)
|
2020-07-02 23:03:29 +08:00
|
|
|
before_action :load_vars_nested, only: %i(new create)
|
|
|
|
before_action :convert_table_contents_to_utf8, only: %i(create update)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2020-12-08 17:44:52 +08:00
|
|
|
before_action :check_view_permissions, only: :show
|
|
|
|
before_action :check_manage_permissions, only: %i(new create edit update destroy move_up move_down
|
|
|
|
update_view_state update_asset_view_mode)
|
2020-07-02 23:03:29 +08:00
|
|
|
before_action :check_complete_and_checkbox_permissions, only: %i(toggle_step_state checklistitem_state)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
def new
|
|
|
|
@step = Step.new
|
|
|
|
|
|
|
|
respond_to do |format|
|
2016-12-08 18:26:13 +08:00
|
|
|
format.json do
|
2016-02-12 23:52:43 +08:00
|
|
|
render json: {
|
2016-12-08 18:26:13 +08:00
|
|
|
html: render_to_string(partial: 'new.html.erb')
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
2016-12-08 18:26:13 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
2019-07-26 00:00:24 +08:00
|
|
|
@step = Step.new
|
|
|
|
@step.transaction do
|
|
|
|
new_step_params = step_params
|
|
|
|
|
|
|
|
# Attach newly uploaded files, and than remove their blob ids from the parameters
|
|
|
|
new_step_params[:assets_attributes]&.each do |key, value|
|
|
|
|
next unless value[:signed_blob_id]
|
|
|
|
|
|
|
|
asset = Asset.create!(created_by: current_user, last_modified_by: current_user, team: current_team)
|
|
|
|
asset.file.attach(value[:signed_blob_id])
|
|
|
|
@step.assets << asset
|
|
|
|
new_step_params[:assets_attributes].delete(key)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2019-07-26 00:00:24 +08:00
|
|
|
@step.assign_attributes(new_step_params)
|
|
|
|
# gerate a tag that replaces img tag in database
|
|
|
|
@step.completed = false
|
|
|
|
@step.position = @protocol.number_of_steps
|
|
|
|
@step.protocol = @protocol
|
|
|
|
@step.user = current_user
|
|
|
|
@step.last_modified_by = current_user
|
|
|
|
@step.tables.each do |table|
|
|
|
|
table.created_by = current_user
|
|
|
|
table.team = current_team
|
|
|
|
end
|
|
|
|
# Update default checked state
|
|
|
|
@step.checklists.each do |checklist|
|
|
|
|
checklist.checklist_items.each do |checklist_item|
|
|
|
|
checklist_item.checked = false
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
2019-07-26 00:00:24 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-08-09 15:47:07 +08:00
|
|
|
|
|
|
|
# link tiny_mce_assets to the step
|
|
|
|
TinyMceAsset.update_images(@step, params[:tiny_mce_images], current_user)
|
|
|
|
|
2019-07-26 00:00:24 +08:00
|
|
|
@step.save!
|
2017-04-24 18:28:27 +08:00
|
|
|
|
2019-07-26 00:00:24 +08:00
|
|
|
# Post process all assets
|
|
|
|
@step.assets.each do |asset|
|
|
|
|
asset.post_process_file(@protocol.team)
|
|
|
|
end
|
2017-04-24 18:28:27 +08:00
|
|
|
|
2019-07-26 00:00:24 +08:00
|
|
|
# link tiny_mce_assets to the step
|
2019-08-09 21:17:19 +08:00
|
|
|
TinyMceAsset.update_images(@step, params[:tiny_mce_images], current_user)
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2019-07-26 00:00:24 +08:00
|
|
|
create_annotation_notifications(@step)
|
|
|
|
|
|
|
|
# Generate activity
|
|
|
|
if @protocol.in_module?
|
|
|
|
log_activity(:create_step, @my_module.experiment.project, my_module: @my_module.id)
|
|
|
|
else
|
|
|
|
log_activity(:add_step_to_protocol_repository, nil, protocol: @protocol.id)
|
|
|
|
end
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-07-26 00:00:24 +08:00
|
|
|
respond_to do |format|
|
|
|
|
if @step.errors.empty?
|
2016-12-09 20:59:49 +08:00
|
|
|
format.json do
|
2016-02-12 23:52:43 +08:00
|
|
|
render json: {
|
2016-12-09 20:59:49 +08:00
|
|
|
html: render_to_string(
|
|
|
|
partial: 'steps/step.html.erb',
|
|
|
|
locals: { step: @step }
|
|
|
|
)
|
|
|
|
},
|
|
|
|
status: :ok
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
else
|
2017-11-21 20:48:06 +08:00
|
|
|
format.json do
|
|
|
|
render json: @step.errors.to_json, status: :bad_request
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def show
|
|
|
|
respond_to do |format|
|
2016-12-08 18:26:13 +08:00
|
|
|
format.json do
|
2016-02-12 23:52:43 +08:00
|
|
|
render json: {
|
2016-12-08 18:26:13 +08:00
|
|
|
html: render_to_string(
|
|
|
|
partial: 'steps/step.html.erb',
|
|
|
|
locals: { step: @step }
|
|
|
|
)
|
|
|
|
},
|
|
|
|
status: :ok
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
|
|
|
respond_to do |format|
|
2016-12-08 18:26:13 +08:00
|
|
|
format.json do
|
2016-02-12 23:52:43 +08:00
|
|
|
render json: {
|
2016-12-08 18:26:13 +08:00
|
|
|
html: render_to_string(partial: 'edit.html.erb')
|
|
|
|
},
|
|
|
|
status: :ok
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
respond_to do |format|
|
2017-04-06 14:42:16 +08:00
|
|
|
old_description = @step.description
|
|
|
|
old_checklists = fetch_old_checklists_data(@step)
|
|
|
|
new_checklists = fetch_new_checklists_data
|
2016-02-12 23:52:43 +08:00
|
|
|
previous_size = @step.space_taken
|
|
|
|
|
|
|
|
step_params_all = step_params
|
|
|
|
|
|
|
|
# process only destroy update on step references. This prevents
|
|
|
|
# skipping deleting reference in case update validation fails.
|
|
|
|
# NOTE - step_params_all variable is updated
|
|
|
|
destroy_attributes(step_params_all)
|
|
|
|
|
2019-07-26 00:00:24 +08:00
|
|
|
# Attach newly uploaded files, and than remove their blob ids from the parameters
|
2019-12-03 17:17:47 +08:00
|
|
|
new_assets = []
|
2019-07-26 00:00:24 +08:00
|
|
|
step_params_all[:assets_attributes]&.each do |key, value|
|
|
|
|
next unless value[:signed_blob_id]
|
|
|
|
|
2020-10-28 18:45:47 +08:00
|
|
|
new_asset = @step.assets.create!(
|
|
|
|
created_by: current_user,
|
|
|
|
last_modified_by: current_user,
|
|
|
|
team: current_team,
|
|
|
|
view_mode: @step.assets_view_mode
|
|
|
|
)
|
2019-12-03 17:17:47 +08:00
|
|
|
new_asset.file
|
|
|
|
.attach(value[:signed_blob_id])
|
|
|
|
new_assets.push(new_asset.id)
|
2019-07-26 00:00:24 +08:00
|
|
|
step_params_all[:assets_attributes].delete(key)
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
@step.assign_attributes(step_params_all)
|
|
|
|
@step.last_modified_by = current_user
|
|
|
|
|
2017-03-22 23:24:48 +08:00
|
|
|
@step.tables.each do |table|
|
|
|
|
table.created_by = current_user if table.new_record?
|
|
|
|
table.last_modified_by = current_user unless table.new_record?
|
|
|
|
table.team = current_team
|
|
|
|
end
|
2020-08-14 02:06:03 +08:00
|
|
|
|
|
|
|
update_checklist_items_without_callback(step_params_all)
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
if @step.save
|
2019-03-11 20:43:50 +08:00
|
|
|
|
2019-08-09 15:47:07 +08:00
|
|
|
TinyMceAsset.update_images(@step, params[:tiny_mce_images], current_user)
|
2016-02-12 23:52:43 +08:00
|
|
|
@step.reload
|
|
|
|
|
2017-04-06 14:42:16 +08:00
|
|
|
# generates notification on step upadate
|
2017-04-10 17:21:28 +08:00
|
|
|
update_annotation_notifications(@step,
|
|
|
|
old_description,
|
|
|
|
new_checklists,
|
|
|
|
old_checklists)
|
2017-04-06 14:42:16 +08:00
|
|
|
|
2017-01-24 23:57:14 +08:00
|
|
|
# Release team's space taken
|
|
|
|
team = @protocol.team
|
|
|
|
team.release_space(previous_size)
|
|
|
|
team.save
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# Post process step assets
|
|
|
|
@step.assets.each do |asset|
|
2019-12-03 17:17:47 +08:00
|
|
|
asset.post_process_file(team) if new_assets.include? asset.id
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Generate activity
|
2016-07-21 19:11:15 +08:00
|
|
|
if @protocol.in_module?
|
2019-03-29 22:09:20 +08:00
|
|
|
log_activity(:edit_step, @my_module.experiment.project, my_module: @my_module.id)
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
2019-03-29 22:09:20 +08:00
|
|
|
log_activity(:edit_step_in_protocol_repository, nil, protocol: @protocol.id)
|
2016-07-21 19:11:15 +08:00
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
format.json {
|
|
|
|
render json: {
|
|
|
|
html: render_to_string({
|
2016-08-18 02:49:20 +08:00
|
|
|
partial: 'step.html.erb',
|
|
|
|
locals: { step: @step }
|
|
|
|
})
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
else
|
|
|
|
format.json {
|
2016-09-21 21:54:12 +08:00
|
|
|
render json: @step.errors.to_json, status: :bad_request
|
2016-02-12 23:52:43 +08:00
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-06-11 16:08:33 +08:00
|
|
|
def update_view_state
|
|
|
|
view_state = @step.current_view_state(current_user)
|
|
|
|
view_state.state['assets']['sort'] = params.require(:assets).require(:order)
|
|
|
|
view_state.save! if view_state.changed?
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
render json: {}, status: :ok
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-10-28 18:45:47 +08:00
|
|
|
def update_asset_view_mode
|
|
|
|
html = ''
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
@step.assets_view_mode = params[:assets_view_mode]
|
|
|
|
@step.save!(touch: false)
|
2020-10-29 23:21:10 +08:00
|
|
|
@step.assets.update_all(view_mode: @step.assets_view_mode)
|
|
|
|
end
|
|
|
|
@step.assets.each do |asset|
|
2020-11-03 01:47:08 +08:00
|
|
|
html += render_to_string(partial: 'assets/asset.html.erb', locals: {
|
2020-11-03 01:51:21 +08:00
|
|
|
asset: asset,
|
|
|
|
gallery_view_id: @step.id
|
|
|
|
})
|
2020-10-28 18:45:47 +08:00
|
|
|
end
|
|
|
|
render json: { html: html }, status: :ok
|
2020-10-29 23:21:10 +08:00
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
2020-10-28 18:45:47 +08:00
|
|
|
Rails.logger.error(e.message)
|
2020-10-29 23:21:10 +08:00
|
|
|
render json: { errors: e.message }, status: :unprocessable_entity
|
2020-10-28 18:45:47 +08:00
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def destroy
|
2016-10-01 20:51:55 +08:00
|
|
|
if @step.can_destroy?
|
|
|
|
|
|
|
|
# Calculate space taken by this step
|
2017-03-10 23:21:43 +08:00
|
|
|
team = @protocol.team
|
2016-10-01 20:51:55 +08:00
|
|
|
previous_size = @step.space_taken
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-03-27 18:24:44 +08:00
|
|
|
# Generate activity
|
|
|
|
if @protocol.in_module?
|
2019-03-29 22:09:20 +08:00
|
|
|
log_activity(:destroy_step, @my_module.experiment.project, my_module: @my_module.id)
|
2019-03-27 18:24:44 +08:00
|
|
|
else
|
2019-03-29 22:09:20 +08:00
|
|
|
log_activity(:delete_step_in_protocol_repository, nil, protocol: @protocol.id)
|
2019-03-27 18:24:44 +08:00
|
|
|
end
|
2019-03-09 00:01:59 +08:00
|
|
|
|
2016-10-01 20:51:55 +08:00
|
|
|
# Destroy the step
|
2020-07-02 23:03:29 +08:00
|
|
|
@step.destroy
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-10-01 20:51:55 +08:00
|
|
|
# Release space taken by the step
|
2017-03-10 23:21:43 +08:00
|
|
|
team.release_space(previous_size)
|
|
|
|
team.save
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-10-01 20:51:55 +08:00
|
|
|
flash[:success] = t(
|
|
|
|
'protocols.steps.destroy.success_flash',
|
2019-04-03 19:07:30 +08:00
|
|
|
step: (@step.position_plus_one).to_s
|
2016-10-01 20:51:55 +08:00
|
|
|
)
|
|
|
|
else
|
|
|
|
flash[:error] = t(
|
|
|
|
'protocols.steps.destroy.error_flash',
|
2019-04-03 19:07:30 +08:00
|
|
|
step: (@step.position_plus_one).to_s
|
2016-10-01 20:51:55 +08:00
|
|
|
)
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
|
|
|
if @protocol.in_module?
|
|
|
|
redirect_to protocols_my_module_path(@step.my_module)
|
|
|
|
else
|
|
|
|
redirect_to edit_protocol_path(@protocol)
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Responds to checkbox toggling in steps view
|
|
|
|
def checklistitem_state
|
|
|
|
respond_to do |format|
|
2018-02-22 20:51:22 +08:00
|
|
|
checked = params[:checked] == 'true'
|
2018-02-13 23:47:28 +08:00
|
|
|
changed = @chk_item.checked != checked
|
|
|
|
@chk_item.checked = checked
|
|
|
|
|
|
|
|
if @chk_item.save
|
|
|
|
format.json { render json: {}, status: :accepted }
|
|
|
|
|
|
|
|
# Create activity
|
|
|
|
if changed
|
|
|
|
completed_items = @chk_item.checklist.checklist_items
|
2018-02-10 00:05:43 +08:00
|
|
|
.where(checked: true).count
|
2018-02-13 23:47:28 +08:00
|
|
|
all_items = @chk_item.checklist.checklist_items.count
|
|
|
|
text_activity = smart_annotation_parser(@chk_item.text)
|
|
|
|
.gsub(/\s+/, ' ')
|
2019-03-19 23:12:07 +08:00
|
|
|
type_of = if checked
|
|
|
|
:check_step_checklist_item
|
|
|
|
else
|
|
|
|
:uncheck_step_checklist_item
|
|
|
|
end
|
2018-02-13 23:47:28 +08:00
|
|
|
# This should always hold true (only in module can
|
|
|
|
# check items be checked, but still check just in case)
|
|
|
|
if @protocol.in_module?
|
2019-03-21 04:34:47 +08:00
|
|
|
log_activity(type_of,
|
|
|
|
@protocol.my_module.experiment.project,
|
2019-03-29 22:09:20 +08:00
|
|
|
my_module: @my_module.id,
|
2019-03-21 04:34:47 +08:00
|
|
|
step: @chk_item.checklist.step.id,
|
|
|
|
step_position: { id: @chk_item.checklist.step.id,
|
2019-04-03 19:10:45 +08:00
|
|
|
value_for: 'position_plus_one' },
|
2019-03-21 04:34:47 +08:00
|
|
|
checkbox: text_activity,
|
|
|
|
num_completed: completed_items.to_s,
|
|
|
|
num_all: all_items.to_s)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
2018-02-13 23:47:28 +08:00
|
|
|
format.json { render json: {}, status: :unprocessable_entity }
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Complete/uncomplete step
|
|
|
|
def toggle_step_state
|
|
|
|
respond_to do |format|
|
2018-02-09 23:14:40 +08:00
|
|
|
completed = params[:completed] == 'true'
|
2018-02-13 23:47:28 +08:00
|
|
|
changed = @step.completed != completed
|
|
|
|
@step.completed = completed
|
2018-02-09 23:14:40 +08:00
|
|
|
|
2018-02-13 23:47:28 +08:00
|
|
|
if @step.save
|
2018-02-09 23:14:40 +08:00
|
|
|
# Create activity
|
|
|
|
if changed
|
2018-02-13 23:47:28 +08:00
|
|
|
completed_steps = @protocol.steps.where(completed: true).count
|
|
|
|
all_steps = @protocol.steps.count
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-03-19 23:12:07 +08:00
|
|
|
type_of = completed ? :complete_step : :uncomplete_step
|
2018-02-09 23:14:40 +08:00
|
|
|
# Toggling step state can only occur in
|
|
|
|
# module protocols, so my_module is always
|
|
|
|
# not nil; nonetheless, check if my_module is present
|
2018-02-13 23:47:28 +08:00
|
|
|
if @protocol.in_module?
|
2019-03-21 04:34:47 +08:00
|
|
|
log_activity(type_of,
|
|
|
|
@protocol.my_module.experiment.project,
|
2019-03-29 22:09:20 +08:00
|
|
|
my_module: @my_module.id,
|
2019-03-21 04:34:47 +08:00
|
|
|
num_completed: completed_steps.to_s,
|
|
|
|
num_all: all_steps.to_s)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
2018-02-09 23:14:40 +08:00
|
|
|
end
|
2020-10-31 01:47:01 +08:00
|
|
|
format.json { render json: {}, status: :ok }
|
2016-02-12 23:52:43 +08:00
|
|
|
else
|
2018-02-13 23:47:28 +08:00
|
|
|
format.json { render json: {}, status: :unprocessable_entity }
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_up
|
2016-07-21 19:11:15 +08:00
|
|
|
respond_to do |format|
|
2020-07-27 22:14:14 +08:00
|
|
|
format.json do
|
2020-09-07 16:44:00 +08:00
|
|
|
@step.move_up
|
2020-07-27 22:14:14 +08:00
|
|
|
|
2020-09-07 16:44:00 +08:00
|
|
|
render json: {
|
|
|
|
steps_order: @protocol.steps.order(:position).select(:id, :position)
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_down
|
2016-07-21 19:11:15 +08:00
|
|
|
respond_to do |format|
|
2020-07-27 22:14:14 +08:00
|
|
|
format.json do
|
2020-09-07 16:44:00 +08:00
|
|
|
@step.move_down
|
2020-07-27 22:14:14 +08:00
|
|
|
|
2020-09-07 16:44:00 +08:00
|
|
|
render json: {
|
|
|
|
steps_order: @protocol.steps.order(:position).select(:id, :position)
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
# This function is used for partial update of step references and
|
|
|
|
# it's useful when you want to execute destroy action on attribute
|
|
|
|
# collection separately from normal update action, for example if
|
|
|
|
# you don't want that update validation interupt destroy action.
|
|
|
|
# In case of step model you can delete checkboxes, assets or tables.
|
|
|
|
def destroy_attributes(params)
|
|
|
|
update_params = {}
|
2016-09-01 19:38:09 +08:00
|
|
|
delete_step_tables(params)
|
2016-02-12 23:52:43 +08:00
|
|
|
extract_destroy_params(params, update_params)
|
2019-09-12 23:21:48 +08:00
|
|
|
@step.update(update_params) unless update_params.empty?
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2016-09-01 19:38:09 +08:00
|
|
|
# Delete the step table
|
|
|
|
def delete_step_tables(params)
|
|
|
|
return unless params[:tables_attributes].present?
|
2018-02-06 23:15:10 +08:00
|
|
|
params[:tables_attributes].each do |_, table|
|
2018-02-06 00:32:38 +08:00
|
|
|
next unless table['_destroy']
|
|
|
|
table_to_destroy = Table.find_by_id(table['id'])
|
2018-08-21 19:26:16 +08:00
|
|
|
next if table_to_destroy.nil?
|
2016-09-01 19:38:09 +08:00
|
|
|
table_to_destroy.report_elements.destroy_all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
# Checks if hash contains destroy parameter '_destroy' and returns
|
|
|
|
# boolean value.
|
2019-07-26 00:00:24 +08:00
|
|
|
def has_destroy_params?(params)
|
|
|
|
params.each do |key, values|
|
|
|
|
next unless values.respond_to?(:each)
|
|
|
|
|
|
|
|
params[key].each do |_, attrs|
|
|
|
|
return true if attrs[:_destroy] == '1'
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Extracts part of hash that contains destroy parameters. It deletes
|
|
|
|
# values that contains destroy parameters from original variable and
|
|
|
|
# puts them into update_params variable.
|
|
|
|
def extract_destroy_params(params, update_params)
|
2019-07-26 00:00:24 +08:00
|
|
|
params.each do |key, values|
|
|
|
|
next unless values.respond_to?(:each)
|
|
|
|
|
|
|
|
update_params[key] = {} unless update_params[key]
|
|
|
|
attr_params = update_params[key]
|
|
|
|
|
|
|
|
params[key].each do |pos, attrs|
|
|
|
|
if attrs[:_destroy] == '1'
|
|
|
|
if attrs[:id].present?
|
|
|
|
asset = Asset.find_by_id(attrs[:id])
|
|
|
|
if asset.try(&:locked?)
|
|
|
|
asset.errors.add(:base, 'This file is locked.')
|
|
|
|
else
|
|
|
|
attr_params[pos] = { id: attrs[:id], _destroy: '1' }
|
2016-09-07 05:05:42 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
2019-07-26 00:00:24 +08:00
|
|
|
params[key].delete(pos)
|
|
|
|
elsif has_destroy_params?(params[key][pos])
|
|
|
|
attr_params[pos] = { id: attrs[:id] }
|
|
|
|
extract_destroy_params(params[key][pos], attr_params[pos])
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_vars
|
2020-07-02 23:03:29 +08:00
|
|
|
@step = Step.find_by(id: params[:id])
|
|
|
|
return render_404 unless @step
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2020-07-02 23:03:29 +08:00
|
|
|
@protocol = @step.protocol
|
|
|
|
@chk_item = ChecklistItem.find_by(id: params[:checklistitem_id]) if params[:checklistitem_id]
|
|
|
|
@my_module = @protocol.my_module if @protocol.in_module?
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def load_vars_nested
|
2020-07-02 23:03:29 +08:00
|
|
|
@protocol = Protocol.find_by(id: params[:protocol_id])
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2020-07-02 23:03:29 +08:00
|
|
|
return render_404 unless @protocol
|
2016-07-21 19:11:15 +08:00
|
|
|
|
2020-07-02 23:03:29 +08:00
|
|
|
@my_module = @protocol.my_module if @protocol.in_module?
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def convert_table_contents_to_utf8
|
|
|
|
if params.include? :step and
|
|
|
|
params[:step].include? :tables_attributes then
|
|
|
|
params[:step][:tables_attributes].each do |k,v|
|
|
|
|
params[:step][:tables_attributes][k][:contents] =
|
|
|
|
v[:contents].encode(Encoding::UTF_8).force_encoding(Encoding::UTF_8)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2020-08-14 02:06:03 +08:00
|
|
|
def update_checklist_items_without_callback(params)
|
|
|
|
params.dig('checklists_attributes')&.values&.each do |cl|
|
|
|
|
ck = @step.checklists.find_by(id: cl[:id])
|
|
|
|
next if ck.nil? # ck is new checklist, skip update positions
|
|
|
|
|
|
|
|
cl['checklist_items_attributes']&.each do |item|
|
|
|
|
# Here item is somehow array of index and parameters [0, paramteters<Object>], should be fixed on FE also
|
|
|
|
item_record = ck.checklist_items.find_by(id: item[1][:id])
|
2020-08-14 18:32:39 +08:00
|
|
|
|
|
|
|
next unless item_record
|
2020-08-25 21:30:22 +08:00
|
|
|
|
2020-08-14 02:06:03 +08:00
|
|
|
item_record.update_attribute('position', item[1][:position])
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def check_view_permissions
|
2020-07-02 23:03:29 +08:00
|
|
|
render_403 unless can_read_protocol_in_module?(@protocol) || can_read_protocol_in_repository?(@protocol)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2018-02-02 01:41:28 +08:00
|
|
|
def check_manage_permissions
|
2020-07-02 23:03:29 +08:00
|
|
|
render_403 unless can_manage_protocol_in_module?(@protocol) || can_manage_protocol_in_repository?(@protocol)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2018-02-09 23:14:40 +08:00
|
|
|
def check_complete_and_checkbox_permissions
|
2018-02-13 23:47:28 +08:00
|
|
|
render_403 unless can_complete_or_checkbox_step?(@protocol)
|
2018-02-09 23:14:40 +08:00
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def step_params
|
|
|
|
params.require(:step).permit(
|
|
|
|
:name,
|
|
|
|
:description,
|
|
|
|
checklists_attributes: [
|
|
|
|
:id,
|
|
|
|
:name,
|
|
|
|
:_destroy,
|
|
|
|
checklist_items_attributes: [
|
|
|
|
:id,
|
|
|
|
:text,
|
|
|
|
:position,
|
|
|
|
:_destroy
|
|
|
|
]
|
|
|
|
],
|
|
|
|
assets_attributes: [
|
|
|
|
:id,
|
2019-07-26 00:00:24 +08:00
|
|
|
:_destroy,
|
|
|
|
:signed_blob_id
|
2016-02-12 23:52:43 +08:00
|
|
|
],
|
|
|
|
tables_attributes: [
|
|
|
|
:id,
|
2017-01-17 00:11:08 +08:00
|
|
|
:name,
|
2016-02-12 23:52:43 +08:00
|
|
|
:contents,
|
|
|
|
:_destroy
|
2019-05-03 21:24:28 +08:00
|
|
|
],
|
|
|
|
marvin_js_assets_attributes: %i(
|
|
|
|
id
|
|
|
|
_destroy
|
|
|
|
)
|
2016-02-12 23:52:43 +08:00
|
|
|
)
|
|
|
|
end
|
2019-03-09 00:01:59 +08:00
|
|
|
|
2019-03-21 04:34:47 +08:00
|
|
|
def log_activity(type_of, project = nil, message_items = {})
|
2019-03-29 22:09:20 +08:00
|
|
|
default_items = { step: @step.id,
|
2019-04-03 19:10:45 +08:00
|
|
|
step_position: { id: @step.id, value_for: 'position_plus_one' } }
|
2019-03-21 04:34:47 +08:00
|
|
|
message_items = default_items.merge(message_items)
|
|
|
|
|
2019-03-09 00:01:59 +08:00
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @protocol,
|
|
|
|
team: current_team,
|
2019-03-19 23:12:07 +08:00
|
|
|
project: project,
|
2019-03-21 04:34:47 +08:00
|
|
|
message_items: message_items)
|
2019-03-09 00:01:59 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|