summaryrefslogtreecommitdiff
path: root/spec/support/select2_helper.rb
blob: 6b1853c2364a7f5be569a845cc53b030819571a5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# Select2 ajax programmatic helper
# It allows you to select value from select2
#
# Params
#   value - real value of selected item
#   opts - options containing css selector
#
# Usage:
#
#   select2(2, from: '#user_ids')
#

module Select2Helper
  def select2(value, options = {})
    raise ArgumentError, 'options must be a Hash' unless options.is_a?(Hash)

    selector = options.fetch(:from)

    if options[:multiple]
      execute_script("$('#{selector}').select2('val', ['#{value}']).trigger('change');")
    else
      execute_script("$('#{selector}').select2('val', '#{value}').trigger('change');")
    end
  end

  def open_select2(selector)
    execute_script("$('#{selector}').select2('open');")
  end

  def scroll_select2_to_bottom(selector)
    evaluate_script "$('#{selector}').scrollTop($('#{selector}')[0].scrollHeight); $('#{selector}');"
  end
end