ClosureTree/closure_tree

reason for with_ancestor method using map

ggLee6352 opened this issue · 2 comments

# lib/closure_tree/finders.rb

def with_ancestor(*ancestors)
  ancestor_ids = ancestors.map { |ea| ea.is_a?(ActiveRecord::Base) ? ea._ct_id : ea }
  scope = ancestor_ids.blank? ? all : joins(:ancestor_hierarchies).
    where("#{_ct.hierarchy_table_name}.ancestor_id" => ancestor_ids).
    where("#{_ct.hierarchy_table_name}.generations > 0").
    readonly(false)
  _ct.scope_with_order(scope)
end
def with_ancestor(*ancestors)
  scope = ancestor_ids.blank? ? all : joins(:ancestor_hierarchies).
    where("#{_ct.hierarchy_table_name}.ancestor_id" => ancestor_ids).
    where("#{_ct.hierarchy_table_name}.generations > 0").
    readonly(false)
  _ct.scope_with_order(scope)
end

seem to be the same

@ggLee6352 map was used to process *ancestors arguments if given any to filter out the results that is descendants of given ancestors. If no argument passed, then will use all scope instead

Model.with_ancestor(1, 2).to_sql
=> SELECT "models".* FROM "models" INNER JOIN "model_hierarchies" ON "model_hierarchies"."descendant_id" = "models"."id" WHERE "model_hierarchies"."ancestor_id" IN (1, 2) AND (model_hierarchies.generations > 0)

Model.with_ancestor([1, 2]).to_sql
=> SELECT "models".* FROM "models" INNER JOIN "model_hierarchies" ON "model_hierarchies"."descendant_id" = "models"."id" WHERE "model_hierarchies"."ancestor_id" IN (1, 2) AND (model_hierarchies.generations > 0)

Model.with_ancestor.to_sql
=> SELECT "models".* FROM "models"

Your example by removing ancestors.map { ... } will raise error caused ancestor_ids was not defined since with_ancestor method was a class method not instance method (with_ancestor defined under ClassMethods module)

Is this still an issue? Can it be closed?