aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/relations/join.rb
diff options
context:
space:
mode:
Diffstat (limited to 'lib/active_relation/relations/join.rb')
-rw-r--r--lib/active_relation/relations/join.rb45
1 files changed, 45 insertions, 0 deletions
diff --git a/lib/active_relation/relations/join.rb b/lib/active_relation/relations/join.rb
new file mode 100644
index 0000000000..1bd1439dd6
--- /dev/null
+++ b/lib/active_relation/relations/join.rb
@@ -0,0 +1,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, :to => :relation1
+
+ private
+ def join
+ "#{join_sql} #{quote_table_name(relation2.table)} ON #{predicates.collect { |p| p.to_sql(:quote => false) }.join(' AND ')}"
+ end
+ end
+ end
+end \ No newline at end of file