2023-08-01 17:13:39 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class ResultOrderableElementsController < ApplicationController
|
|
|
|
before_action :load_vars_nested
|
|
|
|
before_action :check_manage_permissions
|
|
|
|
|
|
|
|
def reorder
|
2024-01-24 17:07:59 +08:00
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
params[:result_orderable_element_positions].each do |id, position|
|
|
|
|
result_element = @result.result_orderable_elements.find(id)
|
2023-09-05 17:02:39 +08:00
|
|
|
result_element.insert_at(position)
|
2023-08-01 17:13:39 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2023-09-05 17:02:39 +08:00
|
|
|
log_activity(:result_content_rearranged, @my_module.experiment.project, my_module: @my_module.id)
|
|
|
|
@result.touch
|
|
|
|
|
2023-08-01 17:13:39 +08:00
|
|
|
render json: params[:result_orderable_element_positions], status: :ok
|
2023-09-05 17:02:39 +08:00
|
|
|
rescue ActiveRecord::RecordInvalid
|
|
|
|
render json: { errors: result_element.errors }, status: :conflict
|
2023-08-01 17:13:39 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_vars_nested
|
|
|
|
@result = Result.find_by(id: params[:result_id])
|
|
|
|
return render_404 unless @result
|
|
|
|
|
|
|
|
@my_module = @result.my_module
|
|
|
|
end
|
|
|
|
|
|
|
|
def check_manage_permissions
|
|
|
|
render_403 unless can_manage_result?(@result)
|
|
|
|
end
|
|
|
|
|
|
|
|
def log_activity(type_of, project = nil, message_items = {})
|
|
|
|
default_items = { result: @result.id }
|
|
|
|
message_items = default_items.merge(message_items)
|
|
|
|
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @result,
|
2023-09-05 17:02:39 +08:00
|
|
|
team: @my_module.team,
|
2023-08-01 17:13:39 +08:00
|
|
|
project: project,
|
|
|
|
message_items: message_items)
|
|
|
|
end
|
|
|
|
end
|