lib/fertile_forest/modules/finders.rb
#
# Find trunk (= ancestor) nodes from base node in ordered range.
#
# @param base_obj [Entity|Integer] Base node|id to find.
# @param range [Integer] Ordered range of trunk nodes.
#   -1:To designate as root node.
# @param columns [Array] Columns for SELECT clause.
# @return [ActiveRecord::Relation] Basic query for finding trunk nodes.
# @return [nil] No trunk nodes.
#
Find trunk (= ancestor) nodes from base node in ordered range.
Find trunk ( = ancestor ) nodes from base node in ordered range .
def trunk(base_obj, range = ANCESTOR_ALL, columns = nil)
            base_node = ff_resolve_nodes(base_obj)
            return nil if base_node.blank?
            aim_queue = base_node.ff_queue
            aim_depth = base_node.ff_depth
            aim_grove = base_node.ff_grove  
            return nil if aim_depth == ROOT_DEPTH
            ffqq = arel_table[@_ff_queue]
            ffdd = arel_table[@_ff_depth]
            ffgg = arel_table[@_ff_grove]
            aim_subquery = ff_usual_projection(aim_grove)
                .where(ffqq.lt(aim_queue))
            if range < 0
              aim_subquery = aim_subquery.where(ffdd.eq(ROOT_DEPTH))
            else
              aim_subquery = aim_subquery.where(ffdd.lt(aim_depth))
              aim_subquery = aim_subquery.where(ffdd.gteq(aim_depth - range)) \
                  if 0 < range
            end
            aim_group = [ffdd]
            aim_group.unshift(ffgg) if has_grove?
            aim_subquery = aim_subquery.project(ffqq.maximum.as('ancestor_queue'))
              .group(aim_group)
            ff_required_columns_scope()
                .ff_usual_conditions_scope(aim_grove)
                .ff_usual_order_scope()
                .where(ffqq.in(aim_subquery))
                .select(ff_all_optional_columns(columns))
          end
def trunk ( base_obj , range = ANCESTOR_ALL , columns = nil ) base_node = ff_resolve_nodes ( base_obj ) return nil if base_node . blank? aim_queue = base_node . ff_queue aim_depth = base_node . ff_depth aim_grove = base_node . ff_grove return nil if aim_depth == ROOT_DEPTH ffqq = arel_table [ @_ff_queue ] ffdd = arel_table [ @_ff_depth ] ffgg = arel_table [ @_ff_grove ] aim_subquery = ff_usual_projection ( aim_grove ) . where ( ffqq . lt ( aim_queue ) ) if range < 0 aim_subquery = aim_subquery . where ( ffdd . eq ( ROOT_DEPTH ) ) else aim_subquery = aim_subquery . where ( ffdd . lt ( aim_depth ) ) aim_subquery = aim_subquery . where ( ffdd . gteq ( aim_depth - range ) ) if 0 < range end aim_group = [ ffdd ] aim_group . unshift ( ffgg ) if has_grove? aim_subquery = aim_subquery . project ( ffqq . maximum . as ( 'ancestor_queue' ) ) . group ( aim_group ) ff_required_columns_scope ( ) . ff_usual_conditions_scope ( aim_grove ) . ff_usual_order_scope ( ) . where ( ffqq . in ( aim_subquery ) ) . select ( ff_all_optional_columns ( columns ) ) end
Find trunk (= ancestor) nodes from base node in ordered range.
Find trunk ( = ancestor ) nodes from base node in ordered range .
# When no grove, nil # create subquery to find queues of ancestor # find nodes by ancestor queues # must use IN(), because trunk() is for general purpose to find ancestors # When one row, can use "=". When plural, can not use "=". # Error: SQLSTATE[21000]: Cardinality violation: 1242 Subquery returns more than 1 row
Basic query for finding trunk nodes.
Basic query for finding trunk nodes .
ActiveRecord::Relation
No trunk nodes.
No trunk nodes .
nil
base_obj
Base node|id to find.
Base node|id to find .
range
Ordered range of trunk nodes. -1:To designate as root node.
Ordered range of trunk nodes . - 1 : To designate as root node .
columns
Columns for SELECT clause.
Columns for SELECT clause .