Skip to content

Commit

Permalink
remove duplication for before_depth scope and friends
Browse files Browse the repository at this point in the history
  • Loading branch information
kbrock committed Sep 26, 2023
1 parent 3f08e10 commit 3c6fc32
Showing 1 changed file with 8 additions and 10 deletions.
18 changes: 8 additions & 10 deletions lib/ancestry/has_ancestry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 3c6fc32

Please sign in to comment.