Module: Parentry::ClassMethods
- Defined in:
- lib/parentry/class_methods.rb
Instance Method Summary collapse
-
#arrange(options = {}) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength.
- #parentry(options = {}) ⇒ Object
Instance Method Details
#arrange(options = {}) ⇒ Object
rubocop:disable Metrics/AbcSize,Metrics/MethodLength
15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/parentry/class_methods.rb', line 15 def arrange( = {}) scope = if (order = .delete(:order)) base_class.order_by_parentry.order(order) else base_class.order_by_parentry end scope.where().each_with_object(Hash.new { |h, k| h[k] = {} }) do |node, memo| insert_node = node.ancestor_ids.reduce(memo) do |subtree, ancestor_id| match = subtree.find { |parent, _children| parent.id == ancestor_id } match ? match[1] : subtree end insert_node[node] = {} end end |
#parentry(options = {}) ⇒ Object
3 4 5 6 7 8 9 10 11 12 |
# File 'lib/parentry/class_methods.rb', line 3 def parentry( = {}) self.parentry_strategy = .fetch(:strategy, 'ltree').to_s self.parentry_column = .fetch(:parentry_column, 'parentry') self.depth_offset = .fetch(:depth_offset, 0) self.cache_depth = .fetch(:cache_depth, false) self.touch_ancestors = .fetch(:touch, false) include Strategy::Ltree if parentry_strategy == 'ltree' include Strategy::Array if parentry_strategy == 'array' end |