Module: ClosureTree::NumericDeterministicOrdering
- Extended by:
- ActiveSupport::Concern
- Defined in:
- lib/closure_tree/numeric_deterministic_ordering.rb
Instance Method Summary collapse
- #_ct_reorder_children(minimum_sort_order_value = nil) ⇒ Object
- #_ct_reorder_prior_siblings_if_parent_changed ⇒ Object
- #_ct_reorder_siblings(minimum_sort_order_value = nil) ⇒ Object
- #add_sibling(sibling, add_after = true) ⇒ Object
- #append_child(child_node) ⇒ Object
- #append_sibling(sibling_node) ⇒ Object
- #prepend_child(child_node) ⇒ Object
- #prepend_sibling(sibling_node) ⇒ Object
- #self_and_descendants_preordered ⇒ Object
Instance Method Details
#_ct_reorder_children(minimum_sort_order_value = nil) ⇒ Object
33 34 35 36 |
# File 'lib/closure_tree/numeric_deterministic_ordering.rb', line 33 def _ct_reorder_children(minimum_sort_order_value = nil) scope_conditions = _ct.scope_values_from_instance(self) _ct.reorder_with_parent_id(_ct_id, minimum_sort_order_value, scope_conditions) end |
#_ct_reorder_prior_siblings_if_parent_changed ⇒ Object
14 15 16 17 18 19 20 21 22 23 24 25 |
# File 'lib/closure_tree/numeric_deterministic_ordering.rb', line 14 def _ct_reorder_prior_siblings_if_parent_changed return if @was_new_record parent_changed = saved_change_to_attribute?(_ct.parent_column_name) scope_changed = _ct.scope_changed?(self) return unless parent_changed || scope_changed was_parent_id = attribute_before_last_save(_ct.parent_column_name) previous_scope_conditions = _ct.previous_scope_values_from_instance(self) _ct.reorder_with_parent_id(was_parent_id, nil, previous_scope_conditions) end |
#_ct_reorder_siblings(minimum_sort_order_value = nil) ⇒ Object
27 28 29 30 31 |
# File 'lib/closure_tree/numeric_deterministic_ordering.rb', line 27 def _ct_reorder_siblings(minimum_sort_order_value = nil) scope_conditions = _ct.scope_values_from_instance(self) _ct.reorder_with_parent_id(_ct_parent_id, minimum_sort_order_value, scope_conditions) reload unless destroyed? end |
#add_sibling(sibling, add_after = true) ⇒ Object
155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 |
# File 'lib/closure_tree/numeric_deterministic_ordering.rb', line 155 def add_sibling(sibling, add_after = true) raise "can't add self as sibling" if self == sibling if _ct.dont_order_roots && parent.nil? raise ClosureTree::RootOrderingDisabledError, 'Root ordering is disabled on this model' end # Make sure self isn't dirty, because we're going to call reload: save _ct.with_advisory_lock do prior_sibling_parent = sibling.parent sibling.order_value = order_value sibling.parent = parent sibling.save # may be a no-op # The sort order should be correct now except for self and sibling, which may need to flip: sibling_is_after = reload.order_value < sibling.reload.order_value if add_after != sibling_is_after # We need to flip the sort orders: self_ov = order_value sib_ov = sibling.order_value update_order_value(sib_ov) sibling.update_order_value(self_ov) end prior_sibling_parent.try(:_ct_reorder_children) if prior_sibling_parent != parent sibling end end |
#append_child(child_node) ⇒ Object
133 134 135 |
# File 'lib/closure_tree/numeric_deterministic_ordering.rb', line 133 def append_child(child_node) add_child(child_node) end |
#append_sibling(sibling_node) ⇒ Object
147 148 149 |
# File 'lib/closure_tree/numeric_deterministic_ordering.rb', line 147 def append_sibling(sibling_node) add_sibling(sibling_node, true) end |
#prepend_child(child_node) ⇒ Object
137 138 139 140 141 142 143 144 145 |
# File 'lib/closure_tree/numeric_deterministic_ordering.rb', line 137 def prepend_child(child_node) child_node.order_value = -1 child_node.parent = self if child_node.save child_node.reload else child_node end end |
#prepend_sibling(sibling_node) ⇒ Object
151 152 153 |
# File 'lib/closure_tree/numeric_deterministic_ordering.rb', line 151 def prepend_sibling(sibling_node) add_sibling(sibling_node, false) end |
#self_and_descendants_preordered ⇒ Object
38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 |
# File 'lib/closure_tree/numeric_deterministic_ordering.rb', line 38 def self_and_descendants_preordered # TODO: raise NotImplementedError if sort_order is not numeric and not null? hierarchy_table = self.class.hierarchy_class.arel_table model_table = self.class.arel_table # Create aliased tables for the joins anc_hier = _ct.aliased_table(hierarchy_table, 'anc_hier') anc = _ct.aliased_table(model_table, 'anc') depths = _ct.aliased_table(hierarchy_table, 'depths') # Build the join conditions using Arel join_anc_hier = hierarchy_table .join(anc_hier) .on(anc_hier[:descendant_id].eq(hierarchy_table[:descendant_id])) join_anc = join_anc_hier .join(anc) .on(anc[self.class.primary_key].eq(anc_hier[:ancestor_id])) join_depths = join_anc .join(depths) .on( depths[:ancestor_id].eq(id) .and(depths[:descendant_id].eq(anc[self.class.primary_key])) ) self_and_descendants .joins(join_depths.join_sources) .group("#{_ct.quoted_table_name}.#{_ct.quoted_id_column_name}") .reorder(self.class._ct_sum_order_by(self)) end |