debian-mirror-gitlab/app/helpers/form_helper.rb

175 lines
4.7 KiB
Ruby
Raw Normal View History

2018-12-05 23:21:45 +05:30
# frozen_string_literal: true
2016-06-02 11:05:42 +05:30
module FormHelper
2022-07-23 23:45:48 +05:30
def form_errors(model, type: 'form', truncate: [], pajamas_alert: false)
2021-09-04 01:27:46 +05:30
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-06-02 11:05:42 +05:30
2020-04-08 14:13:33 +05:30
truncate = Array.wrap(truncate)
2016-06-02 11:05:42 +05:30
2022-07-23 23:45:48 +05:30
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
if pajamas_alert
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
2020-04-08 14:13:33 +05:30
end
2017-08-17 22:00:37 +05:30
end
2022-07-23 23:45:48 +05:30
end
else
tag.div(class: 'alert alert-danger', id: 'error_explanation') do
tag.h4(headline) <<
tag.ul do
messages
end
end
2016-06-02 11:05:42 +05:30
end
end
2017-08-17 22:00:37 +05:30
2019-07-31 22:56:46 +05:30
def assignees_dropdown_options(issuable_type)
dropdown_data = {
2017-08-17 22:00:37 +05:30
toggle_class: 'js-user-search js-assignee-search js-multiselect js-save-user-data',
2022-01-26 12:08:38 +05:30
title: _('Select assignee'),
2017-08-17 22:00:37 +05:30
filter: true,
dropdown_class: 'dropdown-menu-user dropdown-menu-selectable dropdown-menu-assignee',
2019-07-07 11:18:12 +05:30
placeholder: _('Search users'),
2017-08-17 22:00:37 +05:30
data: {
first_user: current_user&.username,
null_user: true,
current_user: true,
2019-07-31 22:56:46 +05:30
project_id: (@target_project || @project)&.id,
field_name: "#{issuable_type}[assignee_ids][]",
2022-01-26 12:08:38 +05:30
default_label: _('Unassigned'),
2017-08-17 22:00:37 +05:30
'max-select': 1,
2022-01-26 12:08:38 +05:30
'dropdown-header': _('Assignee'),
2017-08-17 22:00:37 +05:30
multi_select: true,
'input-meta': 'name',
'always-show-selectbox': true,
2018-03-17 18:26:18 +05:30
current_user_info: UserSerializer.new.represent(current_user)
2017-08-17 22:00:37 +05:30
}
}
2019-07-31 22:56:46 +05:30
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
2021-02-22 17:27:13 +05:30
def reviewers_dropdown_options(issuable_type, iid = nil, target_branch = nil)
2021-01-03 14:25:43 +05:30
dropdown_data = {
2020-11-24 15:15:51 +05:30
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',
2021-01-03 14:25:43 +05:30
'max-select': 1,
'dropdown-header': 'Reviewer',
2020-11-24 15:15:51 +05:30
multi_select: true,
'input-meta': 'name',
'always-show-selectbox': true,
current_user_info: UserSerializer.new.represent(current_user)
}
}
2021-01-03 14:25:43 +05:30
2021-02-22 17:27:13 +05:30
if iid
dropdown_data[:data][:iid] = iid
end
if target_branch
dropdown_data[:data][:target_branch] = target_branch
end
2021-01-03 14:25:43 +05:30
if merge_request_supports_multiple_reviewers?
dropdown_data = multiple_reviewers_dropdown_options(dropdown_data)
end
dropdown_data
2020-11-24 15:15:51 +05:30
end
2019-07-31 22:56:46 +05:30
# Overwritten
def issue_supports_multiple_assignees?
false
end
# Overwritten
def merge_request_supports_multiple_assignees?
false
end
2021-01-03 14:25:43 +05:30
# Overwritten
def merge_request_supports_multiple_reviewers?
false
end
2019-07-31 22:56:46 +05:30
private
2022-07-23 23:45:48 +05:30
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-07-31 22:56:46 +05:30
def multiple_assignees_dropdown_options(options)
new_options = options.dup
2022-01-26 12:08:38 +05:30
new_options[:title] = _('Select assignee(s)')
2019-07-31 22:56:46 +05:30
new_options[:data][:'dropdown-header'] = 'Assignee(s)'
new_options[:data].delete(:'max-select')
new_options
2017-08-17 22:00:37 +05:30
end
2021-01-03 14:25:43 +05:30
def multiple_reviewers_dropdown_options(options)
new_options = options.dup
new_options[:title] = _('Select reviewer(s)')
new_options[:data][:'dropdown-header'] = _('Reviewer(s)')
new_options[:data].delete(:'max-select')
new_options
end
2016-06-02 11:05:42 +05:30
end
2020-05-24 23:13:21 +05:30
2021-06-08 01:23:25 +05:30
FormHelper.prepend_mod_with('FormHelper')