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
Constant Summary collapse
- FILTER_KEYS =
%i[only except priority].freeze
Instance Method Summary collapse
- #city_select(method, state_method, country_method, options = {}, html_options = {}) ⇒ Object
- #country_select(method, options = {}, html_options = {}) ⇒ Object
- #state_select(method, country_method, options = {}, html_options = {}) ⇒ Object
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, = {}, = {}) cities = CountryStateSelect.collect_cities(object.public_send(state_method), object.public_send(country_method)) merged_html = .reverse_merge(data: { 'country-state-select-target': 'city' }) if cities.empty? text_field(method, .merge(merged_html)) else select(method, cities.zip(cities), , 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, = {}, = {}) collection = CountryStateSelect.countries_collection(.slice(*FILTER_KEYS)) merged_html = .reverse_merge(data: { 'country-state-select-target': 'country' }) select(method, collection, .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, = {}, = {}) country_value = object.public_send(country_method) states = CountryStateSelect.collect_states(country_value, **.slice(*FILTER_KEYS)) merged_html = .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, .except(*FILTER_KEYS).merge(merged_html)) else select(method, states, .except(*FILTER_KEYS), merged_html) end end |