Class: TristateRadioInput

Inherits:
Formtastic::Inputs::RadioInput
  • Object
show all
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

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”`)

Returns:

  • (Hash)

    HTML options for the ‘<input type=“radio” />` tag

See Also:



22
23
24
# File 'app/inputs/tristate_radio_input.rb', line 22

def choice_html_options(choice)
  super.merge({ checked: selected?(choice) })
end

#collection_for_booleanArray<[String, (Boolean|String|Symbol)]>

Returns an array of “choices”, each presented as an array with 2 items: HTML label text and HTML input value.

Examples:

Original method

def collection_for_boolean
  true_text  = options[:true]  || Formtastic::I18n.t(:yes)
  false_text = options[:false] || Formtastic::I18n.t(:no)
  [ [true_text, true], [false_text, false] ]
end

collection_for_boolean #=> [["Да", true], ["Нет", false]]

This patched method

collection_for_boolean #=> [["Да", true], ["Нет", false], ["Неизвестно", :null]]

Returns:

  • (Array<[String, (Boolean|String|Symbol)]>)

    an array of “choices”, each presented as an array with 2 items: HTML label text and HTML input value

See Also:



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_unsetString

Checks translation passed as option, then checks in locale

Examples:

label_text_for_unset #=> "Неизвестно"

Returns:

  • (String)

    Label of the radio that stands for the unknown choice

Raises:



57
58
59
# File 'app/inputs/tristate_radio_input.rb', line 57

def label_text_for_unset
  options.fetch(:null, Formtastic::I18n.t(UNSET_KEY)).presence or fail FormtasticTristateRadio::I18n::Error.new(I18n.locale, UNSET_KEY)
end

#selected?(choice) ⇒ Boolean

Note:

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?”.

Examples:

For each item of ‘collection` it runs:

selected?(["Да", true]) #=> false
selected?(["Нет", false]) #=> false
selected?(["Неизвестно", :null]) #=> true

Parameters:

  • choice (Array<[String, (Boolean|String|Symbol)]>)

Returns:

  • (Boolean)

    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