Skip to content

Commit

Permalink
Simplification + better error messages
Browse files Browse the repository at this point in the history
  • Loading branch information
kdp-cloud committed May 24, 2024
1 parent 3c32ab0 commit 2bf968a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
4 changes: 3 additions & 1 deletion app/controllers/isa_assays_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,9 @@ def find_requested_item
end

if @isa_assay.errors.any?
error_messages = @isa_assay.errors.full_messages.map { |msg| "<li>[#{t('isa_assay')}]: #{msg}</li>" }.join('')
error_messages = @isa_assay.errors.map do |error|
"<li>[<b>#{error.attribute.to_s}</b>]: #{error.message}</li>"
end.join('')
flash[:error] = "<ul>#{error_messages}</ul>".html_safe
redirect_to single_page_path(id: @isa_assay.assay.projects.first, item_type: 'assay',
item_id: @isa_assay.assay)
Expand Down
23 changes: 15 additions & 8 deletions app/controllers/isa_studies_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,12 +36,7 @@ def create
end

def edit
@isa_study.source = nil unless requested_item_authorized?(@isa_study.source)
@isa_study.sample_collection = nil unless requested_item_authorized?(@isa_study.sample_collection)

respond_to do |format|
format.html
end
respond_to(&:html)
end

def update
Expand Down Expand Up @@ -132,8 +127,20 @@ def set_up_instance_variable
def find_requested_item
@isa_study = IsaStudy.new
@isa_study.populate(params[:id])
unless requested_item_authorized?(@isa_study.study)
flash[:error] = "You are not authorized to edit this #{t('isa_study')}"

@isa_study.errors.add(:study, "The #{t('isa_study')} was not found.") if @isa_study.study.nil?
@isa_study.errors.add(:study, "You are not authorized to edit this #{t('isa_study')}.") unless requested_item_authorized?(@isa_study.study)

@isa_study.errors.add(:sample_type, "'Study source' #{t('sample_type')} not found.") if @isa_study.source.nil?
@isa_study.errors.add(:sample_type, "You are not authorized to edit the 'study source' #{t('sample_type')}.") unless requested_item_authorized?(@isa_study.source)
@isa_study.errors.add(:sample_type, "'Study sample' #{t('sample_type')} not found.") if @isa_study.sample_collection.nil?
@isa_study.errors.add(:sample_type, "You are not authorized to edit the 'study sample' #{t('sample_type')}.") unless requested_item_authorized?(@isa_study.sample_collection)

if @isa_study.errors.any?
error_messages = @isa_study.errors.map do |error|
"<li>[<b>#{error.attribute.to_s}</b>]: #{error.message}</li>"
end.join('')
flash[:error] = "<ul>#{error_messages}</ul>".html_safe
redirect_to single_page_path(id: @isa_study.study.projects.first, item_type: 'study',
item_id: @isa_study.study)
end
Expand Down

0 comments on commit 2bf968a

Please sign in to comment.