aboutsummaryrefslogtreecommitdiffstats
path: root/lib/arel/algebra/relations/utilities/compound.rb
blob: 451097b5510d36d0db7a0d2a661d8eea3b608ab4 (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
31
32
33
34
35
module Arel
  class Compound
    include Relation

    attr_reader :relation
    delegate :joins, :join?, :inserts, :taken, :skipped, :name, :externalizable?,
             :column_for, :sources, :locked, :table_alias, :array,
             :to => :relation

    def initialize relation
      @relation = relation
      @attributes = nil
    end

    [:wheres, :groupings, :orders, :havings, :projections].each do |operation_name|
      class_eval <<-OPERATION, __FILE__, __LINE__
        def #{operation_name}
          @#{operation_name} ||= relation.#{operation_name}.collect { |o| o.bind(self) }
        end
      OPERATION
    end

    def attributes
      @attributes ||= relation.attributes.bind(self)
    end

    def unoperated_rows
      relation.call.collect { |row| row.bind(self) }
    end

    def engine
      relation.engine
    end
  end
end