Class: DynamicFieldsets::FieldsetAssociator

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
app/models/dynamic_fieldsets/fieldset_associator.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.find_by_fieldset_model_parameters(args) ⇒ Object

Scope to find a fieldset associator based on information from the fieldset model

Arguments fieldset: The nkey of the fieldset fieldset_model_id: The id of the fieldset model fieldset_model_type: The class name of the fieldset model fieldset_model_name: The named fieldset in the model

Throws an error if the fieldset does not exist to help with debugging



32
33
34
35
36
37
38
39
40
41
# File 'app/models/dynamic_fieldsets/fieldset_associator.rb', line 32

def self.find_by_fieldset_model_parameters(args)
  fieldset = Fieldset.where(:nkey => args[:fieldset_nkey].to_s).first
  throw "Fieldset not found in FieldsetAssociator.find_by_fieldset_model_parameters" if fieldset.nil?

  where(
    :fieldset_id => fieldset.id, 
    :fieldset_model_id => args[:fieldset_model_id], 
    :fieldset_model_type => args[:fieldset_model_type], 
    :fieldset_model_name => args[:fieldset_model_name])
end

Instance Method Details

#dependency_child_hashHash

Converts the list of fields with dependencies generated by the look_for_dependent_ons method into a hash The hash contains the actual dependencies for the list of fields that have them

Returns:

  • (Hash)

    dependent fields for fields in the fieldset



79
80
81
82
83
84
85
86
87
88
89
90
91
92
# File 'app/models/dynamic_fieldsets/fieldset_associator.rb', line 79

def dependency_child_hash
  fieldset_child_collection = look_for_dependent_ons(self.fieldset)

  output = {}
  fieldset_child_collection.each do |fieldset_child|
    output[fieldset_child.id] = {}
    fieldset_child.dependencies.each do |dependency|
      dependency_group = dependency.dependency_clause.dependency_group
      output[fieldset_child.id][dependency_group.id] = dependency_group.to_hash
      fieldset = dependency_group.fieldset_child.fieldset
    end
  end
  return output
end

#field_records_by_field_name(name) ⇒ Array

Record whose field matches the name and associator matches the current associator

This will be used to get answers to questions with hard coded names

Returns:

  • (Array)

    the matching records



63
64
65
66
67
68
69
70
71
72
# File 'app/models/dynamic_fieldsets/fieldset_associator.rb', line 63

def field_records_by_field_name(name)
  records = self.field_records.select { |record| record.fieldset_child.child.name == name }
  return records
  
  # this version uses less queries but feels less elegant
  # field = DynamicFieldsets::Field.find_by_name(name)
  # child = DynamicFieldsets::FieldsetChild.where(:child => field, :fieldset => self.fieldset).first
  # records = DynamicFieldsets::FieldRecord.where(:child => child, :associator => self)
  # return records
end

#field_valuesHash

Returns a hash of field record values

This version returns a tree structure for nested fieldsets Could lead to all sorts of problems

Returns:

  • (Hash)

    A hash of field record values associated with field ids



49
50
51
# File 'app/models/dynamic_fieldsets/fieldset_associator.rb', line 49

def field_values
  fieldset.get_values_using_fsa(self)
end

#find_parent_fsa(fieldset) ⇒ Array <FieldsetAssociator>

Gets finds ‘ancestor’ fieldset with fsa in case of nested fieldset when generating dependency hash we need an fsa in the dependencies hash (used for cross-fsa dependencies) AU 08-16-13

Parameters:

Returns:



100
101
102
103
104
105
106
# File 'app/models/dynamic_fieldsets/fieldset_associator.rb', line 100

def find_parent_fsa(fieldset)
  if fieldset.fieldset_associators.present?
    return fieldset.fieldset_associators
  else
    return find_parent_fsa(fieldset.parent)
  end
end

#look_for_dependent_ons(parent_fieldset) ⇒ Array

Gets a list of fields in the given Fieldset That Have Dependencies I am not exactly sure why this method is necessary (JH 3-8-2012) It seems like we could just make the dependency_child_hash method run recursively

Parameters:

Returns:

  • (Array)

    The list of fields with dependencies



114
115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/models/dynamic_fieldsets/fieldset_associator.rb', line 114

def look_for_dependent_ons(parent_fieldset)
  output = []
  parent_fieldset.fieldset_children.each do |fieldset_child|
    if fieldset_child.child_type == "DynamicFieldsets::Field"
      if fieldset_child.dependencies.present?
        output.push fieldset_child
      end
    else # if child type is a fieldset, then recurse
      output += look_for_dependent_ons(fieldset_child.child)
    end
  end
  return output
end

#unique_fieldset_model_name_per_polymorphic_fieldset_modelObject



12
13
14
15
16
17
18
# File 'app/models/dynamic_fieldsets/fieldset_associator.rb', line 12

def unique_fieldset_model_name_per_polymorphic_fieldset_model
  FieldsetAssociator.where(:fieldset_model_id => self.fieldset_model_id, :fieldset_model_type => self.fieldset_model_id, :fieldset_model_name => self.fieldset_model_name).each do |fsa|
    if fsa.id != self.id
      self.errors.add(:fieldset_model_name, "A duplicate Field Model, Field Model Name pair has been found.")
    end
  end
end

#update_fieldset_records_with_form_information(fsa_params) ⇒ Object

given the params, passes along to the fieldset



54
55
56
# File 'app/models/dynamic_fieldsets/fieldset_associator.rb', line 54

def update_fieldset_records_with_form_information(fsa_params)
  fieldset.update_field_records_with_form_information(self, fsa_params)
end