Module: I18nCountrySelect::InstanceTag

Defined in:
lib/i18n_country_select/instance_tag.rb

Instance Method Summary collapse

Instance Method Details

#country_code_select(priority_countries, options, html_options) ⇒ Object

Adapted from Rails country_select. Just uses country codes instead of full names.



12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/i18n_country_select/instance_tag.rb', line 12

def country_code_select(priority_countries, options, html_options)
  selected = options.fetch(:selected, object.respond_to?(@method_name) ? object.send(@method_name) : nil)

  countries = ""

  if options.present? and options[:include_blank]
    option = options[:include_blank] == true ? "" : options[:include_blank]
    countries += "<option>#{option}</option>\n"
  end

  if priority_countries
    countries += options_for_select(priority_countries, selected)
    countries += "<option value=\"\" disabled=\"disabled\">-------------</option>\n"
  end

  countries = countries + options_for_select(country_translations, selected)

  html_options = html_options.stringify_keys
  add_default_name_and_id(html_options)

  (:select, countries.html_safe, html_options)
end

#country_translationsObject



35
36
37
38
39
40
41
42
43
44
45
# File 'lib/i18n_country_select/instance_tag.rb', line 35

def country_translations
  Thread.current[:country_translations] ||= {}
  Thread.current[:country_translations][I18n.locale] ||= begin
    (I18n.t 'countries').keys.map do |code|
      translation = I18n.t(code, :scope => :countries, :default => 'missing')
      translation == 'missing' ? nil : [translation, code]
    end.compact.sort_by do |translation, code|
      normalize_translation(translation)
    end
  end
end

#to_country_code_select_tag(priority_countries, html_options = {}, options = {}) ⇒ Object



3
4
5
6
7
8
9
# File 'lib/i18n_country_select/instance_tag.rb', line 3

def to_country_code_select_tag(priority_countries, html_options = {}, options = {})
  # Rails 4 stores options sent when creating an InstanceTag.
  # Let's use them!
  options = @options if defined?(@options)

  country_code_select(priority_countries, options, html_options)
end