Module: ClosureTree::HierarchyMaintenance::ClassMethods
- Defined in:
- lib/closure_tree/hierarchy_maintenance.rb
Instance Method Summary collapse
- #cleanup! ⇒ Object
-
#rebuild! ⇒ Object
Rebuilds the hierarchy table based on the parent_id column in the database.
Instance Method Details
#cleanup! ⇒ Object
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/closure_tree/hierarchy_maintenance.rb', line 121 def cleanup! hierarchy_table = hierarchy_class.arel_table [:descendant_id, :ancestor_id].each do |foreign_key| alias_name = foreign_key.to_s.split('_').first + "s" alias_table = Arel::Table.new(table_name).alias(alias_name) arel_join = hierarchy_table.join(alias_table, Arel::Nodes::OuterJoin) .on(alias_table[primary_key].eq(hierarchy_table[foreign_key])) .join_sources lonely_childs = hierarchy_class.joins(arel_join).where(alias_table[primary_key].eq(nil)) ids = lonely_childs.pluck(foreign_key) hierarchy_class.where(hierarchy_table[foreign_key].in(ids)).delete_all end end |
#rebuild! ⇒ Object
Rebuilds the hierarchy table based on the parent_id column in the database. Note that the hierarchy table will be truncated.
113 114 115 116 117 118 119 |
# File 'lib/closure_tree/hierarchy_maintenance.rb', line 113 def rebuild! _ct.with_advisory_lock do cleanup! roots.find_each { |n| n.send(:rebuild!) } # roots just uses the parent_id column, so this is safe. end nil end |