Class: CountryStateSelect::Configuration

Inherits:
Object
  • Object
show all
Defined in:
lib/country_state_select/configuration.rb

Overview

Global, per-app configuration. Set it from an initializer:

CountryStateSelect.configure do |config|
config.priority_countries = %w[US IN GB]
config.flags = true
config.cache_duration = 1.week
end

Every option here is a default — the equivalent per-call keyword (e.g. countries_collection(priority: ...)) always wins.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeConfiguration

Returns a new instance of Configuration.



46
47
48
49
50
51
52
53
54
55
56
# File 'lib/country_state_select/configuration.rb', line 46

def initialize
  @data_source = :city_state
  @priority_countries = []
  @only_countries = nil
  @except_countries = nil
  @flags = false
  @dial_codes = false
  @localize_names = false
  @cache_duration = 86_400 # 1 day, in seconds
  @draw_routes = true
end

Instance Attribute Details

#cache_durationObject

How long the JSON lookup endpoints are HTTP-cached (they serve static data). Set to nil/false to disable cache headers.



37
38
39
# File 'lib/country_state_select/configuration.rb', line 37

def cache_duration
  @cache_duration
end

#data_sourceObject

Data source answering countries/states/cities lookups. Accepts an object implementing DataSources::Base or a symbol (:city_state).



17
18
19
# File 'lib/country_state_select/configuration.rb', line 17

def data_source
  @data_source
end

#dial_codesObject

Append the international dial code to country labels ("India (+91)").



29
30
31
# File 'lib/country_state_select/configuration.rb', line 29

def dial_codes
  @dial_codes
end

#draw_routesObject

Draw the legacy top-level /find_states + /find_cities routes in the host app. Disable if you mount the engine at a custom path:

config.draw_routes = false
# config/routes.rb: mount CountryStateSelect::Rails::Engine => '/csc'


44
45
46
# File 'lib/country_state_select/configuration.rb', line 44

def draw_routes
  @draw_routes
end

#except_countriesObject

Whitelist / blacklist of ISO codes applied to every country lookup.



23
24
25
# File 'lib/country_state_select/configuration.rb', line 23

def except_countries
  @except_countries
end

#flagsObject

Prepend the emoji flag to country labels ("🇮🇳 India").



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

def flags
  @flags
end

#localize_namesObject

Translate country names through I18n / the countries gem when translations are available. Labels fall back to the English name.



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

def localize_names
  @localize_names
end

#only_countriesObject

Whitelist / blacklist of ISO codes applied to every country lookup.



23
24
25
# File 'lib/country_state_select/configuration.rb', line 23

def only_countries
  @only_countries
end

#priority_countriesObject

ISO codes listed first in the country dropdown (issue #66).



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

def priority_countries
  @priority_countries
end

Instance Method Details

#data_source_instanceObject



58
59
60
61
62
63
64
65
# File 'lib/country_state_select/configuration.rb', line 58

def data_source_instance
  @data_source_instance ||=
    case data_source
    when :city_state, nil then DataSources::CityState.new
    when Symbol then raise ArgumentError, "Unknown data source: #{data_source.inspect}"
    else data_source
    end
end