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-31 21:50:35 +08:00
|
|
|
include Breadcrumbs
|
2024-02-06 19:59:37 +08:00
|
|
|
include TeamsHelper
|
2023-08-23 21:05:31 +08:00
|
|
|
skip_before_action :verify_authenticity_token, only: %i(create update destroy duplicate)
|
2023-08-04 23:06:32 +08:00
|
|
|
before_action :load_my_module
|
2023-09-06 20:58:59 +08:00
|
|
|
before_action :load_vars, only: %i(destroy elements assets upload_attachment archive restore destroy
|
2023-08-23 21:05:31 +08:00
|
|
|
update_view_state update_asset_view_mode update duplicate)
|
2023-08-03 22:03:40 +08:00
|
|
|
before_action :check_destroy_permissions, only: :destroy
|
2023-08-31 21:50:35 +08:00
|
|
|
before_action :set_breadcrumbs_items, only: %i(index)
|
|
|
|
before_action :set_navigator, only: %i(index)
|
2023-09-19 19:42:56 +08:00
|
|
|
before_action :set_inline_name_editing, only: %i(index)
|
2023-08-03 22:03:40 +08:00
|
|
|
|
|
|
|
def index
|
|
|
|
respond_to do |format|
|
|
|
|
format.json do
|
|
|
|
# API endpoint
|
2023-09-06 20:58:59 +08:00
|
|
|
@results = if params[:view_mode] == 'archived'
|
|
|
|
@my_module.results.archived
|
|
|
|
else
|
|
|
|
@my_module.results.active
|
|
|
|
end
|
2023-08-23 17:59:21 +08:00
|
|
|
|
2024-04-04 15:10:33 +08:00
|
|
|
update_and_apply_user_sort_preference!
|
2023-08-23 17:59:21 +08:00
|
|
|
apply_filters!
|
|
|
|
|
2023-09-12 23:33:47 +08:00
|
|
|
@results = @results.page(params.dig(:page, :number) || 1)
|
2023-08-30 21:47:54 +08:00
|
|
|
|
2024-04-04 15:10:33 +08:00
|
|
|
render json: @results, each_serializer: ResultSerializer, scope: current_user,
|
|
|
|
meta: { sort: @sort_preference }
|
2023-08-03 22:03:40 +08:00
|
|
|
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)
|
2023-09-07 22:34:33 +08:00
|
|
|
log_activity(:add_result, { result: result })
|
2023-08-04 23:06:32 +08:00
|
|
|
render json: result
|
|
|
|
end
|
|
|
|
|
2023-08-17 22:32:42 +08:00
|
|
|
def update
|
|
|
|
@result.update!(result_params)
|
2023-09-07 22:34:33 +08:00
|
|
|
log_activity(:edit_result, { result: @result })
|
2023-08-17 22:32:42 +08:00
|
|
|
render json: @result
|
|
|
|
end
|
|
|
|
|
2023-09-05 15:55:15 +08:00
|
|
|
def archive
|
2023-09-06 20:58:59 +08:00
|
|
|
if @result.archive(current_user)
|
2023-09-07 22:34:33 +08:00
|
|
|
log_activity(:archive_result, { result: @result })
|
2023-09-06 20:58:59 +08:00
|
|
|
render json: {}, status: :ok
|
|
|
|
else
|
|
|
|
render json: { errors: @result.errors.full_messages }, status: :unprocessable_entity
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def restore
|
|
|
|
if @result.restore(current_user)
|
2023-09-07 22:34:33 +08:00
|
|
|
log_activity(:result_restored, { result: @result })
|
2023-09-06 20:58:59 +08:00
|
|
|
render json: {}, status: :ok
|
|
|
|
else
|
|
|
|
render json: { errors: @result.errors.full_messages }, status: :unprocessable_entity
|
2023-09-05 15:55:15 +08:00
|
|
|
end
|
|
|
|
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])
|
2024-02-05 23:24:06 +08:00
|
|
|
@asset.post_process_file
|
2023-08-11 20:48:20 +08:00
|
|
|
end
|
|
|
|
|
2023-09-07 22:34:33 +08:00
|
|
|
log_activity(:result_file_added, { file: @asset.file_name, result: @result })
|
2023-08-11 20:48:20 +08:00
|
|
|
render json: @asset,
|
|
|
|
serializer: AssetSerializer,
|
|
|
|
user: current_user
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_view_state
|
|
|
|
view_state = @result.current_view_state(current_user)
|
2023-09-08 22:07:43 +08:00
|
|
|
view_state.state['assets']['sort'] = params.require(:assets).require(:order)
|
2023-08-11 20:48:20 +08:00
|
|
|
view_state.save! if view_state.changed?
|
|
|
|
|
|
|
|
render json: {}, status: :ok
|
|
|
|
end
|
|
|
|
|
|
|
|
def update_asset_view_mode
|
|
|
|
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
|
2023-09-07 22:34:33 +08:00
|
|
|
name = @result.name
|
2024-06-26 19:37:00 +08:00
|
|
|
if @result.discard
|
2023-09-07 22:34:33 +08:00
|
|
|
log_activity(:destroy_result, { destroyed_result: name })
|
2023-09-06 20:58:59 +08:00
|
|
|
render json: {}, status: :ok
|
|
|
|
else
|
|
|
|
render json: { errors: @result.errors.full_messages }, status: :unprocessable_entity
|
|
|
|
end
|
2016-09-06 22:16:39 +08:00
|
|
|
end
|
|
|
|
|
2023-08-23 21:05:31 +08:00
|
|
|
def duplicate
|
|
|
|
ActiveRecord::Base.transaction do
|
|
|
|
new_result = @result.duplicate(
|
|
|
|
@my_module, current_user, result_name: "#{@result.name} (1)"
|
|
|
|
)
|
|
|
|
|
2023-09-07 22:34:33 +08:00
|
|
|
log_activity(:result_duplicated, { result: @result })
|
2023-08-23 21:05:31 +08:00
|
|
|
render json: new_result, serializer: ResultSerializer, user: current_user
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2016-09-06 22:16:39 +08:00
|
|
|
private
|
|
|
|
|
2023-08-17 22:32:42 +08:00
|
|
|
def result_params
|
|
|
|
params.require(:result).permit(:name)
|
|
|
|
end
|
|
|
|
|
2024-04-04 15:10:33 +08:00
|
|
|
def update_and_apply_user_sort_preference!
|
|
|
|
if params[:sort].present?
|
2024-04-10 22:55:49 +08:00
|
|
|
current_user.update_nested_setting(key: 'results_order', id: @my_module.id.to_s, value: params[:sort])
|
2024-04-04 15:10:33 +08:00
|
|
|
@sort_preference = params[:sort]
|
|
|
|
else
|
|
|
|
@sort_preference = current_user.settings.fetch('results_order', {})[@my_module.id.to_s] || 'created_at_desc'
|
|
|
|
end
|
|
|
|
apply_sort!(@sort_preference)
|
|
|
|
end
|
|
|
|
|
|
|
|
def apply_sort!(sort_order)
|
|
|
|
case sort_order
|
2023-08-04 23:06:32 +08:00
|
|
|
when 'updated_at_asc'
|
2024-03-13 00:21:10 +08:00
|
|
|
@results = @results.order('results.updated_at' => :asc)
|
2023-08-04 23:06:32 +08:00
|
|
|
when 'updated_at_desc'
|
2024-03-13 00:21:10 +08:00
|
|
|
@results = @results.order('results.updated_at' => :desc)
|
2023-08-04 23:06:32 +08:00
|
|
|
when 'created_at_asc'
|
2024-03-13 00:21:10 +08:00
|
|
|
@results = @results.order('results.created_at' => :asc)
|
2023-08-04 23:06:32 +08:00
|
|
|
when 'created_at_desc'
|
2024-03-13 00:21:10 +08:00
|
|
|
@results = @results.order('results.created_at' => :desc)
|
2023-08-04 23:06:32 +08:00
|
|
|
when 'name_asc'
|
2024-03-13 00:21:10 +08:00
|
|
|
@results = @results.order('results.name' => :asc)
|
2023-08-04 23:06:32 +08:00
|
|
|
when 'name_desc'
|
2024-03-13 00:21:10 +08:00
|
|
|
@results = @results.order('results.name' => :desc)
|
2023-08-23 17:59:21 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def apply_filters!
|
|
|
|
if params[:query].present?
|
2024-04-18 21:45:34 +08:00
|
|
|
@results = @results.search(current_user, params[:view_mode] == 'archived', params[:query])
|
|
|
|
.page(params[:page] || 1)
|
|
|
|
.per(Constants::SEARCH_LIMIT)
|
2023-08-04 23:06:32 +08:00
|
|
|
end
|
2023-08-23 17:59:21 +08:00
|
|
|
|
2024-03-13 00:21:10 +08:00
|
|
|
@results = @results.where('results.created_at >= ?', params[:created_at_from]) if params[:created_at_from]
|
|
|
|
@results = @results.where('results.created_at <= ?', params[:created_at_to]) if params[:created_at_to]
|
|
|
|
@results = @results.where('results.updated_at >= ?', params[:updated_at_from]) if params[:updated_at_from]
|
|
|
|
@results = @results.where('results.updated_at <= ?', params[:updated_at_to]) if params[:updated_at_to]
|
2023-08-04 23:06:32 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def load_my_module
|
|
|
|
@my_module = MyModule.readable_by_user(current_user).find(params[:my_module_id])
|
2024-02-06 19:59:37 +08:00
|
|
|
current_team_switch(@my_module.team) if current_team != @my_module.team
|
2023-08-04 23:06:32 +08:00
|
|
|
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
|
2023-08-31 21:50:35 +08:00
|
|
|
|
|
|
|
def set_navigator
|
|
|
|
@navigator = {
|
|
|
|
url: tree_navigator_my_module_path(@my_module),
|
2024-05-17 20:15:04 +08:00
|
|
|
archived: @my_module.archived_branch?,
|
2023-08-31 21:50:35 +08:00
|
|
|
id: @my_module.code
|
|
|
|
}
|
|
|
|
end
|
2023-09-07 22:34:33 +08:00
|
|
|
|
|
|
|
def log_activity(element_type_of, message_items = {})
|
|
|
|
message_items[:my_module] = @my_module.id
|
|
|
|
subject = if message_items.key?(:result)
|
|
|
|
result = message_items[:result]
|
|
|
|
message_items[:result] = result.id
|
|
|
|
result
|
|
|
|
else
|
|
|
|
@my_module
|
|
|
|
end
|
|
|
|
|
|
|
|
Activities::CreateActivityService.call(
|
|
|
|
activity_type: element_type_of,
|
|
|
|
owner: current_user,
|
|
|
|
team: @my_module.team,
|
|
|
|
subject: subject,
|
2023-10-10 22:23:58 +08:00
|
|
|
project: @my_module.experiment.project,
|
2023-09-07 22:34:33 +08:00
|
|
|
message_items: message_items
|
|
|
|
)
|
|
|
|
end
|
2023-09-19 19:42:56 +08:00
|
|
|
|
|
|
|
def set_inline_name_editing
|
|
|
|
return unless can_manage_my_module?(@my_module)
|
|
|
|
|
|
|
|
@inline_editable_title_config = {
|
|
|
|
name: 'title',
|
|
|
|
params_group: 'my_module',
|
|
|
|
item_id: @my_module.id,
|
|
|
|
field_to_udpate: 'name',
|
|
|
|
path_to_update: my_module_path(@my_module)
|
|
|
|
}
|
|
|
|
end
|
2016-09-06 22:16:39 +08:00
|
|
|
end
|