Sha256: d1425ca8f32c15d6fee81809d3a02f4ac85fcd545a0cf7e89825157fe94a8570

Contents?: true

Size: 738 Bytes

Versions: 4

Compression:

Stored size: 738 Bytes

Contents

module Searchgasm
  module Condition
    class DescendantOf < Tree      
      def to_conditions(value)
        # Wish I knew how to do this in SQL
        root = (value.is_a?(klass) ? value : klass.find(value)) rescue return
        strs = []
        subs = []
        all_children_ids(root).each do |child_id|
          strs << "#{quoted_table_name}.#{quote_column_name(klass.primary_key)} = ?"
          subs << child_id
        end
        [strs.join(" OR "), *subs]
      end
      
      private
        def all_children_ids(record)
          ids = record.children.collect { |child| child.send(klass.primary_key) }
          record.children.each { |child| ids += all_children_ids(child) }
          ids
        end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
searchgasm-1.5.0 lib/searchgasm/condition/descendant_of.rb
searchgasm-1.5.2 lib/searchgasm/condition/descendant_of.rb
searchgasm-1.5.1 lib/searchgasm/condition/descendant_of.rb
searchgasm-1.5.3 lib/searchgasm/condition/descendant_of.rb