mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-08 16:06:03 +08:00
Merge pull request #683 from mlorb/ml_sci_1351_v2
Fix multiple errors handling on fields in repositories tables [SCI-1351]
This commit is contained in:
commit
385c197077
1 changed files with 36 additions and 34 deletions
|
@ -21,7 +21,6 @@ class RepositoryRowsController < ApplicationController
|
|||
record.name = record_params[:name] unless record_params[:name].blank?
|
||||
unless record.save
|
||||
errors[:default_fields] = record.errors.messages
|
||||
raise ActiveRecord::RecordInvalid
|
||||
end
|
||||
if params[:repository_cells]
|
||||
params[:repository_cells].each do |key, value|
|
||||
|
@ -37,28 +36,30 @@ class RepositoryRowsController < ApplicationController
|
|||
repository_column: column
|
||||
}
|
||||
)
|
||||
unless cell_value.save
|
||||
if cell_value.save
|
||||
record_annotation_notification(record, cell_value.repository_cell)
|
||||
else
|
||||
errors[:repository_cells] << {
|
||||
"#{column.id}": cell_value.errors.messages
|
||||
}
|
||||
raise ActiveRecord::RecordInvalid
|
||||
end
|
||||
record_annotation_notification(record, cell_value.repository_cell)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
respond_to do |format|
|
||||
format.json do
|
||||
if errors[:default_fields].empty? && errors[:repository_cells].empty?
|
||||
render json: { id: record.id,
|
||||
flash: t('repositories.create.success_flash',
|
||||
record: escape_input(record.name),
|
||||
repository: escape_input(@repository.name)) },
|
||||
status: :ok
|
||||
else
|
||||
render json: errors,
|
||||
status: :bad_request
|
||||
end
|
||||
end
|
||||
rescue
|
||||
respond_to do |format|
|
||||
format.json { render json: errors, status: :bad_request }
|
||||
end
|
||||
end
|
||||
|
||||
|
@ -94,7 +95,6 @@ class RepositoryRowsController < ApplicationController
|
|||
@record.name = record_params[:name].blank? ? nil : record_params[:name]
|
||||
unless @record.save
|
||||
errors[:default_fields] = @record.errors.messages
|
||||
raise ActiveRecord::RecordInvalid
|
||||
end
|
||||
if params[:repository_cells]
|
||||
params[:repository_cells].each do |key, value|
|
||||
|
@ -104,14 +104,14 @@ class RepositoryRowsController < ApplicationController
|
|||
if existing
|
||||
# Cell exists and new value present, so update value
|
||||
existing.value.data = value
|
||||
unless existing.value.save
|
||||
if existing.value.save
|
||||
record_annotation_notification(@record, existing)
|
||||
else
|
||||
errors[:repository_cells] << {
|
||||
"#{existing.repository_column_id}":
|
||||
existing.value.errors.messages
|
||||
}
|
||||
raise ActiveRecord::RecordInvalid
|
||||
end
|
||||
record_annotation_notification(@record, existing)
|
||||
else
|
||||
# Looks like it is a new cell, so we need to create new value, cell
|
||||
# will be created automatically
|
||||
|
@ -127,13 +127,13 @@ class RepositoryRowsController < ApplicationController
|
|||
repository_column: column
|
||||
}
|
||||
)
|
||||
unless value.save
|
||||
if value.save
|
||||
record_annotation_notification(@record, value.repository_cell)
|
||||
else
|
||||
errors[:repository_cells] << {
|
||||
"#{cell.repository_column_id}": value.errors.messages
|
||||
}
|
||||
raise ActiveRecord::RecordInvalid
|
||||
end
|
||||
record_annotation_notification(@record, value.repository_cell)
|
||||
end
|
||||
end
|
||||
# Clean up empty cells, not present in updated record
|
||||
|
@ -146,9 +146,10 @@ class RepositoryRowsController < ApplicationController
|
|||
end
|
||||
end
|
||||
|
||||
# Row sucessfully updated, so sending response to client
|
||||
respond_to do |format|
|
||||
format.json do
|
||||
if errors[:default_fields].empty? && errors[:repository_cells].empty?
|
||||
# Row sucessfully updated, so sending response to client
|
||||
render json: {
|
||||
id: @record.id,
|
||||
flash: t(
|
||||
|
@ -158,11 +159,12 @@ class RepositoryRowsController < ApplicationController
|
|||
)
|
||||
},
|
||||
status: :ok
|
||||
else
|
||||
# Errors
|
||||
render json: errors,
|
||||
status: :bad_request
|
||||
end
|
||||
end
|
||||
rescue
|
||||
respond_to do |format|
|
||||
format.json { render json: errors, status: :bad_request }
|
||||
end
|
||||
end
|
||||
|
||||
|
|
Loading…
Reference in a new issue