From 1952c145bafa94d6ed5fed70b1103f5b22d0e057 Mon Sep 17 00:00:00 2001 From: Hamed Asghari Date: Sun, 9 Feb 2020 10:50:36 -0700 Subject: [PATCH] Use ActiveSupport::Concern to clean up dependencies --- lib/parentry.rb | 69 ++++++++++++++++++++++++------------------------- 1 file changed, 34 insertions(+), 35 deletions(-) diff --git a/lib/parentry.rb b/lib/parentry.rb index 19ccc1a..fea0a70 100644 --- a/lib/parentry.rb +++ b/lib/parentry.rb @@ -6,50 +6,49 @@ module Parentry autoload :Navigation, 'parentry/navigation' autoload :ClassMethods, 'parentry/class_methods' - # rubocop:disable Metrics/AbcSize,Metrics/MethodLength,Metrics/BlockLength - def self.included(base) - base.class_eval do - mattr_accessor :parentry_strategy, :parentry_column, :depth_offset, :cache_depth, :touch_ancestors + extend ActiveSupport::Concern - belongs_to :parent, class_name: base_class.name, optional: true - has_many :children, class_name: base_class.name, foreign_key: :parent_id, dependent: :destroy + # rubocop:disable Metrics/BlockLength + included do + include Navigation + include InstanceMethods - validate do - errors.add(:parent, 'must be persisted') unless parent.blank? || parent.persisted? - end + mattr_accessor :parentry_strategy, :parentry_column, :depth_offset, :cache_depth, :touch_ancestors - validate :prevent_circular_parentry + belongs_to :parent, class_name: base_class.name, optional: true + has_many :children, class_name: base_class.name, foreign_key: :parent_id, dependent: :destroy - after_create :commit_parentry - before_update :assign_parentry, if: proc { changes[:parent_id].present? } - after_update :cascade_parentry, if: proc { saved_changes[parentry_column].present? } + validate do + errors.add(:parent, 'must be persisted') unless parent.blank? || parent.persisted? + end - before_validation :cache_parentry_depth, if: proc { cache_depth } - before_save :cache_parentry_depth, if: proc { cache_depth } + validate :prevent_circular_parentry - after_save :touch_ancestors_callback - after_touch :touch_ancestors_callback - after_destroy :touch_ancestors_callback + after_create :commit_parentry + before_update :assign_parentry, if: proc { changes[:parent_id].present? } + after_update :cascade_parentry, if: proc { saved_changes[parentry_column].present? } - scope :order_by_parentry, -> { order(parentry_depth_function) } + before_validation :cache_parentry_depth, if: proc { cache_depth } + before_save :cache_parentry_depth, if: proc { cache_depth } - scope :before_depth, ->(depth) { where("#{parentry_depth_function} - 1 < ?", depth + depth_offset) } - scope :to_depth, ->(depth) { where("#{parentry_depth_function} - 1 <= ?", depth + depth_offset) } - scope :at_depth, ->(depth) { where("#{parentry_depth_function} - 1 = ?", depth + depth_offset) } - scope :from_depth, ->(depth) { where("#{parentry_depth_function} - 1 >= ?", depth + depth_offset) } - scope :after_depth, ->(depth) { where("#{parentry_depth_function} - 1 > ?", depth + depth_offset) } + after_save :touch_ancestors_callback + after_touch :touch_ancestors_callback + after_destroy :touch_ancestors_callback - scope :roots, -> { where("#{parentry_depth_function} = 1") } - scope :ancestors_of, ->(node) { where(node.ancestor_conditions).where.not(id: node.id) } - scope :children_of, ->(node) { where(parent_id: node.id) } - scope :descendants_of, ->(node) { subtree_of(node).where.not(id: node.id) } - scope :subtree_of, ->(node) { where(node.subtree_conditions) } - scope :siblings_of, ->(node) { where(parent_id: node.parent_id).where.not(id: node.id) } - end + scope :order_by_parentry, -> { order(parentry_depth_function) } + + scope :before_depth, ->(depth) { where("#{parentry_depth_function} - 1 < ?", depth + depth_offset) } + scope :to_depth, ->(depth) { where("#{parentry_depth_function} - 1 <= ?", depth + depth_offset) } + scope :at_depth, ->(depth) { where("#{parentry_depth_function} - 1 = ?", depth + depth_offset) } + scope :from_depth, ->(depth) { where("#{parentry_depth_function} - 1 >= ?", depth + depth_offset) } + scope :after_depth, ->(depth) { where("#{parentry_depth_function} - 1 > ?", depth + depth_offset) } - base.send :include, Navigation - base.send :include, InstanceMethods - base.extend ClassMethods + scope :roots, -> { where("#{parentry_depth_function} = 1") } + scope :ancestors_of, ->(node) { where(node.ancestor_conditions).where.not(id: node.id) } + scope :children_of, ->(node) { where(parent_id: node.id) } + scope :descendants_of, ->(node) { subtree_of(node).where.not(id: node.id) } + scope :subtree_of, ->(node) { where(node.subtree_conditions) } + scope :siblings_of, ->(node) { where(parent_id: node.parent_id).where.not(id: node.id) } end - # rubocop:enable Metrics/AbcSize,Metrics/MethodLength,Metrics/BlockLength + # rubocop:enable Metrics/BlockLength end