Class: CountryOptionsSelect

Inherits:
Object
  • Object
show all
Defined in:
lib/country_options_select.rb,
lib/country_options_select/engine.rb,
lib/country_options_select/version.rb,
lib/country_options_select/helpers/select_tag.rb,
lib/country_options_select/helpers/form_tag_helper.rb,
lib/country_options_select/helpers/form_options_helper.rb

Defined Under Namespace

Modules: FormBuilder, FormOptionsHelper, FormTagHelper Classes: CountryOptionsSelectTag, Engine

Constant Summary collapse

VERSION =
"0.1.3"

Class Method Summary collapse

Class Method Details

.remove_options(options) ⇒ Object



52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/country_options_select.rb', line 52

def remove_options(options)
  countries = options.deep_dup
  countries_with_select_types = countries.extract_options!.transform_keys{|country| country.to_s.upcase}.
                                transform_values{|select_types| select_types.map{|select_type| select_type.to_s.downcase}}

  custom_options = self.custom_options
  all_current_options = default_options.merge(custom_options)

  countries.map{|country| country.to_s.upcase}.each{|country| custom_options[country] = nil if all_current_options[country]}

  countries_with_select_types.each do |country, select_types|
    select_types.each do |select_type|
      if all_current_options.dig(country, select_type)
        custom_options[country] ||= {}
        custom_options[country][select_type] = nil
      end
    end
  end

  File.write(custom_options_path, YAML.dump(custom_options)) if custom_options.present?
end

.transform_updates(options) ⇒ Object



44
45
46
47
48
49
50
# File 'lib/country_options_select.rb', line 44

def transform_updates(options)
  options.deep_dup.
  transform_keys{|iso_code| iso_code.to_s.upcase}.
  transform_values{|select_types| select_types.transform_keys{|select_type| select_type.to_s.downcase}.
                                  slice(*%w(name currency dial_code capital))}.
  reject{|_, select_types| select_types.blank?} if options.present?
end

.update_options(options) ⇒ Object



38
39
40
41
42
# File 'lib/country_options_select.rb', line 38

def update_options(options)
  options = transform_updates(options)

  File.write(custom_options_path, YAML.dump(custom_options.deep_merge(options))) if options.present?
end

.update_options_from_apiObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
# File 'lib/country_options_select.rb', line 16

def update_options_from_api
  latest_response_options = {}

  %w(dial_code currency capital).each do |option|
    iso_code_key = option == 'dial_code' ? 'code' : 'iso2'
    JSON.parse(URI("https://countriesnow.space/api/v0.1/countries/#{option == 'dial_code' ? 'codes' : option}").
         open(&:read))['data'].each do |response_options|
           unless response_options[option].blank?
             latest_response_options[response_options[iso_code_key]] ||= {}
             latest_response_options[response_options[iso_code_key]]['name'] ||= response_options['name'].squish
             latest_response_options[response_options[iso_code_key]][option] = response_options[option].squish
             latest_response_options[response_options[iso_code_key]][option].delete!(' ') if option == 'dial_code'
           end
    end
  end
  options_diff(latest_response_options, default_options)

  if latest_response_options.present?
    File.write(custom_options_path, YAML.dump(latest_response_options.deep_merge(custom_options)))
  end
end

.update_options_from_customObject



11
12
13
14
# File 'lib/country_options_select.rb', line 11

def update_options_from_custom
  country_options.deep_merge!(custom_options).compact!
  country_options.each{|_, options| options.compact!}
end

.update_options_from_defaultObject



7
8
9
# File 'lib/country_options_select.rb', line 7

def update_options_from_default
  self.country_options = default_options
end