aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/relations/join.rb
blob: 4e6c85979e82735c68b16165dfb24247870591e7 (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
36
37
38
39
40
41
42
43
44
45
module ActiveRelation
  module Relations
    class Join < Base
      attr_reader :join_sql, :relation1, :relation2, :predicates
  
      def initialize(join_sql, relation1, relation2, *predicates)
        @join_sql, @relation1, @relation2, @predicates = join_sql, relation1, relation2, predicates
      end
  
      def ==(other)
        predicates == other.predicates and
          ((relation1 == other.relation1 and relation2 == other.relation2) or
          (relation2 == other.relation1 and relation1 == other.relation2))
      end

      def qualify
        Join.new(join_sql, relation1.qualify, relation2.qualify, *predicates.collect(&:qualify))
      end
  
      protected
      def joins
        [relation1.joins, relation2.joins, join].compact.join(" ")
      end
  
      def selects
        relation1.send(:selects) + relation2.send(:selects)
      end
  
      def attributes
        relation1.attributes + relation2.attributes
      end
  
      def attribute(name)
        relation1[name] || relation2[name]
      end
  
      delegate :table_sql, :to => :relation1
  
      private
      def join
        "#{join_sql} #{relation2.send(:table_sql)} ON #{predicates.collect { |p| p.to_sql(Sql::Predicate.new) }.join(' AND ')}"
      end
    end
  end
end