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
-
#get_checkbox_choices(field_slug) ⇒ array
Get the checkbox choice.
-
#get_radio_choice(field_slug) ⇒ hash
Get the radio choice.
-
#get_selection_choice(field_slug) ⇒ hash
Get the select choice.
-
#get_selection_choices(field_slug) ⇒ array
Get the select choices.
Instance Method Details
#get_checkbox_choices(field_slug) ⇒ array
Get the checkbox choice
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.
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
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
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 |