aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/algebra/relations/operations/where.rb
blob: d851aebc4495cb5878b5928405c80d0c8d128bb8 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
module Arel
  class Where < Compound
    attr_reader :predicates

    def initialize(relation, predicates)
      super(relation)
      @predicates = predicates.map { |p| p.bind(relation) }
      @wheres = nil
    end

    def wheres
      @wheres ||= relation.wheres + predicates
    end

    def engine
      engine   = relation.engine

      # Temporary check of whether or not the engine supports where.
      if engine.respond_to?(:supports) && !engine.supports(:restricting)
        Memory::Engine.new
      else
        engine
      end
    end

    def eval
      unoperated_rows.select { |row| predicates.all? { |p| p.eval(row) } }
    end
  end
end