Method: Binda::FieldableAssociationHelpers::FieldableSelectionHelpers#get_checkbox_choices

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

#get_checkbox_choices(field_slug) ⇒ array

Get the checkbox choice

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (array)

    An array of labels and values of the selected choices. ‘[{ label: ’1st label’, value: ‘1st-value’}, { label: ‘2nd label’, value: ‘2nd-value’}]‘

Raises:

  • (ArgumentError)


47
48
49
50
51
52
53
54
55
56
57
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_selection_helpers.rb', line 47

def get_checkbox_choices(field_slug)
	field_setting = FieldSetting.find_by(slug:field_slug)
	obj = self.checkboxes.find{ |t| t.field_setting_id == field_setting.id }
	raise ArgumentError, "There isn't any checkbox 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 checkbox (#{field_slug}) on instance (#{self.class.name} ##{self.id})." unless field_setting.choices.any?
	obj_array = []
	obj.choices.order('label').each do |o|
		obj_array << { label: o.label, value: o.value }
	end
	return obj_array
end