Module: DynamicFieldsets::FieldWithSingleAnswer::InstanceMethods

Defined in:
lib/dynamic_fieldsets/field_with_single_answer.rb

Instance Method Summary collapse

Instance Method Details

#defaultString

Returns Alias for field_defaults.first.

Returns:

  • (String)

    Alias for field_defaults.first



62
63
64
# File 'lib/dynamic_fieldsets/field_with_single_answer.rb', line 62

def default
  return self.field_defaults.first
end

#get_values_using_fsa_and_fsc(fsa, fsc) ⇒ String

Gets the first field record matching the parameters

Parameters:

Returns:

  • (String)

    The first field record



52
53
54
# File 'lib/dynamic_fieldsets/field_with_single_answer.rb', line 52

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

#show_partialString

Returns Default single answer partial filename.

Returns:

  • (String)

    Default single answer partial filename



57
58
59
# File 'lib/dynamic_fieldsets/field_with_single_answer.rb', line 57

def show_partial
  "/dynamic_fieldsets/show_partials/show_single_answer"
end

#show_partial_locals(args) ⇒ Object



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

def show_partial_locals(args)
  output = super(args)
  output[:value] = get_value_for_show(args[:value])
  return output
end

#update_field_records(fsa, fieldset_child, value) ⇒ Object

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

Saves or updates a single field record

Parameters:



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

def update_field_records(fsa, fieldset_child, value)
  # make sure the value is a string in case the input from the form is bad
  throw "Form value type mismatch error: The value from the form must be String for #{self.inspect}." unless value.is_a?(String)

  # retrieve record
  field_record = DynamicFieldsets::FieldRecord.where(:fieldset_associator_id => fsa.id, :fieldset_child_id => fieldset_child.id).first
  if field_record.nil? 
    # create record
    DynamicFieldsets::FieldRecord.create!(
      :fieldset_associator_id => fsa.id, 
      :fieldset_child_id => fieldset_child.id, 
      :value => value)
  else 
    # update record
    field_record.value = value 
    field_record.save
  end
end

#value_or_default_for_form(value) ⇒ String

Returns a list of values for the form

Parameters:

  • value (String)

    A value for the field already saved to the database

Returns:

  • (String)

    A field option saved in the db, or the default if value is blank



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

def value_or_default_for_form(value)
  if value.nil? || value[:value].nil?
    if field_defaults.length == 0
      return ""
    else
      return collect_default_values.first
    end
  else
    value[:value]
  end  
end