Module: CapybaraSelect2::Utils

Defined in:
lib/capybara_select2/utils.rb

Class Method Summary collapse

Class Method Details

.detect_select2_version(container) ⇒ Object



4
5
6
7
8
9
10
11
12
# File 'lib/capybara_select2/utils.rb', line 4

def self.detect_select2_version(container)
  if container['class'] =~ /^select2\s/
    '4'
  elsif container['id'] =~ /^s2id_/
    '3'
  else
    '2'
  end
end

.find_select2_container(options, page) ⇒ Object



20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/capybara_select2/utils.rb', line 20

def self.find_select2_container(options, page)
  container = if options[:xpath]
    page.find(:xpath, options[:xpath])
  elsif options[:css]
    page.find(:css, options[:css])
  else
    page.find(:css, "label:not(.select2-offscreen)", text: options[:from])
      .find(:xpath, '..')
      .find(:css, '.select2-container')
  end

  if container['class'] =~ /select2-container/
    container
  else
    container.find(:css, '.select2-container')
  end
end

.validate_options!(options) ⇒ Object



14
15
16
17
18
# File 'lib/capybara_select2/utils.rb', line 14

def self.validate_options!(options)
  unless options.is_a?(Hash) && [:css, :xpath, :from].any? { |k| options.key?(k) }
    fail ArgumentError.new("Please specify :css, :xpath or :from in options")
  end
end