scinote-web/app/controllers/steps_controller.rb

341 lines
10 KiB
Ruby
Raw Normal View History

2016-02-12 23:52:43 +08:00
class StepsController < ApplicationController
include ActionView::Helpers::TextHelper
include ApplicationHelper
2017-04-06 19:03:13 +08:00
include StepsActions
include MarvinJsActions
2022-10-24 19:07:10 +08:00
before_action :load_vars, only: %i(update destroy show toggle_step_state update_view_state
update_asset_view_mode elements
2022-08-23 23:54:33 +08:00
attachments upload_attachment duplicate)
2022-10-24 19:12:28 +08:00
before_action :load_vars_nested, only: %i(create index reorder)
before_action :convert_table_contents_to_utf8, only: %i(create update)
2016-02-12 23:52:43 +08:00
before_action :check_protocol_manage_permissions, only: %i(reorder)
before_action :check_view_permissions, only: %i(show index attachments elements)
2022-10-24 19:07:10 +08:00
before_action :check_create_permissions, only: %i(create)
before_action :check_manage_permissions, only: %i(update destroy
2022-05-19 14:51:39 +08:00
update_view_state update_asset_view_mode upload_attachment)
2022-10-24 19:07:10 +08:00
before_action :check_complete_and_checkbox_permissions, only: %i(toggle_step_state)
2016-02-12 23:52:43 +08:00
def index
render json: @protocol.steps.in_order, each_serializer: StepSerializer, user: current_user
end
def elements
2022-05-11 17:18:40 +08:00
render json: @step.step_orderable_elements.order(:position),
each_serializer: StepOrderableElementSerializer,
user: current_user
end
def attachments
render json: @step.assets,
each_serializer: AssetSerializer,
user: current_user
end
2022-05-19 14:51:39 +08:00
def upload_attachment
@step.transaction do
@asset = @step.assets.create!(
created_by: current_user,
last_modified_by: current_user,
team: @protocol.team,
view_mode: @step.assets_view_mode
)
2022-05-19 14:51:39 +08:00
@asset.file.attach(params[:signed_blob_id])
@asset.post_process_file
default_message_items = {
step: @step.id,
step_position: { id: @step.id,
value_for: 'position_plus_one' }
}
if @protocol.in_module?
log_activity(
:task_step_file_added,
@my_module.experiment.project,
{
file: @asset.file_name,
my_module: @my_module.id
}.merge(default_message_items)
)
else
log_activity(
:protocol_step_file_added,
nil,
{
file: @asset.file_name,
protocol: @protocol.id
}.merge(default_message_items)
)
end
2022-05-19 14:51:39 +08:00
end
render json: @asset,
serializer: AssetSerializer,
user: current_user
end
2016-02-12 23:52:43 +08:00
def create
2022-04-28 17:13:38 +08:00
@step = Step.new(
name: t('protocols.steps.default_name'),
completed: false,
user: current_user,
last_modified_by: current_user
)
2022-04-28 17:13:38 +08:00
@step = @protocol.insert_step(@step, params[:position])
if @protocol.in_repository? && @protocol.errors.present?
return render json: { error: @protocol.errors }, status: :unprocessable_entity
end
2022-04-28 17:13:38 +08:00
# Generate activity
if @protocol.in_module?
log_activity(:create_step, @my_module.experiment.project, { my_module: @my_module.id }.merge(step_message_items))
2022-04-28 17:13:38 +08:00
else
log_activity(:add_step_to_protocol_repository, nil, { protocol: @protocol.id }.merge(step_message_items))
2022-04-28 17:13:38 +08:00
end
render json: @step, serializer: StepSerializer, user: current_user
end
2016-02-12 23:52:43 +08:00
def show
render json: {
html: render_to_string(
partial: 'steps/step',
locals: { step: @step },
formats: :html
)
}
2016-02-12 23:52:43 +08:00
end
def update
2022-04-28 17:13:38 +08:00
if @step.update(step_params)
# Generate activity
if @protocol.in_module?
log_activity(
:edit_step, @my_module.experiment.project,
{ my_module: @my_module.id }.merge(step_message_items)
)
2022-04-28 17:13:38 +08:00
else
log_activity(
:edit_step_in_protocol_repository,
nil,
{ protocol: @protocol.id }.merge(step_message_items)
)
2022-04-28 17:13:38 +08:00
end
render json: @step, serializer: StepSerializer, user: current_user
2022-04-28 17:13:38 +08:00
else
render json: @protocol.errors.present? ? { errors: @protocol.errors } : {}, status: :unprocessable_entity
2022-04-28 17:13:38 +08:00
end
end
2022-08-23 23:54:33 +08:00
def duplicate
ActiveRecord::Base.transaction do
position = @step.position
@protocol.steps.where('position > ?', position).order(position: :desc).each do |step|
step.update(position: step.position + 1)
end
new_step = @step.duplicate(@protocol, current_user, step_position: position + 1, step_name: @step.name + ' (1)')
if @protocol.in_module?
log_activity(
:task_step_duplicated, @my_module.experiment.project,
{ my_module: @my_module.id }.merge(step_message_items)
)
else
log_activity(
:protocol_step_duplicated,
nil,
{ protocol: @protocol.id }.merge(step_message_items)
)
end
2022-08-23 23:54:33 +08:00
render json: new_step, serializer: StepSerializer, user: current_user
end
rescue ActiveRecord::RecordInvalid
head :unprocessable_entity
end
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?
render json: {}, status: :ok
end
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
render json: { view_mode: @step.assets_view_mode }, status: :ok
2020-10-29 23:21:10 +08:00
rescue ActiveRecord::RecordInvalid => e
Rails.logger.error(e.message)
2020-10-29 23:21:10 +08:00
render json: { errors: e.message }, status: :unprocessable_entity
end
2016-02-12 23:52:43 +08:00
def destroy
if @step.can_destroy?
# Calculate space taken by this step
team = @protocol.team
previous_size = @step.space_taken
2016-02-12 23:52:43 +08:00
# Generate activity
if @protocol.in_module?
log_activity(
:destroy_step,
@my_module.experiment.project,
{ my_module: @my_module.id }.merge(step_message_items)
)
else
log_activity(:delete_step_in_protocol_repository, nil, { protocol: @protocol.id }.merge(step_message_items))
end
2019-03-09 00:01:59 +08:00
# Destroy the step
@step.destroy
2016-02-12 23:52:43 +08:00
# Release space taken by the step
team.release_space(previous_size)
team.save
end
2016-07-21 19:11:15 +08:00
render json: @step, serializer: StepSerializer, user: current_user
2016-02-12 23:52:43 +08:00
end
# Complete/uncomplete step
def toggle_step_state
2022-04-28 17:13:38 +08:00
@step.completed = params[:completed] == 'true'
@step.last_modified_by = current_user
if @step.save
# Create activity
if @step.saved_change_to_completed
completed_steps = @protocol.steps.where(completed: true).count
all_steps = @protocol.steps.count
type_of = @step.completed ? :complete_step : :uncomplete_step
# 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?
log_activity(
type_of,
@protocol.my_module.experiment.project,
{
my_module: @my_module.id,
num_completed: completed_steps.to_s,
num_all: all_steps.to_s
}.merge(step_message_items)
)
end
2016-02-12 23:52:43 +08:00
end
render json: @step, serializer: StepSerializer, user: current_user
2022-04-28 17:13:38 +08:00
else
render json: {}, status: :unprocessable_entity
2016-02-12 23:52:43 +08:00
end
end
def reorder
@protocol.with_lock do
params[:step_positions].each do |id, position|
@protocol.steps.find(id).update_column(:position, position)
end
if @protocol.in_module?
log_activity(:task_steps_rearranged, @my_module.experiment.project, my_module: @my_module.id)
else
log_activity(:protocol_steps_rearranged, nil, protocol: @protocol.id)
end
@protocol.touch
end
render json: {
steps_order: @protocol.steps.order(:position).select(:id, :position)
}
end
2016-02-12 23:52:43 +08:00
private
def load_vars
@step = Step.find_by(id: params[:id])
return render_404 unless @step
2016-07-21 19:11:15 +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
@protocol = Protocol.find_by(id: params[:protocol_id])
2016-02-12 23:52:43 +08:00
return render_404 unless @protocol
2016-07-21 19:11:15 +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
def check_view_permissions
render_403 unless can_read_protocol_in_module?(@protocol) || can_read_protocol_in_repository?(@protocol)
2016-02-12 23:52:43 +08:00
end
def check_protocol_manage_permissions
render_403 unless can_manage_protocol_in_module?(@protocol) || can_manage_protocol_draft_in_repository?(@protocol)
end
def check_manage_permissions
render_403 unless can_manage_step?(@step)
end
def check_create_permissions
if @my_module
render_403 unless can_manage_my_module_steps?(@my_module)
else
render_403 unless can_manage_protocol_draft_in_repository?(@protocol)
end
2016-02-12 23:52:43 +08:00
end
def check_complete_and_checkbox_permissions
render_403 unless can_complete_or_checkbox_step?(@protocol)
end
2016-02-12 23:52:43 +08:00
def step_params
2022-04-28 17:13:38 +08:00
params.require(:step).permit(:name)
end
2019-03-21 04:34:47 +08:00
def log_activity(type_of, project = nil, message_items = {})
2019-03-09 00:01:59 +08:00
Activities::CreateActivityService
.call(activity_type: type_of,
owner: current_user,
subject: @protocol,
team: @protocol.team,
project: project,
2019-03-21 04:34:47 +08:00
message_items: message_items)
2019-03-09 00:01:59 +08:00
end
def step_message_items
{
step: {
id: @step.id,
value_for: 'name'
},
step_position: {
id: @step.id,
value_for: 'position_plus_one'
}
}
end
2016-02-12 23:52:43 +08:00
end