aboutsummaryrefslogtreecommitdiffstats
path: root/lib/active_relation/relations/join.rb
diff options
context:
space:
mode:
authorNick Kallen <nkallen@nick-kallens-computer-2.local>2008-01-10 22:55:59 -0800
committerNick Kallen <nkallen@nick-kallens-computer-2.local>2008-01-10 22:55:59 -0800
commiteeb43fc8987c2ee4a774eeab31ba8cddad11f9af (patch)
treee61d07ed4f791ba767629b08cda2cdbae277615b /lib/active_relation/relations/join.rb
parent984b1833b9c82f2f44fe6960bb8df5e41c478e07 (diff)
downloadrails-eeb43fc8987c2ee4a774eeab31ba8cddad11f9af.tar.gz
rails-eeb43fc8987c2ee4a774eeab31ba8cddad11f9af.tar.bz2
rails-eeb43fc8987c2ee4a774eeab31ba8cddad11f9af.zip
new files?
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