2016-02-12 23:52:43 +08:00
|
|
|
class ResultTablesController < ApplicationController
|
|
|
|
include ResultsHelper
|
|
|
|
|
|
|
|
before_action :load_vars, only: [:edit, :update, :download]
|
|
|
|
before_action :load_vars_nested, only: [:new, :create]
|
|
|
|
before_action :convert_contents_to_utf8, only: [:create, :update]
|
|
|
|
|
2021-09-24 19:11:41 +08:00
|
|
|
before_action :check_manage_permissions, only: %i(edit update)
|
|
|
|
before_action :check_create_permissions, only: %i(new create)
|
2016-02-12 23:52:43 +08:00
|
|
|
before_action :check_archive_permissions, only: [:update]
|
2021-09-24 19:11:41 +08:00
|
|
|
before_action :check_view_permissions, except: %i(new create edit update)
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
def new
|
|
|
|
@table = Table.new
|
|
|
|
@result = Result.new(
|
|
|
|
user: current_user,
|
|
|
|
my_module: @my_module,
|
|
|
|
table: @table
|
|
|
|
)
|
|
|
|
|
2023-07-14 18:30:36 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string({ partial: 'new', formats: :html })
|
|
|
|
}, status: :ok
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def create
|
|
|
|
@table = Table.new(result_params[:table_attributes])
|
2023-07-03 18:35:29 +08:00
|
|
|
@table.metadata = JSON.parse(result_params[:table_attributes][:metadata])
|
2016-02-12 23:52:43 +08:00
|
|
|
@table.created_by = current_user
|
2017-03-22 23:24:48 +08:00
|
|
|
@table.team = current_team
|
2016-02-12 23:52:43 +08:00
|
|
|
@table.last_modified_by = current_user
|
2017-04-18 21:08:13 +08:00
|
|
|
@table.name = nil
|
2016-02-12 23:52:43 +08:00
|
|
|
@result = Result.new(
|
|
|
|
user: current_user,
|
|
|
|
my_module: @my_module,
|
|
|
|
name: result_params[:name],
|
|
|
|
table: @table
|
|
|
|
)
|
|
|
|
@result.last_modified_by = current_user
|
|
|
|
|
2020-07-16 21:42:52 +08:00
|
|
|
if @result.save && @table.save
|
|
|
|
log_activity(:add_result)
|
|
|
|
flash[:success] = t('result_tables.create.success_flash', module: @my_module.name)
|
2020-07-28 21:39:29 +08:00
|
|
|
redirect_to results_my_module_path(@my_module, page: params[:page], order: params[:order])
|
2020-07-16 21:42:52 +08:00
|
|
|
else
|
|
|
|
render json: @result.errors, status: :bad_request
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def edit
|
2023-07-18 19:36:41 +08:00
|
|
|
render json: {
|
|
|
|
html: render_to_string({ partial: 'edit', formats: :html })
|
|
|
|
}, status: :ok
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def update
|
|
|
|
update_params = result_params
|
|
|
|
@result.last_modified_by = current_user
|
|
|
|
@result.table.last_modified_by = current_user
|
2017-03-22 23:24:48 +08:00
|
|
|
@result.table.team = current_team
|
2016-02-12 23:52:43 +08:00
|
|
|
@result.assign_attributes(update_params)
|
2023-07-15 18:33:44 +08:00
|
|
|
@result.table.metadata = JSON.parse(update_params[:table_attributes][:metadata]) if update_params[:table_attributes]
|
2016-02-12 23:52:43 +08:00
|
|
|
flash_success = t("result_tables.update.success_flash",
|
|
|
|
module: @my_module.name)
|
|
|
|
if @result.archived_changed?(from: false, to: true)
|
|
|
|
saved = @result.archive(current_user)
|
|
|
|
flash_success = t("result_tables.archive.success_flash",
|
|
|
|
module: @my_module.name)
|
|
|
|
if saved
|
2019-03-14 02:05:29 +08:00
|
|
|
log_activity(:archive_result)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
elsif @result.archived_changed?(from: true, to: false)
|
|
|
|
render_403
|
|
|
|
else
|
|
|
|
saved = @result.save
|
|
|
|
|
|
|
|
if saved then
|
2019-03-14 02:05:29 +08:00
|
|
|
log_activity(:edit_result)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
respond_to do |format|
|
|
|
|
if saved
|
|
|
|
format.html {
|
|
|
|
flash[:success] = flash_success
|
|
|
|
redirect_to results_my_module_path(@my_module)
|
|
|
|
}
|
|
|
|
format.json {
|
|
|
|
render json: {
|
2023-07-14 18:30:36 +08:00
|
|
|
html: render_to_string(
|
|
|
|
partial: 'my_modules/result',
|
|
|
|
locals: { result: @result },
|
|
|
|
formats: :html
|
|
|
|
)
|
2016-02-12 23:52:43 +08:00
|
|
|
}, status: :ok
|
|
|
|
}
|
|
|
|
else
|
|
|
|
format.json {
|
|
|
|
render json: @result.errors, status: :bad_request
|
|
|
|
}
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def download
|
|
|
|
_ = JSON.parse @result_table.table.contents
|
|
|
|
@table_data = _["data"] || []
|
2023-07-15 18:33:44 +08:00
|
|
|
data = render_to_string partial: 'download'
|
2016-02-12 23:52:43 +08:00
|
|
|
send_data data, filename: @result_table.result.name + '.txt',
|
|
|
|
type: 'plain/text'
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def load_vars
|
|
|
|
@result_table = ResultTable.find_by_id(params[:id])
|
|
|
|
|
|
|
|
if @result_table
|
|
|
|
@result = @result_table.result
|
|
|
|
@my_module = @result.my_module
|
|
|
|
else
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def load_vars_nested
|
|
|
|
@my_module = MyModule.find_by_id(params[:my_module_id])
|
|
|
|
|
|
|
|
unless @my_module
|
|
|
|
render_404
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def convert_contents_to_utf8
|
|
|
|
if params.include? :result and
|
|
|
|
params[:result].include? :table_attributes and
|
|
|
|
params[:result][:table_attributes].include? :contents then
|
|
|
|
params[:result][:table_attributes][:contents] =
|
|
|
|
params[:result][:table_attributes][:contents].encode(Encoding::UTF_8).force_encoding(Encoding::UTF_8)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 19:11:41 +08:00
|
|
|
def check_create_permissions
|
|
|
|
render_403 unless can_create_results?(@my_module)
|
|
|
|
end
|
|
|
|
|
2018-02-09 23:14:40 +08:00
|
|
|
def check_manage_permissions
|
2021-09-24 19:11:41 +08:00
|
|
|
render_403 unless can_manage_result?(@result)
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def check_archive_permissions
|
2018-02-19 21:47:36 +08:00
|
|
|
if result_params[:archived].to_s != '' && !can_manage_result?(@result)
|
2016-02-12 23:52:43 +08:00
|
|
|
render_403
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2021-09-24 19:11:41 +08:00
|
|
|
def check_view_permissions
|
|
|
|
render_403 unless can_read_result?(@result)
|
|
|
|
end
|
|
|
|
|
2016-02-12 23:52:43 +08:00
|
|
|
def result_params
|
|
|
|
params.require(:result).permit(
|
|
|
|
:name, :archived,
|
|
|
|
table_attributes: [
|
|
|
|
:id,
|
2023-02-06 19:55:02 +08:00
|
|
|
:contents,
|
|
|
|
:metadata
|
2016-02-12 23:52:43 +08:00
|
|
|
]
|
|
|
|
)
|
|
|
|
end
|
2019-03-14 02:05:29 +08:00
|
|
|
|
|
|
|
def log_activity(type_of)
|
|
|
|
Activities::CreateActivityService
|
|
|
|
.call(activity_type: type_of,
|
|
|
|
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: t('activities.result_type.table')
|
2019-03-14 02:05:29 +08:00
|
|
|
})
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|