form_helper.rb 3.92 KB
Newer Older
1 2
# frozen_string_literal: true

3
module FormHelper
4
  def form_errors(model, type: 'form', truncate: [])
5 6 7 8 9 10 11 12 13
    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 }
14

15
    truncate = Array.wrap(truncate)
16

17 18 19 20 21 22 23 24 25
    tag.div(class: 'alert alert-danger', id: 'error_explanation') do
      tag.h4(headline) <<
        tag.ul do
          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)
26

27
            tag.li(message)
28 29 30
          end

          messages.join.html_safe
31
        end
32 33
    end
  end
34

35 36
  def assignees_dropdown_options(issuable_type)
    dropdown_data = {
Clement Ho's avatar
Clement Ho committed
37
      toggle_class: 'js-user-search js-assignee-search js-multiselect js-save-user-data',
38
      title: _('Select assignee'),
39 40
      filter: true,
      dropdown_class: 'dropdown-menu-user dropdown-menu-selectable dropdown-menu-assignee',
41
      placeholder: _('Search users'),
42 43 44 45
      data: {
        first_user: current_user&.username,
        null_user: true,
        current_user: true,
46 47
        project_id: (@target_project || @project)&.id,
        field_name: "#{issuable_type}[assignee_ids][]",
48
        default_label: _('Unassigned'),
49
        'max-select': 1,
50
        'dropdown-header': _('Assignee'),
Clement Ho's avatar
Clement Ho committed
51 52 53
        multi_select: true,
        'input-meta': 'name',
        'always-show-selectbox': true,
54
        current_user_info: UserSerializer.new.represent(current_user)
55 56
      }
    }
57 58 59 60 61 62 63 64 65 66 67

    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

68
  def reviewers_dropdown_options(issuable_type, iid = nil, target_branch = nil)
69
    dropdown_data = {
Samantha Ming's avatar
Samantha Ming committed
70 71 72 73 74 75 76 77 78
      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,
79 80
        project_id: (@target_project || @project)&.id,
        field_name: "#{issuable_type}[reviewer_ids][]",
Samantha Ming's avatar
Samantha Ming committed
81
        default_label: 'Unassigned',
82 83
        'max-select': 1,
        'dropdown-header': 'Reviewer',
Samantha Ming's avatar
Samantha Ming committed
84 85 86 87 88 89
        multi_select: true,
        'input-meta': 'name',
        'always-show-selectbox': true,
        current_user_info: UserSerializer.new.represent(current_user)
      }
    }
90

91 92 93 94 95 96 97 98
    if iid
      dropdown_data[:data][:iid] = iid
    end

    if target_branch
      dropdown_data[:data][:target_branch] = target_branch
    end

99 100 101 102 103
    if merge_request_supports_multiple_reviewers?
      dropdown_data = multiple_reviewers_dropdown_options(dropdown_data)
    end

    dropdown_data
Samantha Ming's avatar
Samantha Ming committed
104 105
  end

106 107 108 109 110 111 112 113 114 115
  # Overwritten
  def issue_supports_multiple_assignees?
    false
  end

  # Overwritten
  def merge_request_supports_multiple_assignees?
    false
  end

116 117 118 119 120
  # Overwritten
  def merge_request_supports_multiple_reviewers?
    false
  end

121 122 123 124 125
  private

  def multiple_assignees_dropdown_options(options)
    new_options = options.dup

126
    new_options[:title] = _('Select assignee(s)')
127 128 129 130
    new_options[:data][:'dropdown-header'] = 'Assignee(s)'
    new_options[:data].delete(:'max-select')

    new_options
131
  end
132 133 134 135 136 137 138 139 140 141

  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
142
end
143

144
FormHelper.prepend_mod_with('FormHelper')