diff --git a/lib/ancestry/class_methods.rb b/lib/ancestry/class_methods.rb index ba4120d1..db493982 100644 --- a/lib/ancestry/class_methods.rb +++ b/lib/ancestry/class_methods.rb @@ -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 diff --git a/lib/ancestry/has_ancestry.rb b/lib/ancestry/has_ancestry.rb index 927412bd..d828f44d 100644 --- a/lib/ancestry/has_ancestry.rb +++ b/lib/ancestry/has_ancestry.rb @@ -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