Skip to content
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
9 changes: 8 additions & 1 deletion lib/devise/failure_app.rb
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,14 @@ def i18n_message(default = nil)
human_keys = (auth_keys.respond_to?(:keys) ? auth_keys.keys : auth_keys).map { |key|
# TODO: Remove the fallback and just use `downcase_first` once we drop support for Rails 7.0.
human_key = scope_class.human_attribute_name(key)
human_key.respond_to?(:downcase_first) ? human_key.downcase_first : human_key[0].downcase + human_key[1..]
# Only downcase single-word names ("Email" -> "email"). Downcasing just
# the first letter of multi-word title-cased names ("Company Code" ->
# "company Code") reads like a typo, so those keep their original casing.
if human_key.match?(/\s/)
human_key
else
human_key.respond_to?(:downcase_first) ? human_key.downcase_first : human_key[0].downcase + human_key[1..]
end
}
options[:authentication_keys] = human_keys.join(I18n.t(:"support.array.words_connector"))
options = i18n_options(options)
Expand Down
9 changes: 9 additions & 0 deletions test/failure_app_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -200,6 +200,15 @@ def call_failure(env_params = {})
assert_equal 'Invalid email or password.', @request.flash[:alert]
end

test 'keeps original casing of multi-word authentication_keys in the flash message' do
store_translations :en, activerecord: { attributes: { user: { company_cd: 'Company Code' } } } do
swap Devise, authentication_keys: [:company_cd] do
call_failure('warden' => OpenStruct.new(message: :invalid))
assert_equal 'Invalid Company Code or password.', @request.flash[:alert]
end
end
end

test 'humanizes the flash message' do
call_failure('warden' => OpenStruct.new(message: :invalid))
assert_equal @request.flash[:alert], @request.flash[:alert].humanize
Expand Down