Method: Binda::FieldableAssociationHelpers::FieldableSelectionHelpers#get_selection_choices

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

#get_selection_choices(field_slug) ⇒ array

Get the select choices

Parameters:

  • field_slug (string)

    The slug of the field setting

Returns:

  • (array)

    An array of hashes of containing label and value of the selected choices. ‘{ label: ’the label’, value: ‘the value’}‘

Raises:

  • (ArgumentError)


35
36
37
38
39
40
41
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_selection_helpers.rb', line 35

def get_selection_choices(field_slug)
	field_setting = FieldSetting.find_by(slug:field_slug)
	obj = self.selections.find{ |t| t.field_setting_id == field_setting.id }
	raise ArgumentError, "There isn't any selection 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 selection (#{field_slug}) on instance (#{self.class.name} ##{self.id})." unless field_setting.choices.any?
	return obj.choices.map{|choice| { label: choice.label, value: choice.value }}
end