Module: DynamicFieldsets::FieldWithMultipleAnswers::InstanceMethods

Defined in:
lib/dynamic_fieldsets/field_with_multiple_answers.rb

Instance Method Summary collapse

Instance Method Details

#defaultsArray



83
84
85
86
# File 'lib/dynamic_fieldsets/field_with_multiple_answers.rb', line 83

def defaults
  return self.field_defaults if options?
  return nil
end

#get_values_using_fsa_and_fsc(fsa, fsc) ⇒ Array

Gets the first field record matching the parameters



55
56
57
# File 'lib/dynamic_fieldsets/field_with_multiple_answers.rb', line 55

def get_values_using_fsa_and_fsc(fsa, fsc)
  collect_field_records_by_fsa_and_fsc(fsa, fsc)
end

#show_partialString



60
61
62
# File 'lib/dynamic_fieldsets/field_with_multiple_answers.rb', line 60

def show_partial
  "/dynamic_fieldsets/show_partials/show_multiple_answers"
end

#show_partial_locals(args) ⇒ Object



15
16
17
18
19
# File 'lib/dynamic_fieldsets/field_with_multiple_answers.rb', line 15

def show_partial_locals(args)
  output = super(args)
  output[:values] = args[:values].collect { |value| get_value_for_show(value) }
  return output
end

#update_field_records(fsa, fieldset_child, values) ⇒ Object

Updates the field records for the field based on the given values

Manages multiple records



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/dynamic_fieldsets/field_with_multiple_answers.rb', line 28

def update_field_records(fsa, fieldset_child, values)
  # make sure values is an array in case the input from the form is bad
  throw "Form value type mismatch error: The value from the form must be Array for #{self.inspect}." unless values.is_a?(Array)
  
  # create new records if the values are not in the db
  values.each do |value|
    if fieldset_child.field_records.select{ |record| record.value == value }.empty?
      DynamicFieldsets::FieldRecord.create!( :fieldset_associator_id => fsa.id,
        :fieldset_child_id => fieldset_child.id,
        :value => value)
    end
  end

  # remove records in the db that are not in the input values
  # note that if a record is updated, it is treated as a creation and deletion instead of a single update
  fieldset_child.field_records.each do |record|
    if !values.include?(record.value)
      record.destroy
    end
  end
end

#values_or_defaults_for_form(values) ⇒ Array

Returns a list of values for the form

Only returns the :value part of the input



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/dynamic_fieldsets/field_with_multiple_answers.rb', line 70

def values_or_defaults_for_form(values)
  if values.nil? || values.empty?
    if field_defaults.length == 0
      return []
    else
      return collect_default_values
    end
  else
    return values.collect { |v| v[:value] }
  end  
end