Module: CountryStateSelect

Defined in:
lib/country_state_select/engine.rb,
lib/country_state_select.rb,
lib/country_state_select/engine3.rb,
lib/country_state_select/railtie.rb,
lib/country_state_select/version.rb,
app/controllers/country_state_select/cscs_controller.rb

Overview

Author:

  • : Arvind Vyas

Defined Under Namespace

Modules: Rails Classes: CscsController

Constant Summary collapse

VERSION =
"3.0.3"

Class Method Summary collapse

Class Method Details

.cities_collection(f, options) ⇒ Object

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



25
26
27
28
# File 'lib/country_state_select.rb', line 25

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

.city_options(options) ⇒ Object

Return a hash for use in the simple_form



52
53
54
55
# File 'lib/country_state_select.rb', line 52

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

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

Return the cities of given state and country



37
38
39
40
41
42
43
# File 'lib/country_state_select.rb', line 37

def self.collect_cities(state_id = '', country_id = '')
  if state_id.nil? || country_id.nil?
    []
  else
    CS.cities(state_id.to_sym, country_id.to_sym)
  end
end

.collect_states(country) ⇒ Object

Return the collected States for a given Country



32
33
34
# File 'lib/country_state_select.rb', line 32

def self.collect_states(country)
  CS.states(country).collect {|p| [ p[1], p[0] ] }.compact
end

.countries_collectionObject

Collect the Countries



8
9
10
# File 'lib/country_state_select.rb', line 8

def self.countries_collection
  CS.countries.except!(:COUNTRY_ISO_CODE).collect {|p| [ p[ 1], p[0] ] }.compact
end

.countries_except(*except) ⇒ Object

Pass array of unwanted countries to get back all except those in the array



13
14
15
# File 'lib/country_state_select.rb', line 13

def self.countries_except(*except)
  countries_collection.collect { |c| c unless except.include?(c[1]) }.compact
end

.merge_hash(options, collections) ⇒ Object

Create hash to use in the simple_form



58
59
60
61
62
# File 'lib/country_state_select.rb', line 58

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

.state_options(options) ⇒ Object

Return a hash for use in the simple_form



46
47
48
49
# File 'lib/country_state_select.rb', line 46

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

.states_collection(f, options) ⇒ Object

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



18
19
20
21
22
# File 'lib/country_state_select.rb', line 18

def self.states_collection(f, options)
  states = collect_states(f.object.send(options[:country]))
  return f.object.send(options[:state]) if states.size == 0
  states
end