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
Defined Under Namespace
Modules: CountryMetadata, DataSources, FormBuilder, Generators, Localization, Rails Classes: Configuration, CscsController
Constant Summary collapse
- VERSION =
'4.0.0'
Class Method Summary collapse
-
.cities_collection(f, options) ⇒ Object
Return either the City (String) or Cities (Array).
-
.city_options(options) ⇒ Object
Return a hash for use in the simple_form.
-
.collect_cities(state_id = '', country_id = '') ⇒ Object
Return the cities of given state and country.
-
.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).
- .configuration ⇒ Object
- .configure {|configuration| ... } ⇒ Object
-
.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. -
.countries_except(*except) ⇒ Object
Pass array of unwanted countries to get back all except those in the array.
-
.merge_hash(options, collections) ⇒ Object
Create hash to use in the simple_form.
-
.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).
- .reset_configuration! ⇒ Object
-
.state_options(options) ⇒ Object
Return a hash for use in the simple_form.
-
.states_collection(f, options) ⇒ Object
Return either the State (String) or States (Array).
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, ) collect_cities(f.object.send([:state]), f.object.send([: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.() cities = cities_collection([:form], [:field_names]) merge_hash(, 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 |
.configuration ⇒ Object
76 77 78 |
# File 'lib/country_state_select/configuration.rb', line 76 def configuration @configuration ||= Configuration.new end |
.configure {|configuration| ... } ⇒ Object
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( = {}) config = configuration raw = config.data_source_instance.countries only = .fetch(:only, config.only_countries) except = .fetch(:except, config.except_countries) priority = .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(, collections) = .merge(collection: collections) = .merge(as: :string) if collections.instance_of?(String) 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.() states = states_collection([:form], [:field_names]) merge_hash(, 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, ) states = collect_states(f.object.send([:country]), **filter_opts()) return f.object.send([:state]) if states.empty? states end |