BigW Consortium Gitlab

selects_helper.rb 1.92 KB
Newer Older
1 2 3 4
module SelectsHelper
  def users_select_tag(id, opts = {})
    css_class = "ajax-users-select "
    css_class << "multiselect " if opts[:multiple]
5
    css_class << "skip_ldap " if opts[:skip_ldap]
6 7
    css_class << (opts[:class] || '')
    value = opts[:selected] || ''
8 9
    html = {
      class: css_class,
10
      data: users_select_data_attributes(opts)
11 12 13
    }

    unless opts[:scope] == :all
14 15
      project = opts[:project] || @project

16 17
      if project
        html['data-project-id'] = project.id
18 19 20 21 22 23
      elsif @group
        html['data-group-id'] = @group.id
      end
    end

    hidden_field_tag(id, value, html)
24
  end
25 26

  def groups_select_tag(id, opts = {})
27 28 29 30 31 32 33 34 35 36 37
    opts[:class] ||= ''
    opts[:class] << ' ajax-groups-select'
    select2_tag(id, opts)
  end

  def namespace_select_tag(id, opts = {})
    opts[:class] ||= ''
    opts[:class] << ' ajax-namespace-select'
    select2_tag(id, opts)
  end

38 39 40 41 42 43 44 45 46 47 48 49 50
  def project_select_tag(id, opts = {})
    opts[:class] ||= ''
    opts[:class] << ' ajax-project-select'

    unless opts.delete(:scope) == :all
      if @group
        opts['data-group-id'] = @group.id
      end
    end

    hidden_field_tag(id, opts[:selected], opts)
  end

51
  def select2_tag(id, opts = {})
barthc committed
52
    opts[:class] << ' multiselect' if opts[:multiple]
53 54
    value = opts[:selected] || ''

barthc committed
55
    hidden_field_tag(id, value, opts)
56
  end
57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72

  private

  def users_select_data_attributes(opts)
    {
      placeholder: opts[:placeholder]   || 'Search for a user',
      null_user: opts[:null_user]       || false,
      any_user: opts[:any_user]         || false,
      email_user: opts[:email_user]     || false,
      first_user: opts[:first_user] && current_user ? current_user.username : false,
      current_user: opts[:current_user] || false,
      "push-code-to-protected-branches" => opts[:push_code_to_protected_branches],
      author_id: opts[:author_id] || '',
      skip_users: opts[:skip_users] ? opts[:skip_users].map(&:id) : nil,
    }
  end
73
end