Skip to content

Commit

Permalink
condense order_by_ancestry, order_by_ancestry_and
Browse files Browse the repository at this point in the history
add sqlite3
  • Loading branch information
kbrock committed Jan 9, 2017
1 parent efb603e commit 75cf67e
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 18 deletions.
8 changes: 1 addition & 7 deletions lib/ancestry/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,14 +29,8 @@ def orphan_strategy= orphan_strategy

# Arrangement
def arrange options = {}
scope =
if options[:order].nil?
self.ancestry_base_class.ordered_by_ancestry
else
self.ancestry_base_class.ordered_by_ancestry_and options.delete(:order)
end
# Get all nodes ordered by ancestry and start sorting them into an empty hash
arrange_nodes scope.where(options)
arrange_nodes self.ancestry_base_class.ordered_by_ancestry_and(options.delete(:order)).where(options)
end

# Arrange array of nodes into a nested hash of the form
Expand Down
16 changes: 5 additions & 11 deletions lib/ancestry/has_ancestry.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,20 +47,14 @@ def has_ancestry options = {}
scope :descendants_of, lambda { |object| where(descendant_conditions(object)) }
scope :subtree_of, lambda { |object| where(subtree_conditions(object)) }
scope :siblings_of, lambda { |object| where(sibling_conditions(object)) }
scope :ordered_by_ancestry, lambda {
if %w(mysql mysql2 sqlite postgresql).include?(connection.adapter_name.downcase) && ActiveRecord::VERSION::MAJOR >= 5
reorder("coalesce(#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)}, '')")
scope :ordered_by_ancestry, Proc.new { |order|
if %w(mysql mysql2 sqlite sqlite3 postgresql).include?(connection.adapter_name.downcase) && ActiveRecord::VERSION::MAJOR >= 5
reorder("coalesce(#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)}, '')", order)
else
reorder("(CASE WHEN #{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)} IS NULL THEN 0 ELSE 1 END), #{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)}")
end
}
scope :ordered_by_ancestry_and, lambda { |order|
if %w(mysql mysql2 sqlite postgresql).include?(connection.adapter_name.downcase) && ActiveRecord::VERSION::MAJOR >= 5
reorder("coalesce(#{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)}, ''), #{order}")
else
reorder("(CASE WHEN #{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)} IS NULL THEN 0 ELSE 1 END), #{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)}, #{order}")
reorder("(CASE WHEN #{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)} IS NULL THEN 0 ELSE 1 END), #{connection.quote_table_name(table_name)}.#{connection.quote_column_name(ancestry_column)}", order)
end
}
scope :ordered_by_ancestry_and, Proc.new { |order| ordered_by_ancestry_and(order) }
scope :path_of, lambda { |object| to_node(object).path }

# Update descendants with new ancestry before save
Expand Down

0 comments on commit 75cf67e

Please sign in to comment.