Class: DynamicFieldsets::FieldsetChild
- Inherits:
-
ActiveRecord::Base
- Object
- ActiveRecord::Base
- DynamicFieldsets::FieldsetChild
- Defined in:
- app/models/dynamic_fieldsets/fieldset_child.rb
Overview
FieldsetChild
Constant Summary collapse
- FIELDSET_CHILD_LIST =
Constants
["DynamicFieldsets::Field", "DynamicFieldsets::Fieldset"]
Instance Method Summary collapse
-
#assign_order ⇒ Object
This method is called when FieldsetChildren is instantiated.
-
#cannot_be_own_parent ⇒ Object
Sends a validation error if there is an attempt to save a fieldset as its own parent.
-
#children ⇒ ActiveRecord::Relation
Collection of FieldsetChildren that are direct descendents; ascending order.
-
#fieldset_child_list ⇒ Object
Returns a list of all the values in the FIELDSET_CHILD_LIST constant.
-
#get_value_using_fsa(fsa) ⇒ Hash
returns all the field record values for a single fieldset child.
-
#has_loop?(parent_array) ⇒ Boolean
Recursively calls itself to span entire hierarchy of a fieldset child family to see if any attempts to have a parental loop exist.
-
#last_order_num ⇒ Integer
The order number of the last sibling.
-
#no_duplicate_fields_in_fieldset_children ⇒ Object
Sends a validation error if there are any duplicate pairings of fieldsets and fieldset children.
-
#no_parental_loop ⇒ Object
Recursively looks to see if there is an attempt to save a fieldset as a descendent of itself.
-
#root_fieldset(fs = self.fieldset) ⇒ Fieldset
Returns the root fieldset of the child Loops up the parent fieldset field until the parent is nil, then returns the child.
-
#siblings ⇒ ActiveRecord::Relation
Collection of FieldsetChildren that share the same parent; ascending order.
- #to_hash ⇒ Object
Instance Method Details
#assign_order ⇒ Object
This method is called when FieldsetChildren is instantiated. It ensures that the Child has a valid order number.
45 46 47 |
# File 'app/models/dynamic_fieldsets/fieldset_child.rb', line 45 def assign_order self.order_num = self.last_order_num + 1 if self.order_num.nil? end |
#cannot_be_own_parent ⇒ Object
Sends a validation error if there is an attempt to save a fieldset as its own parent.
66 67 68 69 70 71 72 |
# File 'app/models/dynamic_fieldsets/fieldset_child.rb', line 66 def cannot_be_own_parent if self.child_type == "DynamicFieldsets::Fieldset" if self.fieldset_id == self.child_id self.errors.add(:child_id, "A fieldset cannot have itself as a parent.") end end end |
#children ⇒ ActiveRecord::Relation
Returns Collection of FieldsetChildren that are direct descendents; ascending order.
105 106 107 |
# File 'app/models/dynamic_fieldsets/fieldset_child.rb', line 105 def children FieldsetChild.where( fieldset_id: self.child_id ).ordered end |
#fieldset_child_list ⇒ Object
Returns a list of all the values in the FIELDSET_CHILD_LIST constant
113 114 115 |
# File 'app/models/dynamic_fieldsets/fieldset_child.rb', line 113 def fieldset_child_list return FIELDSET_CHILD_LIST end |
#get_value_using_fsa(fsa) ⇒ Hash
returns all the field record values for a single fieldset child
121 122 123 124 125 126 127 128 129 130 131 |
# File 'app/models/dynamic_fieldsets/fieldset_child.rb', line 121 def get_value_using_fsa(fsa) output = {} if child.class == DynamicFieldsets::Fieldset return child.get_values_using_fsa(fsa) elsif child.class.superclass == DynamicFieldsets::Field || child.class.superclass.to_s == DynamicFieldsets::Field.to_s return child.get_values_using_fsa_and_fsc(fsa, self) else # I am not sure if we need to use child.superclass equals Field due to the sti throw "there is a problem with fieldset_child.get_value_using_fsa possibly due to the single table inheritance." end end |
#has_loop?(parent_array) ⇒ Boolean
Recursively calls itself to span entire hierarchy of a fieldset child family to see if any attempts to have a parental loop exist
88 89 90 91 92 93 94 |
# File 'app/models/dynamic_fieldsets/fieldset_child.rb', line 88 def has_loop?(parent_array) return true if parent_array.include? self.child_id parent_array.push self.child_id return true if FieldsetChild.where(:fieldset_id => self.child_id).find { |f| f.has_loop? parent_array } parent_array.pop return false end |
#last_order_num ⇒ Integer
Returns The order number of the last sibling.
134 135 136 137 |
# File 'app/models/dynamic_fieldsets/fieldset_child.rb', line 134 def last_order_num return 0 if siblings.empty? return self.siblings.last[:order_num] end |
#no_duplicate_fields_in_fieldset_children ⇒ Object
Sends a validation error if there are any duplicate pairings of fieldsets and fieldset children.
53 54 55 56 57 58 59 60 |
# File 'app/models/dynamic_fieldsets/fieldset_child.rb', line 53 def no_duplicate_fields_in_fieldset_children if self.fieldset && self.child duplicate_children = FieldsetChild.where(:fieldset_id => self.fieldset.id, :child_id => self.child_id, :child_type => self.child_type).select { |fieldset_child| fieldset_child.id != self.id } if duplicate_children.length > 0 self.errors.add(:child_id, "There is already a copy of this child in the fieldset.") end end end |
#no_parental_loop ⇒ Object
Recursively looks to see if there is an attempt to save a fieldset as a descendent of itself
78 79 80 81 82 |
# File 'app/models/dynamic_fieldsets/fieldset_child.rb', line 78 def no_parental_loop if (self.child_type == "DynamicFieldsets::Fieldset") && (self.has_loop?([self.fieldset_id])) self.errors.add(:child_id, "There is a fieldset that contains itself as a parent.") end end |
#root_fieldset(fs = self.fieldset) ⇒ Fieldset
Returns the root fieldset of the child Loops up the parent fieldset field until the parent is nil, then returns the child
Important: This method is dependent on fieldsets not being reusable
145 146 147 148 149 150 151 152 153 154 155 |
# File 'app/models/dynamic_fieldsets/fieldset_child.rb', line 145 def root_fieldset(fs = self.fieldset) # whether the parent is a child parent_as_a_child = FieldsetChild.where(:child_id => fs.id, :child_type => "DynamicFieldsets::Fieldset") # my parent is nobody's child, it is the root if parent_as_a_child.count == 0 return fs else # if my parent is someone else's child, then # look at her parent return root_fieldset(parent_as_a_child.first.fieldset) end end |
#siblings ⇒ ActiveRecord::Relation
Returns Collection of FieldsetChildren that share the same parent; ascending order.
158 159 160 161 162 |
# File 'app/models/dynamic_fieldsets/fieldset_child.rb', line 158 def siblings sib = FieldsetChild.where( fieldset_id: self.fieldset_id ).ordered sib.delete_if{ |child| child.id == self.id } sib end |
#to_hash ⇒ Object
164 165 166 |
# File 'app/models/dynamic_fieldsets/fieldset_child.rb', line 164 def to_hash return { "id" => self.id, "fieldset_id" => self.fieldset_id, "child_id" => self.child_id, "child_type" => self.child_type } end |