Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Error message helper #883

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@ class ApplicationController < ActionController::Base
around_action :use_logidze_responsible, only: %i[create destroy update transfer] # rubocop:disable Rails/LexicallyScopedActionFilter
around_action :switch_locale

helper_method :error_message

def set_current_user
Current.user = current_user
yield
Expand Down Expand Up @@ -65,4 +67,8 @@ def route_not_found
def pipelines_enabled?
@pipelines_enabled = Irida::Pipelines.instance.available_pipelines.any?
end

def error_message(object)
object.errors.full_messages.to_sentence
end
end
2 changes: 1 addition & 1 deletion app/controllers/concerns/attachment_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ def destroy # rubocop:disable Metrics/MethodLength
render status: :unprocessable_entity,
locals: { message: t('.error',
filename: @attachment.file.filename,
errors: @attachment.errors.full_messages.first),
errors: error_message(@attachment)),
destroyed_attachments: nil }
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/concerns/bot_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def create # rubocop:disable Metrics/MethodLength
render status: :unprocessable_entity,
locals:
{ type: 'alert',
message: @new_bot_account[:bot_user_account].errors.full_messages.first,
message: error_message(@new_bot_account[:bot_user_account]),
bot_params: }

end
Expand Down Expand Up @@ -74,7 +74,7 @@ def destroy
render status: :unprocessable_entity,
locals: {
type: 'alert',
message: @bot_account.errors.full_messages.first
message: error_message(@bot_account)
}
end
end
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/concerns/bot_personal_access_token_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ def create # rubocop:disable Metrics/MethodLength
render status: :unprocessable_entity,
locals:
{ type: 'alert',
message: @personal_access_token.errors.full_messages.first }
message: error_message(@personal_access_token) }

end
end
Expand Down Expand Up @@ -75,7 +75,7 @@ def revoke
}
else
render status: :unprocessable_entity, locals: { type: 'alert',
message: @personal_access_token.errors.full_messages.first }
message: error_message(@personal_access_token) }
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/concerns/membership_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ def create # rubocop:disable Metrics/MethodLength
respond_to do |format|
format.turbo_stream do
render status: :unprocessable_entity, locals: { member: @new_member, type: 'alert',
message: @new_member.errors.full_messages.first }
message: error_message(@new_member) }
end
end
end
Expand Down Expand Up @@ -81,7 +81,7 @@ def destroy # rubocop:disable Metrics/AbcSize, Metrics/MethodLength, Metrics/Per
I18n.t('activerecord.errors.models.member.destroy.last_member_self',
namespace_type: @namespace.class.model_name.human)
else
@member.errors.full_messages.first
error_message(@member)
end

respond_to do |format|
Expand All @@ -106,7 +106,7 @@ def update # rubocop:disable Metrics/MethodLength
format.turbo_stream do
render status: :bad_request,
locals: { member: @member, type: 'alert',
message: @member.errors.full_messages.first }
message: error_message(@member) }
end
end
end
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/concerns/share_actions.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,13 +50,13 @@ def create # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
render status: :unprocessable_entity,
locals: { namespace_group_link: @created_namespace_group_link,
type: 'alert',
message: @created_namespace_group_link.errors.full_messages.first }
message: error_message(@created_namespace_group_link) }
end
end
end
end

def destroy # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
def destroy # rubocop:disable Metrics/MethodLength
GroupLinks::GroupUnlinkService.new(current_user, @namespace_group_link).execute
@pagy, @namespace_group_links = pagy(load_namespace_group_links)

Expand All @@ -73,7 +73,7 @@ def destroy # rubocop:disable Metrics/MethodLength, Metrics/AbcSize
format.turbo_stream do
render status: :unprocessable_entity,
locals: { namespace_group_link: @namespace_group_link, type: 'alert',
message: @namespace_group_link.errors.full_messages.first }
message: error_message(@namespace_group_link) }
end
end
else
Expand Down
4 changes: 2 additions & 2 deletions app/controllers/data_exports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,14 @@ def new
end
end

def create # rubocop:disable Metrics/AbcSize
def create
@data_export = DataExports::CreateService.new(current_user, data_export_params).execute

