Class: DynamicFieldsets::DependencyGroup

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

Constant Summary collapse

Action_list =

List of allowable actions for the group Success and failure options are returned by the get_action method

{ "show" => { :success => "show", :failure => "hide" }, "enable" => { :success => "enable", :failure => "disable" } }

Instance Method Summary collapse

Instance Method Details

#action_in_action_listObject

adds an error if the action isn’t in the action list



18
19
20
21
22
# File 'app/models/dynamic_fieldsets/dependency_group.rb', line 18

def action_in_action_list
  if !action_list.keys.include?(self.action)
    self.errors.add(:action, "The action must be set to one of the provided values.")
  end
end

#action_listHash

Returns The action list hash.

Returns:

  • (Hash)

    The action list hash



25
26
27
# File 'app/models/dynamic_fieldsets/dependency_group.rb', line 25

def action_list
  return Action_list
end

#dependency_group_fieldset_childrenObject

Parses the dependnecy_group’s fieldset_child and ones inherited through dependency clause and creates a hash where the key is the dependency_group’s fieldset_child and the value is an array of all fieldset children dependent on this group. This is returned as a JSON object



50
51
52
# File 'app/models/dynamic_fieldsets/dependency_group.rb', line 50

def dependency_group_fieldset_children
  return { self.fieldset_child_id => self.dependent_fieldset_children }.to_json
end

#dependent_fieldset_childrenObject

Returns all fieldset children included in this dependency group Not sure if it will be useful, it would probably be called in evaluate I think it will be faster to just pass the input values array all the way down when evaluate is called.



35
36
37
38
39
40
41
42
43
# File 'app/models/dynamic_fieldsets/dependency_group.rb', line 35

def dependent_fieldset_children
  children = []
  self.dependency_clauses.each do |clause|
    clause.dependencies.each do |dep|
      children.push(dep.fieldset_child_id)
    end
  end
  return children
end

#evaluate(values) ⇒ Boolean

Evaluates the clauses by ANDing them together Short circuit evaluation returns false as soon as possible

Parameters:

  • values (Hash)

    A hash of fieldset_child_id:value pairs to test against

Returns:

  • (Boolean)

    True if all of the clauses are true



71
72
73
74
75
76
77
78
# File 'app/models/dynamic_fieldsets/dependency_group.rb', line 71

def evaluate(values)
  self.dependency_clauses.each do |clause|
    if !clause.evaluate(values)
      return false
    end
  end
  return true
end

#get_action(input_values) ⇒ String

Returns the success or failure action depending on what evaluate returns

Parameters:

  • input_values (Hash)

    A hash of fieldset_child_id:value pairs to test against

Returns:

  • (String)

    The success or failure action



58
59
60
61
62
63
64
# File 'app/models/dynamic_fieldsets/dependency_group.rb', line 58

def get_action(input_values)
  if evaluate(input_values)
    return Action_list[self.action][:success]
  else
    return Action_list[self.action][:failure]
  end
end

#to_hashHash

Creates a nested hash that has information on the dependency_group as well as all dependency_clauses and nested dependencies within it. To be used when capturing dependencies for interacting with jQuery.

Parameters:

  • (None)

Returns:

  • (Hash)

    A nested hash of the dependency group and related dependency_clauses and dependencies



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/dynamic_fieldsets/dependency_group.rb', line 86

def to_hash
  dependency_group_hash =
  {
      "action" => self.action, 
      "fieldset_child_id" => self.fieldset_child_id, 
      "field_id" => self.fieldset_child.child_id,
      "clause" => {}
  }
  for dependency_clause in self.dependency_clauses
    dependency_group_hash["clause"][dependency_clause.id] = {}
    for dependency in dependency_clause.dependencies
      dependency_group_hash["clause"][dependency_clause.id][dependency.id] = 
      {
        "fieldset_child_id" => dependency.fieldset_child.id,
        "relationship" => dependency.relationship,
        "value" => dependency.value
      }
    end
  end
  dependency_group_hash
end