Class: TristateRadioInput
- Inherits:
-
Formtastic::Inputs::RadioInput
- Object
- Formtastic::Inputs::RadioInput
- TristateRadioInput
- Defined in:
- app/inputs/tristate_radio_input.rb
Overview
It may also be appropriate to put this file in ‘app/inputs`
Constant Summary collapse
- UNSET_KEY =
Now equals ‘:null`. Should equal one of `ActiveModel::Type::Boolean::NULL_VALUES`
Mind ActiveAdmin [status resolving logic](github.com/activeadmin/activeadmin/blob/master/lib/active_admin/views/components/status_tag.rb#L51): in status tag builder the value is lowercased before casting into Boolean, and the keyword for nil is ‘“unset”`. So if we have lowercase `“unset”`, translations from `ru.formtastic.unset` will be overriden by `ru.active_admin.status_tag.unset`.
FormtasticTristateRadio.config.unset_key
Instance Method Summary collapse
-
#choice_html_options(choice) ⇒ Hash
Adds ‘{ selected: true }` to the original options Hash if the choice value equals attribute value (to ultimately set for `checked=“checked”`).
-
#collection_for_boolean ⇒ Array<[String, (Boolean|String|Symbol)]>
An array of “choices”, each presented as an array with 2 items: HTML label text and HTML input value.
-
#label_text_for_unset ⇒ String
Checks translation passed as option, then checks in locale.
-
#selected?(choice) ⇒ Boolean
Answer to the question “Is the passed option selected?”.
Instance Method Details
#choice_html_options(choice) ⇒ Hash
Adds ‘{ selected: true }` to the original options Hash if the choice value equals attribute value (to ultimately set for `checked=“checked”`)
22 23 24 |
# File 'app/inputs/tristate_radio_input.rb', line 22 def (choice) super.merge({ checked: selected?(choice) }) end |
#collection_for_boolean ⇒ Array<[String, (Boolean|String|Symbol)]>
Returns an array of “choices”, each presented as an array with 2 items: HTML label text and HTML input value.
43 44 45 |
# File 'app/inputs/tristate_radio_input.rb', line 43 def collection_for_boolean super + [[label_text_for_unset, UNSET_KEY]] end |
#label_text_for_unset ⇒ String
Checks translation passed as option, then checks in locale
57 58 59 |
# File 'app/inputs/tristate_radio_input.rb', line 57 def label_text_for_unset .fetch(:null, Formtastic::I18n.t(UNSET_KEY)).presence or fail FormtasticTristateRadio::I18n::Error.new(I18n.locale, UNSET_KEY) end |
#selected?(choice) ⇒ Boolean
For this to work, ‘ActiveModel::Type::Boolean` must be patched to resolve `UNSET_KEY` as `nil`.
Returns answer to the question “Is the passed option selected?”.
73 74 75 |
# File 'app/inputs/tristate_radio_input.rb', line 73 def selected?(choice) ActiveModel::Type::Boolean.new.cast(choice_value(choice)) == object.public_send(method) end |