Module: CountrySelect::TagHelper

Included in:
ActionView::Helpers::CountrySelect, ActionView::Helpers::Tags::CountrySelect
Defined in:
lib/country_select/tag_helper.rb

Instance Method Summary collapse

Instance Method Details

#country_option_tagsObject



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
34
# File 'lib/country_select/tag_helper.rb', line 4

def country_option_tags
  # In Rails 5.2+, `value` accepts no arguments and must also be called
  # called with parens to avoid the local variable of the same name
  # https://github.com/rails/rails/pull/29791
  selected_option = @options.fetch(:selected) do
    if self.method(:value).arity == 0
      value()
    else
      value(@object)
    end
  end

  option_tags_options = {
    :selected => selected_option,
    :disabled => @options[:disabled]
  }

  if priority_countries.present?
    priority_countries_options = country_options_for(priority_countries, false)

    option_tags = options_for_select(priority_countries_options, option_tags_options)
    option_tags += html_safe_newline + options_for_select([priority_countries_divider], disabled: priority_countries_divider)

    option_tags_options[:selected] = [option_tags_options[:selected]] unless option_tags_options[:selected].kind_of?(Array)
    option_tags_options[:selected].delete_if{|selected| priority_countries_options.map(&:second).include?(selected)}

    option_tags += html_safe_newline + options_for_select(country_options, option_tags_options)
  else
    option_tags = options_for_select(country_options, option_tags_options)
  end
end