if @data_export.errors.any?
respond_to do |format|
format.turbo_stream do
render status: :unprocessable_entity,
locals: { type: 'alert', message: @data_export.errors.full_messages.first,
locals: { type: 'alert', message: error_message(@data_export),
export_type: data_export_params['export_type'] }
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/groups_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ def destroy
flash[:success] = t('.success', group_name: @group.name)
redirect_to dashboard_groups_path(format: :html)
else
flash[:error] = @group.errors.full_messages.first
flash[:error] = error_message(@group)
redirect_to group_path(@group)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create # rubocop:disable Metrics/MethodLength
format.turbo_stream do
render status: :unprocessable_entity, locals: { personal_access_token: @personal_access_token,
new_personal_access_token: nil,
message: @personal_access_token.errors.full_messages.first }
message: error_message(@personal_access_token) }
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ def create
if @sample.errors.empty?
render status: :ok, locals: { type: :success, message: t('.success') }
else
@errors = @sample.errors.full_messages.first
@errors = error_message(@sample)
render status: :unprocessable_entity, locals: { type: :danger,
message: @errors }
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/projects/samples/attachments_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ def destroy # rubocop:disable Metrics/MethodLength
render status: :unprocessable_entity,
locals: { message: t('.error',
filename: @attachment.file.filename,
errors: @attachment.errors.full_messages.first),
errors: error_message(@attachment)),
destroyed_attachments: nil }
end
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/projects/samples/deletions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def destroy
else
format.turbo_stream do
render status: :unprocessable_entity,
locals: { type: 'alert', message: @sample.errors.full_messages.first }
locals: { type: 'alert', message: error_message(@sample) }
end
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ def get_destroy_messages(deleted_keys)
message: t('projects.samples.metadata.deletions.destroy.multi_success',
deleted_keys: deleted_keys.join(', ')) }
end
messages << { type: 'error', message: @sample.errors.full_messages.first } if @sample.errors.any?
messages << { type: 'error', message: error_message(@sample) } if @sample.errors.any?
messages
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ def create
create_field_params['create_fields']).execute

if @sample.errors.any?
render status: :unprocessable_entity, locals: { type: 'error', message: @sample.errors.full_messages.first }
render status: :unprocessable_entity, locals: { type: 'error', message: error_message(@sample) }
else
status = get_create_status(create_metadata_fields[:added_keys], create_metadata_fields[:existing_keys])
messages = get_create_messages(create_metadata_fields[:added_keys], create_metadata_fields[:existing_keys])
Expand Down Expand Up @@ -95,7 +95,7 @@ def get_update_status_and_message(updated_metadata_field)
{ type: 'success', message: t('projects.samples.metadata.fields.update.success') }
else
update_render_params[:status] = :unprocessable_entity
update_render_params[:message] = { type: 'error', message: @sample.errors.full_messages.first }
update_render_params[:message] = { type: 'error', message: error_message(@sample) }
end
update_render_params
end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/projects_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ def destroy
flash[:success] = t('.success', project_name: @project.name)
redirect_to dashboard_projects_path(format: :html)
else
flash[:error] = @project.errors.full_messages.first
flash[:error] = error_message(@project)
redirect_to project_path(@project)
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
t(
".error",
filename: attachment.file.filename,
errors: attachment.errors.full_messages.first
)
errors: error_message(attachment),
),
) %>
<% end %>
<% end %>
Expand All @@ -26,6 +26,6 @@
"table-listing",
partial: "projects/samples/attachments/table",
locals: {
attachments: @sample.attachments
}
attachments: @sample.attachments,
},
) %>
6 changes: 3 additions & 3 deletions app/views/projects/samples/metadata/destroy.turbo_stream.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
<%= turbo_stream.update "update-alert",
viral_alert(
type: "alert",
message: @sample.errors.full_messages.first,
classes: "mb-4"
message: error_message(@sample),
classes: "mb-4",
) %>
<% else %>
<%= turbo_stream.append "flashes" do %>
Expand All @@ -13,7 +13,7 @@
<%= turbo_stream.update "table-listing" do %>
<% render partial: "projects/samples/metadata/table",
locals: {
sample_metadata: @sample.metadata_with_provenance
sample_metadata: @sample.metadata_with_provenance,
} %>
<% end %>
<% end %>
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@
locals: {
open: @sample.errors.any?,
key: defined?(key) ? key : nil,
value: defined?(value) ? value : nil
}
value: defined?(value) ? value : nil,
},
) %>

<% if @sample.errors.any? %>
<%= turbo_stream.update "update-alert",
viral_alert(
type: "alert",
message: @sample.errors.full_messages.first,
classes: "mb-4"
message: error_message(@sample),
classes: "mb-4",
) %>
<% else %>
<%= turbo_stream.append "flashes" do %>
Expand All @@ -23,7 +23,7 @@
<%= turbo_stream.update "table-listing" do %>
<% render partial: "projects/samples/metadata/table",
locals: {
sample_metadata: @sample.metadata_with_provenance
sample_metadata: @sample.metadata_with_provenance,
} %>
<% end %>
<% end %>
6 changes: 3 additions & 3 deletions test/models/user_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,23 +91,23 @@ def setup
current_password: 'password1' }

