Module: Binda::FieldableAssociationHelpers::FieldableSelectionHelpers

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

Instance Method Summary collapse

Instance Method Details

#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

#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

#get_selection_choice(field_slug) ⇒ hash

Get the select choice

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)


23
24
25
26
27
28
29
# File 'app/models/concerns/binda/fieldable_association_helpers/fieldable_selection_helpers.rb', line 23

def get_selection_choice(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 { label: obj.choices.first.label, value: obj.choices.first.value }
end

#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