mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-03-05 20:23:16 +08:00
fix bug and some refectoring
This commit is contained in:
parent
2627efe402
commit
dceef349a4
1 changed files with 62 additions and 92 deletions
|
@ -34,62 +34,7 @@ class RepositoryRowsController < ApplicationController
|
||||||
errors[:default_fields] = record.errors.messages unless record.save
|
errors[:default_fields] = record.errors.messages unless record.save
|
||||||
if cell_params
|
if cell_params
|
||||||
cell_params.each do |key, value|
|
cell_params.each do |key, value|
|
||||||
column = @repository.repository_columns.detect do |c|
|
next if create_cell_value(record, key, value, errors).nil?
|
||||||
c.id == key.to_i
|
|
||||||
end
|
|
||||||
if column.data_type == 'RepositoryListValue'
|
|
||||||
next if value == '-1'
|
|
||||||
# check if item existx else revert the transaction
|
|
||||||
list_item = RepositoryListItem.where(repository_column: column)
|
|
||||||
.find(value)
|
|
||||||
cell_value = RepositoryListValue.new(
|
|
||||||
repository_list_item_id: list_item.id,
|
|
||||||
created_by: current_user,
|
|
||||||
last_modified_by: current_user,
|
|
||||||
repository_cell_attributes: {
|
|
||||||
repository_row: record,
|
|
||||||
repository_column: column
|
|
||||||
}
|
|
||||||
)
|
|
||||||
elsif column.data_type == 'RepositoryAssetValue'
|
|
||||||
asset = Asset.new(file: value,
|
|
||||||
created_by: current_user,
|
|
||||||
last_modified_by: current_user,
|
|
||||||
team: current_team)
|
|
||||||
if asset.save
|
|
||||||
asset.post_process_file(current_team)
|
|
||||||
else
|
|
||||||
errors[:repository_cells] << {
|
|
||||||
"#{column.id}": { data: asset.errors.messages[:file].first }
|
|
||||||
}
|
|
||||||
end
|
|
||||||
cell_value = RepositoryAssetValue.new(
|
|
||||||
asset: asset,
|
|
||||||
created_by: current_user,
|
|
||||||
last_modified_by: current_user,
|
|
||||||
repository_cell_attributes: {
|
|
||||||
repository_row: record,
|
|
||||||
repository_column: column
|
|
||||||
}
|
|
||||||
)
|
|
||||||
else
|
|
||||||
cell_value = RepositoryTextValue.new(
|
|
||||||
data: value,
|
|
||||||
created_by: current_user,
|
|
||||||
last_modified_by: current_user,
|
|
||||||
repository_cell_attributes: {
|
|
||||||
repository_row: record,
|
|
||||||
repository_column: column
|
|
||||||
}
|
|
||||||
)
|
|
||||||
end
|
|
||||||
if cell_value.save
|
|
||||||
record_annotation_notification(record, cell_value.repository_cell)
|
|
||||||
else
|
|
||||||
errors[:repository_cells] << {
|
|
||||||
"#{column.id}": cell_value.errors.messages
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
raise ActiveRecord::Rollback if errors[:repository_cells].any?
|
raise ActiveRecord::Rollback if errors[:repository_cells].any?
|
||||||
|
@ -205,42 +150,7 @@ class RepositoryRowsController < ApplicationController
|
||||||
else
|
else
|
||||||
# Looks like it is a new cell, so we need to create new value, cell
|
# Looks like it is a new cell, so we need to create new value, cell
|
||||||
# will be created automatically
|
# will be created automatically
|
||||||
column = @repository.repository_columns.detect do |c|
|
next if create_cell_value(@record, key, value, errors).nil?
|
||||||
c.id == key.to_i
|
|
||||||
end
|
|
||||||
if column.data_type == 'RepositoryListValue'
|
|
||||||
next if value == '-1'
|
|
||||||
# check if item existx else revert the transaction
|
|
||||||
list_item = RepositoryListItem.where(repository_column: column)
|
|
||||||
.find(value)
|
|
||||||
cell_value = RepositoryListValue.new(
|
|
||||||
repository_list_item_id: list_item.id,
|
|
||||||
created_by: current_user,
|
|
||||||
last_modified_by: current_user,
|
|
||||||
repository_cell_attributes: {
|
|
||||||
repository_row: @record,
|
|
||||||
repository_column: column
|
|
||||||
}
|
|
||||||
)
|
|
||||||
else
|
|
||||||
cell_value = RepositoryTextValue.new(
|
|
||||||
data: value,
|
|
||||||
created_by: current_user,
|
|
||||||
last_modified_by: current_user,
|
|
||||||
repository_cell_attributes: {
|
|
||||||
repository_row: @record,
|
|
||||||
repository_column: column
|
|
||||||
}
|
|
||||||
)
|
|
||||||
end
|
|
||||||
if cell_value.save
|
|
||||||
record_annotation_notification(@record,
|
|
||||||
cell_value.repository_cell)
|
|
||||||
else
|
|
||||||
errors[:repository_cells] << {
|
|
||||||
"#{column.id}": cell_value.errors.messages
|
|
||||||
}
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
# Clean up empty cells, not present in updated record
|
# Clean up empty cells, not present in updated record
|
||||||
|
@ -277,6 +187,66 @@ class RepositoryRowsController < ApplicationController
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def create_cell_value(record, key, value, errors)
|
||||||
|
column = @repository.repository_columns.detect do |c|
|
||||||
|
c.id == key.to_i
|
||||||
|
end
|
||||||
|
if column.data_type == 'RepositoryListValue'
|
||||||
|
return if value == '-1'
|
||||||
|
# check if item existx else revert the transaction
|
||||||
|
list_item = RepositoryListItem.where(repository_column: column)
|
||||||
|
.find(value)
|
||||||
|
cell_value = RepositoryListValue.new(
|
||||||
|
repository_list_item_id: list_item.id,
|
||||||
|
created_by: current_user,
|
||||||
|
last_modified_by: current_user,
|
||||||
|
repository_cell_attributes: {
|
||||||
|
repository_row: record,
|
||||||
|
repository_column: column
|
||||||
|
}
|
||||||
|
)
|
||||||
|
elsif column.data_type == 'RepositoryAssetValue'
|
||||||
|
asset = Asset.new(file: value,
|
||||||
|
created_by: current_user,
|
||||||
|
last_modified_by: current_user,
|
||||||
|
team: current_team)
|
||||||
|
if asset.save
|
||||||
|
asset.post_process_file(current_team)
|
||||||
|
else
|
||||||
|
errors[:repository_cells] << {
|
||||||
|
"#{column.id}": { data: asset.errors.messages[:file].first }
|
||||||
|
}
|
||||||
|
end
|
||||||
|
cell_value = RepositoryAssetValue.new(
|
||||||
|
asset: asset,
|
||||||
|
created_by: current_user,
|
||||||
|
last_modified_by: current_user,
|
||||||
|
repository_cell_attributes: {
|
||||||
|
repository_row: record,
|
||||||
|
repository_column: column
|
||||||
|
}
|
||||||
|
)
|
||||||
|
else
|
||||||
|
cell_value = RepositoryTextValue.new(
|
||||||
|
data: value,
|
||||||
|
created_by: current_user,
|
||||||
|
last_modified_by: current_user,
|
||||||
|
repository_cell_attributes: {
|
||||||
|
repository_row: record,
|
||||||
|
repository_column: column
|
||||||
|
}
|
||||||
|
)
|
||||||
|
end
|
||||||
|
if cell_value.save
|
||||||
|
record_annotation_notification(record,
|
||||||
|
cell_value.repository_cell)
|
||||||
|
else
|
||||||
|
errors[:repository_cells] << {
|
||||||
|
"#{column.id}": cell_value.errors.messages
|
||||||
|
}
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
def delete_records
|
def delete_records
|
||||||
deleted_count = 0
|
deleted_count = 0
|
||||||
if selected_params
|
if selected_params
|
||||||
|
|
Loading…
Reference in a new issue