assert_not @user.update_password_with_password(params)
assert_equal "Password confirmation doesn't match Password", @user.errors.full_messages.first
assert_equal "Password confirmation doesn't match Password", @user.errors.full_messages.to_sentence
end

test 'unable to update password with blank password' do
params = { password: ' ', password_confirmation: ' ',
current_password: 'password1' }

assert_not @user.update_password_with_password(params)
assert_equal "Password can't be blank", @user.errors.full_messages.first
assert_equal "Password can't be blank", @user.errors.full_messages.to_sentence
end

test 'unable to update password with wrong password' do
params = { password: 'new_password', password_confirmation: 'new_password',
current_password: 'wrong_password' }

assert_not @user.update_password_with_password(params)
assert_equal 'Current password is invalid', @user.errors.full_messages.first
assert_equal 'Current password is invalid', @user.errors.full_messages.to_sentence
end

test 'full_name should combine the users first and last names' do
Expand Down
20 changes: 10 additions & 10 deletions test/services/data_exports/create_service_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ def setup
assert_no_difference -> { DataExport.count } do
data_export = DataExports::CreateService.new(@user, invalid_params).execute
assert_equal I18n.t('services.data_exports.create.invalid_export_samples'),
data_export.errors.full_messages.first
data_export.errors.full_messages.to_sentence
end
end

Expand All @@ -63,7 +63,7 @@ def setup
assert_no_difference -> { DataExport.count } do
data_export = DataExports::CreateService.new(@user, invalid_params).execute
assert_equal I18n.t('services.data_exports.create.invalid_export_samples'),
data_export.errors.full_messages.first
data_export.errors.full_messages.to_sentence
end
end

Expand Down Expand Up @@ -117,7 +117,7 @@ def setup
assert_no_difference -> { DataExport.count } do
data_export = DataExports::CreateService.new(@user, invalid_params).execute
assert_equal I18n.t('services.data_exports.create.invalid_export_workflow_executions'),
data_export.errors.full_messages.first
data_export.errors.full_messages.to_sentence
end
end

Expand Down Expand Up @@ -218,7 +218,7 @@ def setup
assert_no_difference -> { DataExport.count } do
data_export = DataExports::CreateService.new(user, valid_params).execute
assert_equal I18n.t('services.data_exports.create.invalid_export_workflow_executions'),
data_export.errors.full_messages.first
data_export.errors.full_messages.to_sentence
end
end

Expand Down Expand Up @@ -273,7 +273,7 @@ def setup
assert_no_difference -> { DataExport.count } do
data_export = DataExports::CreateService.new(user, invalid_params).execute
assert_equal I18n.t('services.data_exports.create.invalid_export_samples'),
data_export.errors.full_messages.first
data_export.errors.full_messages.to_sentence
end
end

Expand All @@ -293,7 +293,7 @@ def setup
assert_no_difference -> { DataExport.count } do
data_export = DataExports::CreateService.new(user, invalid_params).execute
assert_equal I18n.t('services.data_exports.create.invalid_export_samples'),
data_export.errors.full_messages.first
data_export.errors.full_messages.to_sentence
end
end

Expand Down Expand Up @@ -498,7 +498,7 @@ def setup
assert_no_difference -> { DataExport.count } do
data_export = DataExports::CreateService.new(@user, params).execute
assert_equal I18n.t('services.data_exports.create.invalid_export_workflow_executions'),
data_export.errors.full_messages.first
data_export.errors.full_messages.to_sentence
end
end

Expand All @@ -511,7 +511,7 @@ def setup
assert_no_difference -> { DataExport.count } do
data_export = DataExports::CreateService.new(@user, params).execute
assert_equal I18n.t('services.data_exports.create.invalid_export_workflow_executions'),
data_export.errors.full_messages.first
data_export.errors.full_messages.to_sentence
end
end

Expand All @@ -524,7 +524,7 @@ def setup
assert_no_difference -> { DataExport.count } do
data_export = DataExports::CreateService.new(@user, params).execute
assert_equal I18n.t('services.data_exports.create.non_completed_workflow_executions'),
data_export.errors.full_messages.first
data_export.errors.full_messages.to_sentence
end
end

Expand All @@ -538,7 +538,7 @@ def setup
assert_no_difference -> { DataExport.count } do
data_export = DataExports::CreateService.new(@user, params).execute
assert_equal I18n.t('services.data_exports.create.non_completed_workflow_executions'),
data_export.errors.full_messages.first
data_export.errors.full_messages.to_sentence
end
end
end
Expand Down