mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-11-10 00:11:22 +08:00
Refactor method for renaming attached files [SCI-10256]
This commit is contained in:
parent
7366134fe3
commit
580f67dbf1
10 changed files with 47 additions and 83 deletions
|
|
@ -18,7 +18,7 @@ class AssetsController < ApplicationController
|
||||||
|
|
||||||
before_action :load_vars, except: :create_wopi_file
|
before_action :load_vars, except: :create_wopi_file
|
||||||
before_action :check_read_permission, except: %i(edit destroy create_wopi_file toggle_view_mode)
|
before_action :check_read_permission, except: %i(edit destroy create_wopi_file toggle_view_mode)
|
||||||
before_action :check_edit_permission, only: %i(edit destroy toggle_view_mode rename)
|
before_action :check_manage_permission, only: %i(edit destroy toggle_view_mode rename)
|
||||||
|
|
||||||
def file_preview
|
def file_preview
|
||||||
render json: { html: render_to_string(
|
render json: { html: render_to_string(
|
||||||
|
|
@ -315,22 +315,43 @@ class AssetsController < ApplicationController
|
||||||
new_name = params.require(:asset).permit(:name)[:name]
|
new_name = params.require(:asset).permit(:name)[:name]
|
||||||
|
|
||||||
if new_name.empty?
|
if new_name.empty?
|
||||||
render json: { error: 'File name must be at least 1 character long.' }, status: :unprocessable_entity
|
render json: { error: I18n.t('assets.rename_modal.min_length_error') }, status: :unprocessable_entity
|
||||||
return
|
return
|
||||||
elsif new_name.length > Constants::NAME_MAX_LENGTH
|
elsif new_name.length > Constants::NAME_MAX_LENGTH
|
||||||
render json: { error: 'File name is too long (maximum number is 255 characters).' }, status: :unprocessable_entity
|
render json: { error: I18n.t('assets.rename_modal.max_length_error') }, status: :unprocessable_entity
|
||||||
return
|
return
|
||||||
end
|
end
|
||||||
|
|
||||||
ActiveRecord::Base.transaction do
|
ActiveRecord::Base.transaction do
|
||||||
|
old_name = @asset.name
|
||||||
@asset.last_modified_by = current_user
|
@asset.last_modified_by = current_user
|
||||||
@asset.rename_file(new_name)
|
@asset.rename_file(new_name)
|
||||||
@asset.save!
|
@asset.save!
|
||||||
|
|
||||||
|
case @asset.parent
|
||||||
|
when Step
|
||||||
|
message_items = { old_name:, new_name:, user: current_user.id }
|
||||||
|
message_items[:my_module] = @assoc.protocol.my_module.id if @assoc.protocol.in_module?
|
||||||
|
|
||||||
|
log_step_activity(
|
||||||
|
"#{@assoc.protocol.in_module? ? 'task' : 'protocol'}_step_asset_renamed",
|
||||||
|
@assoc,
|
||||||
|
@assoc.my_module&.project,
|
||||||
|
message_items
|
||||||
|
)
|
||||||
|
when Result
|
||||||
|
log_result_activity(
|
||||||
|
:result_asset_renamed,
|
||||||
|
@assoc,
|
||||||
|
old_name:,
|
||||||
|
new_name:,
|
||||||
|
user: current_user.id,
|
||||||
|
my_module: @assoc.my_module.id
|
||||||
|
)
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
render json: @asset, serializer: AssetSerializer, user: current_user
|
render json: @asset, serializer: AssetSerializer, user: current_user
|
||||||
rescue ActiveRecord::RecordInvalid
|
|
||||||
render json: @asset.errors, status: :unprocessable_entity
|
|
||||||
end
|
end
|
||||||
|
|
||||||
def checksum
|
def checksum
|
||||||
|
|
@ -362,7 +383,7 @@ class AssetsController < ApplicationController
|
||||||
render_403 and return unless can_read_asset?(@asset)
|
render_403 and return unless can_read_asset?(@asset)
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_edit_permission
|
def check_manage_permission
|
||||||
render_403 and return unless can_manage_asset?(@asset)
|
render_403 and return unless can_manage_asset?(@asset)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -64,34 +64,6 @@ class GeneSequenceAssetsController < ApplicationController
|
||||||
head :ok
|
head :ok
|
||||||
end
|
end
|
||||||
|
|
||||||
def rename
|
|
||||||
new_name = params.require(:asset).permit(:name)[:name]
|
|
||||||
|
|
||||||
if new_name.empty?
|
|
||||||
render json: { error: 'File name must be at least 1 character long.' }, status: :unprocessable_entity
|
|
||||||
return
|
|
||||||
elsif new_name.length > Constants::NAME_MAX_LENGTH
|
|
||||||
render json: { error: 'File name is too long (maximum number is 255 characters).' }, status: :unprocessable_entity
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
asset = current_team.assets.find_by(id: params[:id])
|
|
||||||
return render_404 unless asset
|
|
||||||
|
|
||||||
ActiveRecord::Base.transaction do
|
|
||||||
asset.last_modified_by = current_user
|
|
||||||
asset.rename_file(new_name)
|
|
||||||
asset.save!
|
|
||||||
# log_activity(TODO)
|
|
||||||
end
|
|
||||||
|
|
||||||
render json: asset, serializer: AssetSerializer, user: current_user
|
|
||||||
rescue StandardError => e
|
|
||||||
Rails.logger.error(e.message)
|
|
||||||
Rails.logger.error(e.backtrace.join("\n"))
|
|
||||||
render json: { error: I18n.t('errors.general') }, status: :unprocessable_entity
|
|
||||||
end
|
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def save_asset!
|
def save_asset!
|
||||||
|
|
|
||||||
|
|
@ -8,7 +8,7 @@ class MarvinJsAssetsController < ApplicationController
|
||||||
before_action :load_create_vars, only: :create
|
before_action :load_create_vars, only: :create
|
||||||
|
|
||||||
before_action :check_read_permission
|
before_action :check_read_permission
|
||||||
before_action :check_edit_permission, only: %i(update create start_editing)
|
before_action :check_manage_permission, only: %i(update create start_editing)
|
||||||
|
|
||||||
def create
|
def create
|
||||||
result = MarvinJsService.create_sketch(marvin_params, current_user, current_team)
|
result = MarvinJsService.create_sketch(marvin_params, current_user, current_team)
|
||||||
|
|
@ -45,28 +45,6 @@ class MarvinJsAssetsController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def rename
|
|
||||||
new_name = params.require(:asset).permit(:name)[:name]
|
|
||||||
|
|
||||||
if new_name.empty?
|
|
||||||
render json: { error: 'File name must be at least 1 character long.' }, status: :unprocessable_entity
|
|
||||||
return
|
|
||||||
elsif new_name.length > Constants::NAME_MAX_LENGTH
|
|
||||||
render json: { error: 'File name is too long (maximum number is 255 characters).' }, status: :unprocessable_entity
|
|
||||||
return
|
|
||||||
end
|
|
||||||
|
|
||||||
asset = MarvinJsService.update_file_name(new_name, params[:id], current_user, current_team)
|
|
||||||
|
|
||||||
# create_rename_marvinjs_activity(asset, current_user, :TODO)
|
|
||||||
|
|
||||||
if asset
|
|
||||||
render json: asset, serializer: AssetSerializer, user: current_user
|
|
||||||
else
|
|
||||||
render json: { error: t('marvinjs.no_sketches_found') }, status: :unprocessable_entity
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
def start_editing
|
def start_editing
|
||||||
create_edit_marvinjs_activity(@asset, current_user, :start_editing)
|
create_edit_marvinjs_activity(@asset, current_user, :start_editing)
|
||||||
end
|
end
|
||||||
|
|
@ -109,7 +87,7 @@ class MarvinJsAssetsController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def check_edit_permission
|
def check_manage_permission
|
||||||
if @assoc.class == Step
|
if @assoc.class == Step
|
||||||
return render_403 unless can_manage_step?(@assoc)
|
return render_403 unless can_manage_step?(@assoc)
|
||||||
elsif @assoc.class == Result
|
elsif @assoc.class == Result
|
||||||
|
|
|
||||||
|
|
@ -550,8 +550,6 @@
|
||||||
this.$emit('stepUpdated');
|
this.$emit('stepUpdated');
|
||||||
},
|
},
|
||||||
updateAttachment(attachment) {
|
updateAttachment(attachment) {
|
||||||
console.log(this.attachments)
|
|
||||||
console.log(attachment)
|
|
||||||
const index = this.attachments.findIndex(a => a.id === attachment.id);
|
const index = this.attachments.findIndex(a => a.id === attachment.id);
|
||||||
if (index !== -1) {
|
if (index !== -1) {
|
||||||
this.attachments[index] = attachment;
|
this.attachments[index] = attachment;
|
||||||
|
|
|
||||||
|
|
@ -79,9 +79,9 @@ export default {
|
||||||
this.close();
|
this.close();
|
||||||
} catch (error) {
|
} catch (error) {
|
||||||
if (error.response) {
|
if (error.response) {
|
||||||
console.error('Error: ', error.response.data.error || error.response.statusText);
|
this.error = error.response.data.errors || error.response.statusText;
|
||||||
} else {
|
} else {
|
||||||
console.error('Error: ', error.message);
|
this.error = error.message;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -146,16 +146,7 @@ class AssetSerializer < ActiveModel::Serializer
|
||||||
delete: asset_destroy_path(object),
|
delete: asset_destroy_path(object),
|
||||||
move_targets: asset_move_tagets_path(object),
|
move_targets: asset_move_tagets_path(object),
|
||||||
move: asset_move_path(object),
|
move: asset_move_path(object),
|
||||||
rename: if object.file.attached?
|
rename: asset_rename_path(object)
|
||||||
case object.file.metadata['asset_type']
|
|
||||||
when 'marvinjs'
|
|
||||||
rename_marvin_js_asset_path(object)
|
|
||||||
when 'gene_sequence'
|
|
||||||
rename_gene_sequence_asset_path(object)
|
|
||||||
else
|
|
||||||
asset_rename_path(object)
|
|
||||||
end
|
|
||||||
end
|
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
urls[:open_vector_editor_edit] = edit_gene_sequence_asset_path(object.id) if can_manage_asset?(user, object)
|
urls[:open_vector_editor_edit] = edit_gene_sequence_asset_path(object.id) if can_manage_asset?(user, object)
|
||||||
|
|
|
||||||
|
|
@ -488,16 +488,19 @@ class Extends
|
||||||
inventory_item_relationships_unlinked: 298,
|
inventory_item_relationships_unlinked: 298,
|
||||||
edit_task_step_file_locally: 299,
|
edit_task_step_file_locally: 299,
|
||||||
edit_protocol_template_file_locally: 300,
|
edit_protocol_template_file_locally: 300,
|
||||||
edit_task_result_file_locally: 301
|
edit_task_result_file_locally: 301,
|
||||||
|
task_step_asset_renamed: 305,
|
||||||
|
result_asset_renamed: 306,
|
||||||
|
protocol_step_asset_renamed: 307
|
||||||
}
|
}
|
||||||
|
|
||||||
ACTIVITY_GROUPS = {
|
ACTIVITY_GROUPS = {
|
||||||
projects: [*0..7, 32, 33, 34, 95, 108, 65, 109, *158..162, 241, 242, 243],
|
projects: [*0..7, 32, 33, 34, 95, 108, 65, 109, *158..162, 241, 242, 243],
|
||||||
task_results: [23, 26, 25, 42, 24, 40, 41, 99, 110, 122, 116, 128, *246..248, *257..273, *284..291, 301],
|
task_results: [23, 26, 25, 42, 24, 40, 41, 99, 110, 122, 116, 128, *246..248, *257..273, *284..291, 301, 306],
|
||||||
task: [8, 58, 9, 59, *10..14, 35, 36, 37, 53, 54, *60..63, 138, 139, 140, 64, 66, 106, 126, 120, 132,
|
task: [8, 58, 9, 59, *10..14, 35, 36, 37, 53, 54, *60..63, 138, 139, 140, 64, 66, 106, 126, 120, 132,
|
||||||
148, 166],
|
148, 166],
|
||||||
task_protocol: [15, 22, 16, 18, 19, 20, 21, 17, 38, 39, 100, 111, 45, 46, 47, 121, 124, 115, 118, 127, 130, 137,
|
task_protocol: [15, 22, 16, 18, 19, 20, 21, 17, 38, 39, 100, 111, 45, 46, 47, 121, 124, 115, 118, 127, 130, 137,
|
||||||
184, 185, 188, 189, *192..203, 221, 222, 224, 225, 226, 236, *249..252, *274..278, 299],
|
184, 185, 188, 189, *192..203, 221, 222, 224, 225, 226, 236, *249..252, *274..278, 299, 305],
|
||||||
task_inventory: [55, 56, 146, 147, 183],
|
task_inventory: [55, 56, 146, 147, 183],
|
||||||
experiment: [*27..31, 57, 141, 165],
|
experiment: [*27..31, 57, 141, 165],
|
||||||
reports: [48, 50, 49, 163, 164],
|
reports: [48, 50, 49, 163, 164],
|
||||||
|
|
@ -506,7 +509,7 @@ class Extends
|
||||||
protocol_repository: [80, 103, 89, 87, 79, 90, 91, 88, 85, 86, 84, 81, 82,
|
protocol_repository: [80, 103, 89, 87, 79, 90, 91, 88, 85, 86, 84, 81, 82,
|
||||||
83, 101, 112, 123, 125, 117, 119, 129, 131, 187, 186,
|
83, 101, 112, 123, 125, 117, 119, 129, 131, 187, 186,
|
||||||
190, 191, *204..215, 220, 223, 227, 228, 229, *230..235,
|
190, 191, *204..215, 220, 223, 227, 228, 229, *230..235,
|
||||||
*237..240, *253..256, *279..283, 300],
|
*237..240, *253..256, *279..283, 300, 307],
|
||||||
team: [92, 94, 93, 97, 104, 244, 245],
|
team: [92, 94, 93, 97, 104, 244, 245],
|
||||||
label_templates: [*216..219]
|
label_templates: [*216..219]
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -314,6 +314,9 @@ en:
|
||||||
edit_image_on_inventory_item_html: "%{user} edited image %{asset_name} on inventory item %{repository_row} in inventory %{repository}: %{action}."
|
edit_image_on_inventory_item_html: "%{user} edited image %{asset_name} on inventory item %{repository_row} in inventory %{repository}: %{action}."
|
||||||
edit_wopi_file_on_inventory_item_html: "%{user} edited Office online file %{asset_name} on inventory item %{repository_row} in inventory %{repository}: %{action}."
|
edit_wopi_file_on_inventory_item_html: "%{user} edited Office online file %{asset_name} on inventory item %{repository_row} in inventory %{repository}: %{action}."
|
||||||
export_inventory_stock_consumption_html: "%{user} exported stock consumption for inventory item(s) %{inventory_items} in inventory %{repository}."
|
export_inventory_stock_consumption_html: "%{user} exported stock consumption for inventory item(s) %{inventory_items} in inventory %{repository}."
|
||||||
|
task_step_asset_renamed_html: "%{user} renamed file %{old_name} to %{new_name} on protocol’s step <strong>%{step}</strong> on task <strong>%{my_module}</strong>."
|
||||||
|
protocol_step_asset_renamed_html: "%{user} renamed file %{old_name} to %{new_name} on protocol’s step <strong>%{step}</strong> in Protocol repository."
|
||||||
|
result_asset_renamed_html: "%{user} renamed file %{old_name} to %{new_name} on result <strong>%{result}</strong> on task <strong>%{my_module}</strong>."
|
||||||
activity_name:
|
activity_name:
|
||||||
create_project: "Project created"
|
create_project: "Project created"
|
||||||
rename_project: "Project renamed"
|
rename_project: "Project renamed"
|
||||||
|
|
@ -585,6 +588,9 @@ en:
|
||||||
edit_image_on_inventory_item: "Inventory item image edited"
|
edit_image_on_inventory_item: "Inventory item image edited"
|
||||||
edit_wopi_file_on_inventory_item: "Inventory item wopi file edited"
|
edit_wopi_file_on_inventory_item: "Inventory item wopi file edited"
|
||||||
export_inventory_stock_consumption: "Inventory stock consumptions exported"
|
export_inventory_stock_consumption: "Inventory stock consumptions exported"
|
||||||
|
task_step_asset_renamed: "File attachment on Task step renamed"
|
||||||
|
protocol_step_asset_renamed: "File attachment on Protocol step renamed"
|
||||||
|
result_asset_renamed: "File attachment on Task result renamed"
|
||||||
activity_group:
|
activity_group:
|
||||||
projects: "Projects"
|
projects: "Projects"
|
||||||
task_results: "Task results"
|
task_results: "Task results"
|
||||||
|
|
|
||||||
|
|
@ -1007,7 +1007,6 @@ Rails.application.routes.draw do
|
||||||
end
|
end
|
||||||
member do
|
member do
|
||||||
post :start_editing
|
post :start_editing
|
||||||
patch :rename
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
@ -1026,11 +1025,7 @@ Rails.application.routes.draw do
|
||||||
post 'wopi/files/:id', to: 'wopi#post_file_endpoint'
|
post 'wopi/files/:id', to: 'wopi#post_file_endpoint'
|
||||||
end
|
end
|
||||||
|
|
||||||
resources :gene_sequence_assets, only: %i(new create edit update) do
|
resources :gene_sequence_assets, only: %i(new create edit update)
|
||||||
member do
|
|
||||||
patch :rename
|
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
if Rails.env.development? || ENV['ENABLE_DESIGN_ELEMENTS'] == 'true'
|
if Rails.env.development? || ENV['ENABLE_DESIGN_ELEMENTS'] == 'true'
|
||||||
resources :design_elements, only: %i(index) do
|
resources :design_elements, only: %i(index) do
|
||||||
|
|
|
||||||
|
|
@ -23,7 +23,7 @@ describe AssetsController, type: :controller do
|
||||||
|
|
||||||
describe 'POST start_edit' do
|
describe 'POST start_edit' do
|
||||||
before do
|
before do
|
||||||
allow(controller).to receive(:check_edit_permission).and_return(true)
|
allow(controller).to receive(:check_manage_permission).and_return(true)
|
||||||
end
|
end
|
||||||
let(:action) { post :create_start_edit_image_activity, params: params, format: :json }
|
let(:action) { post :create_start_edit_image_activity, params: params, format: :json }
|
||||||
let!(:params) do
|
let!(:params) do
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue