From 3c6fc3297ed2f413db7cc7b584cd2e32b211a081 Mon Sep 17 00:00:00 2001 From: Keenan Brock Date: Tue, 26 Sep 2023 11:50:03 -0400 Subject: [PATCH] remove duplication for before_depth scope and friends --- lib/ancestry/has_ancestry.rb | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/lib/ancestry/has_ancestry.rb b/lib/ancestry/has_ancestry.rb index 55daed94..f14b68fa 100644 --- a/lib/ancestry/has_ancestry.rb +++ b/lib/ancestry/has_ancestry.rb @@ -89,20 +89,18 @@ def has_ancestry options = {} # Validate depth column validates_numericality_of depth_cache_column, :greater_than_or_equal_to => 0, :only_integer => true, :allow_nil => false - scope :before_depth, lambda { |depth| where("#{depth_cache_column} < ?", depth) } - scope :to_depth, lambda { |depth| where("#{depth_cache_column} <= ?", depth) } - scope :at_depth, lambda { |depth| where("#{depth_cache_column} = ?", depth) } - scope :from_depth, lambda { |depth| where("#{depth_cache_column} >= ?", depth) } - scope :after_depth, lambda { |depth| where("#{depth_cache_column} > ?", depth) } + depth_cache_sql = depth_cache_column else # this is not efficient, but it works - scope :before_depth, lambda { |depth| where("#{ancestry_depth_sql} < ?", depth) } - scope :to_depth, lambda { |depth| where("#{ancestry_depth_sql} <= ?", depth) } - scope :at_depth, lambda { |depth| where("#{ancestry_depth_sql} = ?", depth) } - scope :from_depth, lambda { |depth| where("#{ancestry_depth_sql} >= ?", depth) } - scope :after_depth, lambda { |depth| where("#{ancestry_depth_sql} > ?", depth) } + depth_cache_sql = ancestry_depth_sql end + scope :before_depth, lambda { |depth| where("#{depth_cache_sql} < ?", depth) } + scope :to_depth, lambda { |depth| where("#{depth_cache_sql} <= ?", depth) } + scope :at_depth, lambda { |depth| where("#{depth_cache_sql} = ?", depth) } + scope :from_depth, lambda { |depth| where("#{depth_cache_sql} >= ?", depth) } + scope :after_depth, lambda { |depth| where("#{depth_cache_sql} > ?", depth) } + # Create counter cache column accessor and set to option or default if options[:counter_cache] cattr_accessor :counter_cache_column