Class: DynamicFieldsets::Fieldset

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

Overview

Stores a collection of fields and other fieldsets

Author:

  • Jeremiah Hemphill, Ethan Pemble

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.parent_fieldset_listArray

Returns An array of name, id pairs to be used in select tags.

Returns:

  • (Array)

    An array of name, id pairs to be used in select tags



40
41
42
# File 'app/models/dynamic_fieldsets/fieldset.rb', line 40

def self.parent_fieldset_list
  all.collect { |f| [f.name, f.id] }
end

.rootsArray

Returns Scope: parent-less fieldsets.

Returns:

  • (Array)

    Scope: parent-less fieldsets



27
28
29
30
31
32
# File 'app/models/dynamic_fieldsets/fieldset.rb', line 27

def self.roots
  # the old method used a scope and the old table definition
  # scope :roots, :conditions => ["parent_fieldset_id IS NULL"]
  # the new method doesn't use a scope because I am bad at them
  all.select { |fs| fs.parent.nil? }
end

Instance Method Details

#childrenArray

The collected descendents of a fieldset. This group is sorted by order number on the fieldsetchild model

IMPORTANT NOTE: At first glance, this looks like it should return a collection of fieldset children. It does not. This is a relic from the original design, and there is code working around the issue in several different places.

Returns:

  • (Array)

    Ordered collection of descendent fields and fieldsets.



51
52
53
54
55
56
57
58
59
60
61
62
# File 'app/models/dynamic_fieldsets/fieldset.rb', line 51

def children
  collected_children = []
  self.fieldset_children.sort_by(&:order_num).each do |fs_child|
    child = fs_child.child_type.constantize.find_by_id fs_child.child_id
    if child.respond_to? :enabled? and child.enabled?
      collected_children.push child
    elsif !child.respond_to? :enabled?
      collected_children.push child
    end
  end
  return collected_children
end

#get_values_using_fsa(fsa) ⇒ Hash

returns field record values for every field in the fsa

note that this adds a hierarchy to the values not sure if this will require a rewrite somewhere else

Parameters:

Returns:

  • (Hash)

    A hash of field record values using the fieldset child id as they key



75
76
77
78
79
80
81
# File 'app/models/dynamic_fieldsets/fieldset.rb', line 75

def get_values_using_fsa(fsa)
  output = {}
  fieldset_children.each do |child|
    output[child.id] = child.get_value_using_fsa(fsa)
  end      
  return output
end

#has_children?Boolean

Returns:

  • (Boolean)


64
65
66
# File 'app/models/dynamic_fieldsets/fieldset.rb', line 64

def has_children?
  return !self.fieldset_children.empty?
end

#root?Boolean

Returns True if fieldset has no parent.

Returns:

  • (Boolean)

    True if fieldset has no parent



35
36
37
# File 'app/models/dynamic_fieldsets/fieldset.rb', line 35

def root?
  return parent.nil?
end

#update_field_records_with_form_information(fsa, form_values) ⇒ Object

Updates field options based on information from the form

The form values should only be values for the current fsa

Parameters:

  • form_values (Hash)

    Information from the form



88
89
90
91
92
93
94
95
96
97
# File 'app/models/dynamic_fieldsets/fieldset.rb', line 88

def update_field_records_with_form_information(fsa, form_values)
  form_values.each_pair do |fieldset_child_key, value|
    if fieldset_child_key.start_with?(DynamicFieldsets.config.form_field_prefix)
      fieldset_child_id = fieldset_child_key.gsub(/^#{DynamicFieldsets.config.form_field_prefix}/, "")
      fieldset_child = fieldset_children.select { |child| child.id == fieldset_child_id.to_i }.first
      # this could potentially hit a fieldset and cause problems
      fieldset_child.child.update_field_records(fsa, fieldset_child, value) if fieldset_child.present?
    end
  end
end