Module: DynamicFieldsets::FieldWithFieldOptions::InstanceMethods

Defined in:
lib/dynamic_fieldsets/field_with_field_options.rb

Instance Method Summary collapse

Instance Method Details

#at_least_one_field_optionObject

validation for field options



49
50
51
52
53
# File 'lib/dynamic_fieldsets/field_with_field_options.rb', line 49

def at_least_one_field_option
  if self.field_options.empty?
    self.errors.add(:field_options, "This field must have options")
  end
end

#collect_field_records_by_fsa_and_fsc(fsa, fsc) ⇒ Array

Collects the field records for the field so they can be used on the front end These are only the currently saved values in the database, don’t worry about defaults here

Converts the ids into field option names for :name part of the return hash

Returns:

  • (Array)

    An array of field record values



25
26
27
28
29
30
31
32
33
# File 'lib/dynamic_fieldsets/field_with_field_options.rb', line 25

def collect_field_records_by_fsa_and_fsc(fsa, fsc)
  records = DynamicFieldsets::FieldRecord.where(:fieldset_associator_id => fsa.id, :fieldset_child_id => fsc.id)
  return records.collect do |r| 
    { 
      :value => r.value, 
      :name => DynamicFieldsets::FieldOption.find(r.value.to_i).name
    }
  end
end

#get_value_for_show(value) ⇒ Object

given a value hash for a field, return the part that needs to be shown on the show page



36
37
38
39
40
41
42
# File 'lib/dynamic_fieldsets/field_with_field_options.rb', line 36

def get_value_for_show(value)
  if value.nil?
    return nil
  else
    value[:name]
  end
end

#optionsFieldOptions

Returns all field options that are enabled

Returns:

  • (FieldOptions)

    Returns all field options that are enabled



56
57
58
# File 'lib/dynamic_fieldsets/field_with_field_options.rb', line 56

def options
  return self.field_options.reject{ |option| !option.enabled }
end

#uses_field_options?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/dynamic_fieldsets/field_with_field_options.rb', line 44

def uses_field_options?
  true
end