Module: CountryStateSelect::FormBuilder

Defined in:
lib/country_state_select/form_builder.rb

Overview

Native form_with/form_for support — f.state_select renders a plain HTML when the current country has no states, matching simple_form's state_options behavior) without requiring simple_form. Both helpers stamp data-controller/data-*-target attributes so the bundled Stimulus controller (app/javascript/ country_state_select/controller.js) wires up automatically — wrap the fields in a data-controller="country-state-select" container and no further JS is needed.

Constant Summary collapse

FILTER_KEYS =
%i[only except priority].freeze

Instance Method Summary collapse

Instance Method Details

#city_select(method, state_method, country_method, options = {}, html_options = {}) ⇒ Object



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

def city_select(method, state_method, country_method, options = {}, html_options = {})
  cities = CountryStateSelect.collect_cities(object.public_send(state_method), object.public_send(country_method))
  merged_html = html_options.reverse_merge(data: { 'country-state-select-target': 'city' })

  if cities.empty?
    text_field(method, options.merge(merged_html))
  else
    select(method, cities.zip(cities), options, merged_html)
  end
end

#country_select(method, options = {}, html_options = {}) ⇒ Object



15
16
17
18
19
20
# File 'lib/country_state_select/form_builder.rb', line 15

def country_select(method, options = {}, html_options = {})
  collection = CountryStateSelect.countries_collection(options.slice(*FILTER_KEYS))
  merged_html = html_options.reverse_merge(data: { 'country-state-select-target': 'country' })

  select(method, collection, options.except(*FILTER_KEYS), merged_html)
end

#state_select(method, country_method, options = {}, html_options = {}) ⇒ Object



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

def state_select(method, country_method, options = {}, html_options = {})
  country_value = object.public_send(country_method)
  states = CountryStateSelect.collect_states(country_value, **options.slice(*FILTER_KEYS))
  merged_html = html_options.reverse_merge(data: { 'country-state-select-target': 'state' })

  if states.empty?
    # FormBuilder#text_field (generated for the `field_helpers` list)
    # only takes (method, options) — html attributes and options share
    # one hash, unlike `select`'s separate options/html_options.
    text_field(method, options.except(*FILTER_KEYS).merge(merged_html))
  else
    select(method, states, options.except(*FILTER_KEYS), merged_html)
  end
end