Method: Binda::FieldableAssociationHelpers::FieldableSelectionHelpers#get_radio_choice

Defined in:
app/models/concerns/binda/fieldable_association_helpers/fieldable_selection_helpers.rb

#get_radio_choice(field_slug) ⇒ hash

Get the radio choice

If by mistake the Radio instance has many choices associated,

only the first one will be retrieved.

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (hash)

    A hash of containing the label and value of the selected choice. ‘{ label: ’the label’, value: ‘the value’}‘

Raises:

  • (ArgumentError)


11
12
13
14
15
16
17
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_selection_helpers.rb', line 11

def get_radio_choice(field_slug)
	field_setting = FieldSetting.find_by(slug:field_slug)
	obj = self.radios.find{ |t| t.field_setting_id == field_setting.id }
	raise ArgumentError, "There isn't any radio associated to the current slug (#{field_slug}) on instance (#{self.class.name} ##{self.id}).", caller if obj.nil?
	raise "There isn't any choice available for the current radio (#{field_slug}) on instance (#{self.class.name} ##{self.id})." unless obj.choices.any?
	return { label: obj.choices.first.label, value: obj.choices.first.value }
end