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-21 22:09:04 +08:00
|
|
|
include TinyMceHelper
|
2017-04-06 19:03:13 +08:00
|
|
|
include StepsActions
|
2017-01-19 21:13:57 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
before_action :load_vars, only: [:edit, :update, :destroy, :show]
|
|
|
|
before_action :load_vars_nested, only: [:new, :create]
|
|
|
|
before_action :convert_table_contents_to_utf8, only: [:create, :update]
|
|
|
|
|
|
|
|
before_action :check_view_permissions, only: [:show]
|
|
|
|
before_action :check_create_permissions, only: [:new, :create]
|
|
|
|
before_action :check_edit_permissions, only: [:edit, :update]
|
|
|
|
before_action :check_destroy_permissions, only: [:destroy]
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
before_action :update_checklist_item_positions, only: [:create, :update]
|
|
|
|
|
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
|
2016-12-08 18:26:13 +08:00
|
|
|
@step = Step.new(step_params)
|
2017-04-21 22:09:04 +08:00
|
|
|
# gerate a tag that replaces img tag in database
|
|
|
|
@step.description = parse_tiny_mce_asset_to_token(@step.description)
|
2016-02-12 23:52:43 +08:00
|
|
|
@step.completed = false
|
2016-07-21 19:11:15 +08:00
|
|
|
@step.position = @protocol.number_of_steps
|
|
|
|
@step.protocol = @protocol
|
2016-02-12 23:52:43 +08:00
|
|
|
@step.user = current_user
|
|
|
|
@step.last_modified_by = current_user
|
2017-03-22 23:24:48 +08:00
|
|
|
@step.assets.each do |asset|
|
|
|
|
asset.created_by = current_user
|
|
|
|
asset.team = current_team
|
|
|
|
end
|
|
|
|
@step.tables.each do |table|
|
|
|
|
table.created_by = current_user
|
|
|
|
table.team = current_team
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
# Update default checked state
|
|
|
|
@step.checklists.each do |checklist|
|
|
|
|
checklist.checklist_items.each do |checklist_item|
|
|
|
|
checklist_item.checked = false
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if @step.save
|
|
|
|
# Post process all assets
|
|
|
|
@step.assets.each do |asset|
|
2017-01-24 23:57:14 +08:00
|
|
|
asset.post_process_file(@protocol.team)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
2017-04-24 22:22:25 +08:00
|
|
|
# link tiny_mce_assets to the step
|
2017-04-24 18:28:27 +08:00
|
|
|
link_tiny_mce_assets(@step.description, @step)
|
|
|
|
|
2017-04-10 17:21:28 +08:00
|
|
|
create_annotation_notifications(@step)
|
2017-04-24 18:28:27 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
# Generate activity
|
2016-07-21 19:11:15 +08:00
|
|
|
if @protocol.in_module?
|
|
|
|
Activity.create(
|
|
|
|
type_of: :create_step,
|
|
|
|
user: current_user,
|
2016-08-10 19:48:28 +08:00
|
|
|
project: @my_module.experiment.project,
|
2017-04-20 16:56:25 +08:00
|
|
|
experiment: @my_module.experiment,
|
2016-07-21 19:11:15 +08:00
|
|
|
my_module: @my_module,
|
|
|
|
message: t(
|
|
|
|
"activities.create_step",
|
|
|
|
user: current_user.full_name,
|
|
|
|
step: @step.position + 1,
|
|
|
|
step_name: @step.name
|
|
|
|
)
|
2016-02-12 23:52:43 +08:00
|
|
|
)
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
2017-01-24 23:57:14 +08:00
|
|
|
# TODO: Activity for team if step
|
2016-07-21 19:11:15 +08:00
|
|
|
# created in protocol management??
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update protocol timestamp
|
|
|
|
update_protocol_ts(@step)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
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
|
|
|
|
format.json {
|
|
|
|
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
|
|
|
}, status: :bad_request
|
|
|
|
}
|
|
|
|
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
|
2017-04-21 22:09:04 +08:00
|
|
|
@step.description = generate_image_tag_from_token(@step.description)
|
2016-02-12 23:52:43 +08:00
|
|
|
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)
|
|
|
|
|
|
|
|
@step.assign_attributes(step_params_all)
|
|
|
|
@step.last_modified_by = current_user
|
|
|
|
|
2017-03-22 23:24:48 +08:00
|
|
|
@step.assets.each do |asset|
|
|
|
|
asset.created_by = current_user if asset.new_record?
|
|
|
|
asset.last_modified_by = current_user unless asset.new_record?
|
|
|
|
asset.team = current_team
|
|
|
|
end
|
|
|
|
|
|
|
|
@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
|
|
|
|
|
2017-04-25 20:24:05 +08:00
|
|
|
# gerate a tag that replaces img tag in databases
|
|
|
|
@step.description = parse_tiny_mce_asset_to_token(
|
|
|
|
params[:step][:description],
|
|
|
|
@step
|
|
|
|
)
|
2017-04-21 22:09:04 +08:00
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
if @step.save
|
|
|
|
@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|
|
2017-01-24 23:57:14 +08:00
|
|
|
asset.post_process_file(team)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Generate activity
|
2016-07-21 19:11:15 +08:00
|
|
|
if @protocol.in_module?
|
|
|
|
Activity.create(
|
|
|
|
type_of: :edit_step,
|
|
|
|
user: current_user,
|
2016-08-10 19:48:28 +08:00
|
|
|
project: @my_module.experiment.project,
|
2017-04-20 16:56:25 +08:00
|
|
|
experiment: @my_module.experiment,
|
2016-07-21 19:11:15 +08:00
|
|
|
my_module: @my_module,
|
|
|
|
message: t(
|
|
|
|
"activities.edit_step",
|
|
|
|
user: current_user.full_name,
|
|
|
|
step: @step.position + 1,
|
|
|
|
step_name: @step.name
|
|
|
|
)
|
2016-02-12 23:52:43 +08:00
|
|
|
)
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
2017-01-24 23:57:14 +08:00
|
|
|
# TODO: Activity for team if step
|
2016-07-21 19:11:15 +08:00
|
|
|
# updated in protocol management??
|
|
|
|
end
|
|
|
|
|
|
|
|
# Update protocol timestamp
|
|
|
|
update_protocol_ts(@step)
|
|
|
|
|
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
|
|
|
|
|
|
|
|
def destroy
|
2016-10-01 20:51:55 +08:00
|
|
|
if @step.can_destroy?
|
|
|
|
# Update position on other steps of this module
|
|
|
|
@protocol.steps.where('position > ?', @step.position).each do |step|
|
|
|
|
step.position = step.position - 1
|
|
|
|
step.save
|
|
|
|
end
|
|
|
|
|
|
|
|
# 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
|
|
|
|
2016-10-01 20:51:55 +08:00
|
|
|
# Destroy the step
|
|
|
|
@step.destroy(current_user)
|
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
|
|
|
# Update protocol timestamp
|
|
|
|
update_protocol_ts(@step)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-10-01 20:51:55 +08:00
|
|
|
flash[:success] = t(
|
|
|
|
'protocols.steps.destroy.success_flash',
|
|
|
|
step: (@step.position + 1).to_s
|
|
|
|
)
|
|
|
|
else
|
|
|
|
flash[:error] = t(
|
|
|
|
'protocols.steps.destroy.error_flash',
|
|
|
|
step: (@step.position + 1).to_s
|
|
|
|
)
|
|
|
|
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
|
|
|
|
chkItem = ChecklistItem.find_by_id(params["checklistitem_id"])
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if chkItem
|
|
|
|
checked = params[:checked] == "true"
|
2016-07-21 19:11:15 +08:00
|
|
|
protocol = chkItem.checklist.step.protocol
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
authorized = ((checked and can_check_checkbox(protocol)) or (!checked and can_uncheck_checkbox(protocol)))
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
if authorized
|
|
|
|
changed = chkItem.checked != checked
|
|
|
|
chkItem.checked = checked
|
|
|
|
|
|
|
|
if chkItem.save
|
|
|
|
format.json {
|
|
|
|
render json: {}, status: :accepted
|
|
|
|
}
|
|
|
|
|
|
|
|
# Create activity
|
|
|
|
if changed
|
|
|
|
str = checked ? "activities.check_step_checklist_item" :
|
|
|
|
"activities.uncheck_step_checklist_item"
|
|
|
|
completed_items = chkItem.checklist.checklist_items.where(checked: true).count
|
|
|
|
all_items = chkItem.checklist.checklist_items.count
|
|
|
|
message = t(
|
|
|
|
str,
|
|
|
|
user: current_user.full_name,
|
2017-01-19 21:13:57 +08:00
|
|
|
checkbox: smart_annotation_parser(simple_format(chkItem.text)),
|
2016-02-12 23:52:43 +08:00
|
|
|
step: chkItem.checklist.step.position + 1,
|
|
|
|
step_name: chkItem.checklist.step.name,
|
|
|
|
completed: completed_items,
|
|
|
|
all: all_items
|
|
|
|
)
|
|
|
|
|
2016-07-21 19:11:15 +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?
|
|
|
|
Activity.create(
|
|
|
|
user: current_user,
|
2016-08-10 19:48:28 +08:00
|
|
|
project: protocol.my_module.experiment.project,
|
2017-04-20 16:56:25 +08:00
|
|
|
experiment: protocol.my_module.experiment,
|
2016-07-21 19:11:15 +08:00
|
|
|
my_module: protocol.my_module,
|
|
|
|
message: message,
|
|
|
|
type_of: checked ? :check_step_checklist_item : :uncheck_step_checklist_item
|
|
|
|
)
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
else
|
|
|
|
format.json {
|
|
|
|
render json: {}, status: :unprocessable_entity
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
format.json {
|
|
|
|
render json: {}, status: :unauthorized
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
format.json {
|
|
|
|
render json: {}, status: :not_found
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# Complete/uncomplete step
|
|
|
|
def toggle_step_state
|
|
|
|
step = Step.find_by_id(params[:id])
|
|
|
|
|
|
|
|
respond_to do |format|
|
|
|
|
if step
|
2017-05-04 18:41:22 +08:00
|
|
|
completed = params[:completed] == 'true'
|
2016-07-21 19:11:15 +08:00
|
|
|
protocol = step.protocol
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2017-05-04 18:41:22 +08:00
|
|
|
authorized = (
|
|
|
|
(completed and can_complete_step_in_protocol(protocol)) ||
|
|
|
|
(!completed and can_uncomplete_step_in_protocol(protocol))
|
|
|
|
)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
if authorized
|
|
|
|
changed = step.completed != completed
|
|
|
|
step.completed = completed
|
|
|
|
|
|
|
|
# Update completed_on
|
|
|
|
if changed
|
|
|
|
step.completed_on = completed ? Time.current : nil
|
|
|
|
end
|
|
|
|
|
|
|
|
if step.save
|
2017-02-10 21:27:20 +08:00
|
|
|
if protocol.in_module?
|
2017-05-04 18:41:22 +08:00
|
|
|
ready_to_complete = protocol.my_module.check_completness_status
|
2017-02-10 21:27:20 +08:00
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
# Create activity
|
|
|
|
if changed
|
2016-07-21 19:11:15 +08:00
|
|
|
completed_steps = protocol.steps.where(completed: true).count
|
|
|
|
all_steps = protocol.steps.count
|
2017-05-04 18:41:22 +08:00
|
|
|
str = 'activities.uncomplete_step'
|
|
|
|
str = 'activities.complete_step' if completed
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
message = t(
|
|
|
|
str,
|
|
|
|
user: current_user.full_name,
|
|
|
|
step: step.position + 1,
|
|
|
|
step_name: step.name,
|
|
|
|
completed: completed_steps,
|
|
|
|
all: all_steps
|
|
|
|
)
|
|
|
|
|
2016-07-21 19:11:15 +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
|
|
|
|
if protocol.in_module?
|
|
|
|
Activity.create(
|
|
|
|
user: current_user,
|
2016-08-10 19:48:28 +08:00
|
|
|
project: protocol.my_module.experiment.project,
|
2017-04-20 16:56:25 +08:00
|
|
|
experiment: protocol.my_module.experiment,
|
2016-07-21 19:11:15 +08:00
|
|
|
my_module: protocol.my_module,
|
|
|
|
message: message,
|
|
|
|
type_of: completed ? :complete_step : :uncomplete_step
|
|
|
|
)
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
# Create localized title for complete/uncomplete button
|
2017-02-13 22:42:28 +08:00
|
|
|
localized_title = if !completed
|
|
|
|
t('protocols.steps.options.complete_title')
|
|
|
|
else
|
|
|
|
t('protocols.steps.options.uncomplete_title')
|
|
|
|
end
|
2017-02-10 21:27:20 +08:00
|
|
|
format.json do
|
2017-05-04 18:41:22 +08:00
|
|
|
if ready_to_complete && protocol.my_module.uncompleted?
|
2017-02-10 21:27:20 +08:00
|
|
|
render json: {
|
2017-05-04 18:41:22 +08:00
|
|
|
task_ready_to_complete: true,
|
|
|
|
new_title: localized_title
|
|
|
|
}, status: :ok
|
2017-02-10 21:27:20 +08:00
|
|
|
else
|
2017-05-04 18:41:22 +08:00
|
|
|
render json: { new_title: localized_title }, status: :ok
|
2017-02-10 21:27:20 +08:00
|
|
|
end
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
else
|
|
|
|
format.json {
|
|
|
|
render json: {}, status: :unprocessable_entity
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
format.json {
|
|
|
|
render json: {}, status: :unauthorized
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
format.json {
|
|
|
|
render json: {}, status: :not_found
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_up
|
|
|
|
step = Step.find_by_id(params[:id])
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
respond_to do |format|
|
|
|
|
if step
|
|
|
|
if can_reorder_step_in_protocol(step.protocol)
|
|
|
|
if step.position > 0
|
|
|
|
step_down = step.protocol.steps.where(position: step.position - 1).first
|
|
|
|
step.position -= 1
|
|
|
|
step.save
|
|
|
|
|
|
|
|
if step_down
|
|
|
|
step_down.position += 1
|
|
|
|
step_down.save
|
|
|
|
|
|
|
|
# Update protocol timestamp
|
|
|
|
update_protocol_ts(step)
|
|
|
|
|
|
|
|
format.json {
|
|
|
|
render json: { move_direction: "up", step_up_position: step.position, step_down_position: step_down.position },
|
|
|
|
status: :ok
|
|
|
|
}
|
|
|
|
else
|
|
|
|
format.json {
|
|
|
|
render json: {}, status: :forbidden
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
format.json {
|
|
|
|
render json: {}, status: :forbidden
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
|
|
|
format.json {
|
|
|
|
render json: {}, status: :forbidden
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
else
|
2016-07-21 19:11:15 +08:00
|
|
|
format.json {
|
|
|
|
render json: {}, status: :not_found
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def move_down
|
|
|
|
step = Step.find_by_id(params[:id])
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
respond_to do |format|
|
|
|
|
if step
|
|
|
|
if can_reorder_step_in_protocol(step.protocol)
|
|
|
|
if step.position < step.protocol.steps.count - 1
|
|
|
|
step_up = step.protocol.steps.where(position: step.position + 1).first
|
|
|
|
step.position += 1
|
|
|
|
step.save
|
|
|
|
|
|
|
|
if step_up
|
|
|
|
step_up.position -= 1
|
|
|
|
step_up.save
|
|
|
|
|
|
|
|
# Update protocol timestamp
|
|
|
|
update_protocol_ts(step)
|
|
|
|
|
|
|
|
format.json {
|
|
|
|
render json: { move_direction: "down", step_up_position: step_up.position, step_down_position: step.position },
|
|
|
|
status: :ok
|
|
|
|
}
|
|
|
|
else
|
|
|
|
format.json {
|
|
|
|
render json: {}, status: :forbidden
|
|
|
|
}
|
|
|
|
end
|
|
|
|
else
|
|
|
|
format.json {
|
|
|
|
render json: {}, status: :forbidden
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
else
|
|
|
|
format.json {
|
|
|
|
render json: {}, status: :forbidden
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
else
|
2016-07-21 19:11:15 +08:00
|
|
|
format.json {
|
|
|
|
render json: {}, status: :not_found
|
|
|
|
}
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def update_checklist_item_positions
|
|
|
|
if params["step"].present? && params["step"]["checklists_attributes"].present?
|
|
|
|
params["step"]["checklists_attributes"].values.each do |cla|
|
|
|
|
if cla["checklist_items_attributes"].present?
|
|
|
|
cla["checklist_items_attributes"].each do |idx, item|
|
|
|
|
item["position"] = idx
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
# 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)
|
|
|
|
@step.update_attributes(update_params) unless update_params.empty?
|
|
|
|
end
|
|
|
|
|
2016-09-01 19:38:09 +08:00
|
|
|
# Delete the step table
|
|
|
|
def delete_step_tables(params)
|
|
|
|
return unless params[:tables_attributes].present?
|
|
|
|
params[:tables_attributes].each do |table|
|
2016-09-01 19:49:12 +08:00
|
|
|
next unless table.second['_destroy']
|
2016-09-01 19:38:09 +08:00
|
|
|
table_to_destroy = Table.find_by(id: table.second['id'])
|
|
|
|
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.
|
|
|
|
def has_destroy_params(params)
|
|
|
|
for key, values in params do
|
|
|
|
if values.respond_to?(:each)
|
|
|
|
for pos, attrs in params[key] do
|
2016-08-18 02:49:20 +08:00
|
|
|
if attrs[:_destroy] == '1'
|
|
|
|
return true
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
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)
|
|
|
|
for key, values in params do
|
|
|
|
if values.respond_to?(:each)
|
|
|
|
update_params[key] = {} unless update_params[key]
|
|
|
|
attr_params = update_params[key]
|
|
|
|
|
|
|
|
for pos, attrs in params[key] do
|
2016-08-18 02:49:20 +08:00
|
|
|
if attrs[:_destroy] == '1'
|
2016-09-07 05:05:42 +08:00
|
|
|
if attrs[:id].present?
|
2016-10-02 00:47:05 +08:00
|
|
|
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' }
|
|
|
|
end
|
2016-09-07 05:05:42 +08:00
|
|
|
end
|
2016-08-17 15:44:53 +08:00
|
|
|
params[key].delete(pos)
|
2016-08-05 23:00:29 +08:00
|
|
|
elsif has_destroy_params(params[key][pos])
|
2016-08-18 02:49:20 +08:00
|
|
|
attr_params[pos] = { id: attrs[:id] }
|
2016-08-05 23:00:29 +08:00
|
|
|
extract_destroy_params(params[key][pos], attr_params[pos])
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_vars
|
|
|
|
@step = Step.find_by_id(params[:id])
|
2016-07-21 19:11:15 +08:00
|
|
|
@protocol = @step.protocol
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
unless @protocol
|
2016-02-12 23:52:43 +08:00
|
|
|
render_404
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
|
|
|
if @protocol.in_module?
|
|
|
|
@my_module = @protocol.my_module
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def load_vars_nested
|
2016-07-21 19:11:15 +08:00
|
|
|
@protocol = Protocol.find_by_id(params[:protocol_id])
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
unless @protocol
|
2016-02-12 23:52:43 +08:00
|
|
|
render_404
|
|
|
|
end
|
2016-07-21 19:11:15 +08:00
|
|
|
|
|
|
|
if @protocol.in_module?
|
|
|
|
@my_module = @protocol.my_module
|
|
|
|
end
|
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
|
|
|
|
|
2016-07-21 19:11:15 +08:00
|
|
|
def update_protocol_ts(step)
|
|
|
|
if step.present? && step.protocol.present?
|
|
|
|
step.protocol.update(updated_at: Time.now)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def check_view_permissions
|
2016-07-21 19:11:15 +08:00
|
|
|
unless can_view_steps_in_protocol(@protocol)
|
2016-02-12 23:52:43 +08:00
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_create_permissions
|
2016-07-21 19:11:15 +08:00
|
|
|
unless can_create_step_in_protocol(@protocol)
|
2016-02-12 23:52:43 +08:00
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_edit_permissions
|
2016-07-21 19:11:15 +08:00
|
|
|
unless can_edit_step_in_protocol(@protocol)
|
2016-02-12 23:52:43 +08:00
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_destroy_permissions
|
2016-07-21 19:11:15 +08:00
|
|
|
unless can_delete_step_in_protocol(@protocol)
|
2016-02-12 23:52:43 +08:00
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def step_params
|
|
|
|
params.require(:step).permit(
|
|
|
|
:name,
|
|
|
|
:description,
|
|
|
|
checklists_attributes: [
|
|
|
|
:id,
|
|
|
|
:name,
|
|
|
|
:_destroy,
|
|
|
|
checklist_items_attributes: [
|
|
|
|
:id,
|
|
|
|
:text,
|
|
|
|
:position,
|
|
|
|
:_destroy
|
|
|
|
]
|
|
|
|
],
|
|
|
|
assets_attributes: [
|
|
|
|
:id,
|
|
|
|
:file,
|
|
|
|
:_destroy
|
|
|
|
],
|
|
|
|
tables_attributes: [
|
|
|
|
:id,
|
2017-01-17 00:11:08 +08:00
|
|
|
:name,
|
2016-02-12 23:52:43 +08:00
|
|
|
:contents,
|
|
|
|
:_destroy
|
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|