2023-08-03 22:03:40 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-09-06 22:16:39 +08:00
|
|
|
class ResultsController < ApplicationController
|
2023-08-04 23:06:32 +08:00
|
|
|
skip_before_action :verify_authenticity_token, only: %i(create destroy)
|
|
|
|
|
|
|
|
before_action :load_my_module
|
2023-08-11 20:48:20 +08:00
|
|
|
before_action :load_vars, only: %i(destroy elements assets upload_attachment update_view_state update_asset_view_mode )
|
2023-08-03 22:03:40 +08:00
|
|
|
before_action :check_destroy_permissions, only: :destroy
|
|
|
|
|
|
|
|
def index
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
# API endpoint
|
|
|
|
render(
|
2023-08-04 23:06:32 +08:00
|
|
|
json: apply_sort(@my_module.results),
|
2023-08-03 22:03:40 +08:00
|
|
|
formats: :json
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
format.html do
|
|
|
|
# Main view
|
|
|
|
@experiment = @my_module.experiment
|
|
|
|
@project = @experiment.project
|
|
|
|
render(:index, formats: :html)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2016-09-06 22:16:39 +08:00
|
|
|
|
2023-08-04 23:06:32 +08:00
|
|
|
def create
|
|
|
|
result = @my_module.results.create!(user: current_user)
|
|
|
|
|
|
|
|
render json: result
|
|
|
|
end
|
|
|
|
|
2023-08-07 21:23:02 +08:00
|
|
|
def elements
|
|
|
|
render json: @result.result_orderable_elements.order(:position),
|
|
|
|
each_serializer: ResultOrderableElementSerializer,
|
|
|
|
user: current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def assets
|
|
|
|
render json: @result.assets,
|
|
|
|
each_serializer: AssetSerializer,
|
|
|
|
user: current_user
|
|
|
|
end
|
|
|
|
|
2023-08-11 20:48:20 +08:00
|
|
|
def upload_attachment
|
|
|
|
@result.transaction do
|
|
|
|
@asset = @result.assets.create!(
|
|
|
|
created_by: current_user,
|
|
|
|
last_modified_by: current_user,
|
|
|
|
team: @my_module.team,
|
|
|
|
view_mode: @result.assets_view_mode
|
|
|
|
)
|
|
|
|
@asset.file.attach(params[:signed_blob_id])
|
|
|
|
@asset.post_process_file(@my_module.team)
|
|
|
|
end
|
|
|
|
|
|
|
|
render json: @asset,
|
|
|
|
serializer: AssetSerializer,
|
|
|
|
user: current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_view_state
|
|
|
|
view_state = @result.current_view_state(current_user)
|
|
|
|
view_state.state['result_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
|
|
|
|
@result.assets_view_mode = params[:assets_view_mode]
|
|
|
|
@result.save!(touch: false)
|
|
|
|
@result.assets.update_all(view_mode: @result.assets_view_mode)
|
|
|
|
end
|
|
|
|
render json: { view_mode: @result.assets_view_mode }, status: :ok
|
|
|
|
rescue ActiveRecord::RecordInvalid => e
|
|
|
|
Rails.logger.error(e.message)
|
|
|
|
render json: { errors: e.message }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
|
2016-09-06 22:16:39 +08:00
|
|
|
def destroy
|
2019-03-14 02:05:29 +08:00
|
|
|
result_type = if @result.is_text
|
|
|
|
t('activities.result_type.text')
|
|
|
|
elsif @result.is_table
|
|
|
|
t('activities.result_type.table')
|
|
|
|
elsif @result.is_asset
|
|
|
|
t('activities.result_type.asset')
|
|
|
|
end
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: :destroy_result,
|
|
|
|
owner: current_user,
|
|
|
|
subject: @result,
|
2022-11-24 22:19:17 +08:00
|
|
|
team: @my_module.team,
|
|
|
|
project: @my_module.project,
|
2019-03-14 02:05:29 +08:00
|
|
|
message_items: { result: @result.id,
|
2019-03-26 18:39:54 +08:00
|
|
|
type_of_result: result_type })
|
2016-09-06 22:16:39 +08:00
|
|
|
flash[:success] = t('my_modules.module_archive.delete_flash',
|
|
|
|
result: @result.name,
|
|
|
|
module: @my_module.name)
|
|
|
|
@result.destroy
|
|
|
|
redirect_to archive_my_module_path(@my_module)
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
2023-08-04 23:06:32 +08:00
|
|
|
def apply_sort(results)
|
|
|
|
case params[:sort]
|
|
|
|
when 'updated_at_asc'
|
|
|
|
results.order(updated_at: :asc)
|
|
|
|
when 'updated_at_desc'
|
|
|
|
results.order(updated_at: :desc)
|
|
|
|
when 'created_at_asc'
|
|
|
|
results.order(created_at: :asc)
|
|
|
|
when 'created_at_desc'
|
|
|
|
results.order(created_at: :desc)
|
|
|
|
when 'name_asc'
|
|
|
|
results.order(name: :asc)
|
|
|
|
when 'name_desc'
|
|
|
|
results.order(name: :desc)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_my_module
|
|
|
|
@my_module = MyModule.readable_by_user(current_user).find(params[:my_module_id])
|
|
|
|
end
|
|
|
|
|
2016-09-06 22:16:39 +08:00
|
|
|
def load_vars
|
2023-08-07 21:23:02 +08:00
|
|
|
@result = @my_module.results.find(params[:id])
|
2023-08-03 22:03:40 +08:00
|
|
|
|
2016-09-13 19:57:31 +08:00
|
|
|
return render_403 unless @result
|
2023-08-03 22:03:40 +08:00
|
|
|
|
2016-09-06 22:16:39 +08:00
|
|
|
@my_module = @result.my_module
|
|
|
|
end
|
|
|
|
|
2018-02-16 01:46:29 +08:00
|
|
|
def check_destroy_permissions
|
2021-01-20 16:00:45 +08:00
|
|
|
render_403 unless can_delete_result?(@result)
|
2016-09-06 22:16:39 +08:00
|
|
|
end
|
|
|
|
end
|