2018-08-18 07:19:57 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-04-04 21:25:38 -04:00
|
|
|
module FormHelper
|
2022-08-18 17:12:09 -04:00
|
|
|
def form_errors(model, type: 'form', truncate: [])
|
2021-06-10 14:10:05 -04:00
|
|
|
errors = model.errors
|
|
|
|
|
|
|
|
return unless errors.any?
|
|
|
|
|
|
|
|
headline = n_(
|
|
|
|
'The %{type} contains the following error:',
|
|
|
|
'The %{type} contains the following errors:',
|
|
|
|
errors.count
|
|
|
|
) % { type: type }
|
2016-04-04 21:25:38 -04:00
|
|
|
|
2020-03-02 13:07:42 -05:00
|
|
|
truncate = Array.wrap(truncate)
|
2016-04-04 21:25:38 -04:00
|
|
|
|
2022-06-10 08:09:36 -04:00
|
|
|
messages = errors.map do |error|
|
|
|
|
attribute = error.attribute
|
|
|
|
message = error.message
|
|
|
|
|
|
|
|
message = html_escape_once(errors.full_message(attribute, message)).html_safe
|
|
|
|
message = tag.span(message, class: 'str-truncated-100') if truncate.include?(attribute)
|
|
|
|
message = append_help_page_link(message, error.options) if error.options[:help_page_url].present?
|
|
|
|
|
|
|
|
tag.li(message)
|
|
|
|
end.join.html_safe
|
|
|
|
|
2022-08-12 23:09:28 -04:00
|
|
|
render Pajamas::AlertComponent.new(
|
|
|
|
variant: :danger,
|
|
|
|
title: headline,
|
|
|
|
dismissible: false,
|
|
|
|
alert_options: { id: 'error_explanation', class: 'gl-mb-5' }
|
|
|
|
) do |c|
|
|
|
|
c.body do
|
|
|
|
tag.ul(class: 'gl-pl-5 gl-mb-0') do
|
|
|
|
messages
|
2016-12-15 17:14:20 -05:00
|
|
|
end
|
2022-06-10 08:09:36 -04:00
|
|
|
end
|
2016-04-04 21:25:38 -04:00
|
|
|
end
|
|
|
|
end
|
2017-05-04 08:11:15 -04:00
|
|
|
|
2022-10-11 14:09:43 -04:00
|
|
|
def dropdown_max_select(data, feature_flag)
|
|
|
|
return data[:'max-select'] unless Feature.enabled?(feature_flag)
|
2022-08-02 20:10:17 -04:00
|
|
|
|
2022-10-11 14:09:43 -04:00
|
|
|
if data[:'max-select'] && data[:'max-select'] < ::Issuable::MAX_NUMBER_OF_ASSIGNEES_OR_REVIEWERS
|
2022-08-02 20:10:17 -04:00
|
|
|
data[:'max-select']
|
|
|
|
else
|
2022-10-11 14:09:43 -04:00
|
|
|
::Issuable::MAX_NUMBER_OF_ASSIGNEES_OR_REVIEWERS
|
2022-08-02 20:10:17 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-07 14:35:16 -04:00
|
|
|
def assignees_dropdown_options(issuable_type)
|
|
|
|
dropdown_data = {
|
2017-05-04 16:55:36 -04:00
|
|
|
toggle_class: 'js-user-search js-assignee-search js-multiselect js-save-user-data',
|
2021-12-03 04:10:57 -05:00
|
|
|
title: _('Select assignee'),
|
2017-05-04 08:11:15 -04:00
|
|
|
filter: true,
|
|
|
|
dropdown_class: 'dropdown-menu-user dropdown-menu-selectable dropdown-menu-assignee',
|
2019-04-04 13:05:25 -04:00
|
|
|
placeholder: _('Search users'),
|
2017-05-04 08:11:15 -04:00
|
|
|
data: {
|
|
|
|
first_user: current_user&.username,
|
|
|
|
null_user: true,
|
|
|
|
current_user: true,
|
2019-04-07 14:35:16 -04:00
|
|
|
project_id: (@target_project || @project)&.id,
|
|
|
|
field_name: "#{issuable_type}[assignee_ids][]",
|
2021-12-03 04:10:57 -05:00
|
|
|
default_label: _('Unassigned'),
|
2017-05-04 08:11:15 -04:00
|
|
|
'max-select': 1,
|
2022-08-26 08:11:48 -04:00
|
|
|
'dropdown-header': s_('SearchToken|Assignee'),
|
2017-05-04 16:55:36 -04:00
|
|
|
multi_select: true,
|
|
|
|
'input-meta': 'name',
|
|
|
|
'always-show-selectbox': true,
|
2017-09-19 14:22:43 -04:00
|
|
|
current_user_info: UserSerializer.new.represent(current_user)
|
2017-05-04 08:11:15 -04:00
|
|
|
}
|
|
|
|
}
|
2019-04-07 14:35:16 -04:00
|
|
|
|
|
|
|
type = issuable_type.to_s
|
|
|
|
|
|
|
|
if type == 'issue' && issue_supports_multiple_assignees? ||
|
|
|
|
type == 'merge_request' && merge_request_supports_multiple_assignees?
|
|
|
|
dropdown_data = multiple_assignees_dropdown_options(dropdown_data)
|
|
|
|
end
|
|
|
|
|
|
|
|
dropdown_data
|
|
|
|
end
|
|
|
|
|
2020-12-16 16:09:57 -05:00
|
|
|
def reviewers_dropdown_options(issuable_type, iid = nil, target_branch = nil)
|
2020-10-07 11:08:40 -04:00
|
|
|
dropdown_data = {
|
2020-09-14 08:09:34 -04:00
|
|
|
toggle_class: 'js-reviewer-search js-multiselect js-save-user-data',
|
|
|
|
title: 'Request review from',
|
|
|
|
filter: true,
|
|
|
|
dropdown_class: 'dropdown-menu-user dropdown-menu-selectable dropdown-menu-reviewer',
|
|
|
|
placeholder: _('Search users'),
|
|
|
|
data: {
|
|
|
|
first_user: current_user&.username,
|
|
|
|
null_user: true,
|
|
|
|
current_user: true,
|
|
|
|
project_id: (@target_project || @project)&.id,
|
|
|
|
field_name: "#{issuable_type}[reviewer_ids][]",
|
|
|
|
default_label: 'Unassigned',
|
2020-10-07 11:08:40 -04:00
|
|
|
'max-select': 1,
|
|
|
|
'dropdown-header': 'Reviewer',
|
2020-09-14 08:09:34 -04:00
|
|
|
multi_select: true,
|
|
|
|
'input-meta': 'name',
|
|
|
|
'always-show-selectbox': true,
|
|
|
|
current_user_info: UserSerializer.new.represent(current_user)
|
|
|
|
}
|
|
|
|
}
|
2020-10-07 11:08:40 -04:00
|
|
|
|
2020-12-16 16:09:57 -05:00
|
|
|
if iid
|
|
|
|
dropdown_data[:data][:iid] = iid
|
|
|
|
end
|
|
|
|
|
|
|
|
if target_branch
|
|
|
|
dropdown_data[:data][:target_branch] = target_branch
|
|
|
|
end
|
|
|
|
|
2020-10-07 11:08:40 -04:00
|
|
|
if merge_request_supports_multiple_reviewers?
|
|
|
|
dropdown_data = multiple_reviewers_dropdown_options(dropdown_data)
|
|
|
|
end
|
|
|
|
|
2022-09-23 14:12:59 -04:00
|
|
|
dropdown_data[:data].merge!(reviewers_dropdown_options_for_suggested_reviewers)
|
2020-10-07 11:08:40 -04:00
|
|
|
dropdown_data
|
2020-09-14 08:09:34 -04:00
|
|
|
end
|
|
|
|
|
2022-09-23 14:12:59 -04:00
|
|
|
# Overwritten
|
|
|
|
def reviewers_dropdown_options_for_suggested_reviewers
|
|
|
|
{}
|
|
|
|
end
|
|
|
|
|
2019-04-07 14:35:16 -04:00
|
|
|
# Overwritten
|
|
|
|
def issue_supports_multiple_assignees?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
|
|
|
# Overwritten
|
|
|
|
def merge_request_supports_multiple_assignees?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2020-10-07 11:08:40 -04:00
|
|
|
# Overwritten
|
|
|
|
def merge_request_supports_multiple_reviewers?
|
|
|
|
false
|
|
|
|
end
|
|
|
|
|
2019-04-07 14:35:16 -04:00
|
|
|
private
|
|
|
|
|
2022-06-08 02:08:10 -04:00
|
|
|
def append_help_page_link(message, options)
|
|
|
|
help_page_url = options[:help_page_url]
|
|
|
|
help_link_text = options[:help_link_text] || _('Learn more.')
|
|
|
|
|
|
|
|
help_link = link_to(
|
|
|
|
help_link_text,
|
|
|
|
help_page_url,
|
|
|
|
target: '_blank',
|
|
|
|
rel: 'noopener noreferrer'
|
|
|
|
)
|
|
|
|
|
|
|
|
message + " #{help_link}".html_safe
|
|
|
|
end
|
|
|
|
|
2019-04-07 14:35:16 -04:00
|
|
|
def multiple_assignees_dropdown_options(options)
|
|
|
|
new_options = options.dup
|
|
|
|
|
2021-12-03 04:10:57 -05:00
|
|
|
new_options[:title] = _('Select assignee(s)')
|
2019-04-07 14:35:16 -04:00
|
|
|
new_options[:data][:'dropdown-header'] = 'Assignee(s)'
|
2022-10-11 14:09:43 -04:00
|
|
|
|
|
|
|
if Feature.enabled?(:limit_assignees_per_issuable)
|
|
|
|
new_options[:data][:'max-select'] = ::Issuable::MAX_NUMBER_OF_ASSIGNEES_OR_REVIEWERS
|
|
|
|
else
|
|
|
|
new_options[:data].delete(:'max-select')
|
|
|
|
end
|
2019-04-07 14:35:16 -04:00
|
|
|
|
|
|
|
new_options
|
2017-05-04 08:11:15 -04:00
|
|
|
end
|
2020-10-07 11:08:40 -04:00
|
|
|
|
|
|
|
def multiple_reviewers_dropdown_options(options)
|
|
|
|
new_options = options.dup
|
|
|
|
|
|
|
|
new_options[:title] = _('Select reviewer(s)')
|
|
|
|
new_options[:data][:'dropdown-header'] = _('Reviewer(s)')
|
2022-08-02 20:10:17 -04:00
|
|
|
|
|
|
|
if Feature.enabled?(:limit_reviewer_and_assignee_size)
|
2022-10-11 14:09:43 -04:00
|
|
|
new_options[:data][:'max-select'] = ::Issuable::MAX_NUMBER_OF_ASSIGNEES_OR_REVIEWERS
|
2022-08-02 20:10:17 -04:00
|
|
|
else
|
|
|
|
new_options[:data].delete(:'max-select')
|
|
|
|
end
|
2020-10-07 11:08:40 -04:00
|
|
|
|
|
|
|
new_options
|
|
|
|
end
|
2016-04-04 21:25:38 -04:00
|
|
|
end
|
2020-05-14 08:08:21 -04:00
|
|
|
|
2021-05-11 17:10:21 -04:00
|
|
|
FormHelper.prepend_mod_with('FormHelper')
|