Module: CountryStateSelect

Defined in:
lib/country_state_select/engine.rb,
lib/country_state_select.rb,
lib/country_state_select/version.rb,
lib/country_state_select/form_builder.rb,
lib/country_state_select/localization.rb,
lib/country_state_select/configuration.rb,
lib/country_state_select/country_metadata.rb,
lib/country_state_select/data_sources/base.rb,
lib/country_state_select/data_sources/city_state.rb,
app/controllers/country_state_select/cscs_controller.rb,
lib/generators/country_state_select/install/install_generator.rb

Overview

Author:

  • : Arvind Vyas

Defined Under Namespace

Modules: CountryMetadata, DataSources, FormBuilder, Generators, Localization, Rails Classes: Configuration, CscsController

Constant Summary collapse

VERSION =
'4.0.0'

Class Method Summary collapse

Class Method Details

.cities_collection(f, options) ⇒ Object

Return either the City (String) or Cities (Array)



55
56
57
# File 'lib/country_state_select.rb', line 55

def self.cities_collection(f, options)
  collect_cities(f.object.send(options[:state]), f.object.send(options[:country]))
end

.city_options(options) ⇒ Object

Return a hash for use in the simple_form



92
93
94
95
# File 'lib/country_state_select.rb', line 92

def self.city_options(options)
  cities = cities_collection(options[:form], options[:field_names])
  merge_hash(options, cities)
end

.collect_cities(state_id = '', country_id = '') ⇒ Object

Return the cities of given state and country.



79
80
81
82
83
# File 'lib/country_state_select.rb', line 79

def self.collect_cities(state_id = '', country_id = '')
  return [] if state_id.nil? || country_id.nil? || state_id.to_s.empty?

  configuration.data_source_instance.cities(state_id, country_id)
end

.collect_states(country, only: nil, except: nil, priority: nil) ⇒ Object

Return the collected States for a given Country, as [name, code] pairs — the shape simple_form/options_for_select expects (label, then value).



74
75
76
# File 'lib/country_state_select.rb', line 74

def self.collect_states(country, only: nil, except: nil, priority: nil)
  raw_states(country, only: only, except: except, priority: priority).collect { |code, name| [name, code] }
end

.configurationObject



76
77
78
# File 'lib/country_state_select/configuration.rb', line 76

def configuration
  @configuration ||= Configuration.new
end

.configure {|configuration| ... } ⇒ Object

Yields:



80
81
82
83
# File 'lib/country_state_select/configuration.rb', line 80

def configure
  yield(configuration)
  configuration
end

.countries_collection(options = {}) ⇒ Object

Collect the Countries as [label, code] pairs, honoring configuration (priority_countries, only/except, flags, dial_codes, localize_names) and any per-call overrides passed in options.

CountryStateSelect.countries_collection
CountryStateSelect.countries_collection(priority: %w[US IN], only: %w[US IN GB])


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

def self.countries_collection(options = {})
  config = configuration
  raw = config.data_source_instance.countries

  only = options.fetch(:only, config.only_countries)
  except = options.fetch(:except, config.except_countries)
  priority = options.fetch(:priority, config.priority_countries)

  raw = raw.select { |code, _| only.map(&:to_s).include?(code.to_s) } if only
  raw = raw.reject { |code, _| except.map(&:to_s).include?(code.to_s) } if except

  labeled = raw.collect do |code, name|
    localized = Localization.country_name(code, name)
    [CountryMetadata.decorate(code, localized), code]
  end

  reorder_by_priority(labeled, priority)
end

.countries_except(*except) ⇒ Object

Pass array of unwanted countries to get back all except those in the array. Kept for backward compatibility with 3.x; prefer countries_collection(except: ...).



41
42
43
44
# File 'lib/country_state_select.rb', line 41

def self.countries_except(*except)
  except = except.flatten.compact
  countries_collection(except: except.presence)
end

.merge_hash(options, collections) ⇒ Object

Create hash to use in the simple_form



98
99
100
101
102
# File 'lib/country_state_select.rb', line 98

def self.merge_hash(options, collections)
  options = options.merge(collection: collections)
  options = options.merge(as: :string) if collections.instance_of?(String)
  options
end

.raw_states(country, only: nil, except: nil, priority: nil) ⇒ Object

Return the raw [code, name] pairs for a given Country's states/provinces — the shape the JSON lookup endpoint hands to the front-end (element 0 is the option value, element 1 is the label).



62
63
64
65
66
67
68
69
70
# File 'lib/country_state_select.rb', line 62

def self.raw_states(country, only: nil, except: nil, priority: nil)
  return [] if country.nil? || country.to_s.empty?

  states = configuration.data_source_instance.states(country) # [[code, name], ...]
  states = states.select { |code, _| only.map(&:to_s).include?(code.to_s) } if only
  states = states.reject { |code, _| except.map(&:to_s).include?(code.to_s) } if except

  reorder_by_priority(states, priority, index: 0)
end

.reset_configuration!Object



85
86
87
# File 'lib/country_state_select/configuration.rb', line 85

def reset_configuration!
  @configuration = Configuration.new
end

.state_options(options) ⇒ Object

Return a hash for use in the simple_form



86
87
88
89
# File 'lib/country_state_select.rb', line 86

def self.state_options(options)
  states = states_collection(options[:form], options[:field_names])
  merge_hash(options, states)
end

.states_collection(f, options) ⇒ Object

Return either the State (String) or States (Array)



47
48
49
50
51
52
# File 'lib/country_state_select.rb', line 47

def self.states_collection(f, options)
  states = collect_states(f.object.send(options[:country]), **filter_opts(options))
  return f.object.send(options[:state]) if states.empty?

  states
end