Commit 1ea589cb authored by Luke Duncalfe's avatar Luke Duncalfe

Add .to_exclusive_sentence to GitLab::Utils

Wraps ActiveSupport's Array#to_sentence to convert the given array to a
comma-separated sentence joined with localized 'or' Strings instead of
'and'.
parent db0c0b74
...@@ -89,7 +89,7 @@ module DiffViewer ...@@ -89,7 +89,7 @@ module DiffViewer
{ {
viewer: switcher_title, viewer: switcher_title,
reason: render_error_reason, reason: render_error_reason,
options: render_error_options.to_sentence(two_words_connector: _(' or '), last_word_connector: _(', or ')) options: Gitlab::Utils.to_exclusive_sentence(render_error_options)
} }
end end
......
...@@ -142,13 +142,9 @@ class Key < ApplicationRecord ...@@ -142,13 +142,9 @@ class Key < ApplicationRecord
end end
def forbidden_key_type_message def forbidden_key_type_message
allowed_types = allowed_types = Gitlab::CurrentSettings.allowed_key_types.map(&:upcase)
Gitlab::CurrentSettings
.allowed_key_types
.map(&:upcase)
.to_sentence(last_word_connector: ', or ', two_words_connector: ' or ')
"type is forbidden. Must be #{allowed_types}" "type is forbidden. Must be #{Gitlab::Utils.to_exclusive_sentence(allowed_types)}"
end end
end end
......
...@@ -21,7 +21,8 @@ class KeyRestrictionValidator < ActiveModel::EachValidator ...@@ -21,7 +21,8 @@ class KeyRestrictionValidator < ActiveModel::EachValidator
def supported_sizes_message def supported_sizes_message
sizes = self.class.supported_sizes(options[:type]) sizes = self.class.supported_sizes(options[:type])
sizes.to_sentence(last_word_connector: ', or ', two_words_connector: ' or ')
Gitlab::Utils.to_exclusive_sentence(sizes)
end end
def valid_restriction?(value) def valid_restriction?(value)
......
...@@ -3,5 +3,5 @@ ...@@ -3,5 +3,5 @@
The #{viewer.switcher_title} could not be displayed because #{blob_render_error_reason(viewer)}. The #{viewer.switcher_title} could not be displayed because #{blob_render_error_reason(viewer)}.
You can You can
= blob_render_error_options(viewer).to_sentence(two_words_connector: ' or ', last_word_connector: ', or ').html_safe = Gitlab::Utils.to_exclusive_sentence(blob_render_error_options(viewer)).html_safe
instead. instead.
...@@ -4,6 +4,6 @@ After you've reviewed these contribution guidelines, you'll be all set to ...@@ -4,6 +4,6 @@ After you've reviewed these contribution guidelines, you'll be all set to
- options = contribution_options(viewer.project) - options = contribution_options(viewer.project)
- if options.any? - if options.any?
= succeed '.' do = succeed '.' do
= options.to_sentence(two_words_connector: ' or ', last_word_connector: ', or ').html_safe = Gitlab::Utils.to_exclusive_sentence(options).html_safe
- else - else
contribute to this project. contribute to this project.
...@@ -50,6 +50,12 @@ module Gitlab ...@@ -50,6 +50,12 @@ module Gitlab
.gsub(/(\A-+|-+\z)/, '') .gsub(/(\A-+|-+\z)/, '')
end end
# Wraps ActiveSupport's Array#to_sentence to convert the given array to a
# comma-separated sentence joined with localized 'or' Strings instead of 'and'.
def to_exclusive_sentence(array)
array.to_sentence(two_words_connector: _(' or '), last_word_connector: _(', or '))
end
# Converts newlines into HTML line break elements # Converts newlines into HTML line break elements
def nlbr(str) def nlbr(str)
ActionView::Base.full_sanitizer.sanitize(+str, tags: []).gsub(/\r?\n/, '<br>').html_safe ActionView::Base.full_sanitizer.sanitize(+str, tags: []).gsub(/\r?\n/, '<br>').html_safe
......
...@@ -3,8 +3,9 @@ ...@@ -3,8 +3,9 @@
require 'spec_helper' require 'spec_helper'
describe Gitlab::Utils do describe Gitlab::Utils do
delegate :to_boolean, :boolean_to_yes_no, :slugify, :random_string, :which, :ensure_array_from_string, delegate :to_boolean, :boolean_to_yes_no, :slugify, :random_string, :which,
:bytes_to_megabytes, :append_path, :check_path_traversal!, to: :described_class :ensure_array_from_string, :to_exclusive_sentence, :bytes_to_megabytes,
:append_path, :check_path_traversal!, to: :described_class
describe '.check_path_traversal!' do describe '.check_path_traversal!' do
it 'detects path traversal at the start of the string' do it 'detects path traversal at the start of the string' do
...@@ -46,6 +47,36 @@ describe Gitlab::Utils do ...@@ -46,6 +47,36 @@ describe Gitlab::Utils do
end end
end end
describe '.to_exclusive_sentence' do
it 'calls #to_sentence on the array' do
array = double
expect(array).to receive(:to_sentence)
to_exclusive_sentence(array)
end
it 'joins arrays with two elements correctly' do
array = %w(foo bar)
expect(to_exclusive_sentence(array)).to eq('foo or bar')
end
it 'joins arrays with more than two elements correctly' do
array = %w(foo bar baz)
expect(to_exclusive_sentence(array)).to eq('foo, bar, or baz')
end
it 'localizes the connector words' do
array = %w(foo bar baz)
expect(described_class).to receive(:_).with(' or ').and_return(' <1> ')
expect(described_class).to receive(:_).with(', or ').and_return(', <2> ')
expect(to_exclusive_sentence(array)).to eq('foo, bar, <2> baz')
end
end
describe '.nlbr' do describe '.nlbr' do
it 'replaces new lines with <br>' do it 'replaces new lines with <br>' do
expect(described_class.nlbr("<b>hello</b>\n<i>world</i>".freeze)).to eq("hello<br>world") expect(described_class.nlbr("<b>hello</b>\n<i>world</i>".freeze)).to eq("hello<br>world")
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment