From c1cbf02e3170f1004daf4a146cbc41176c2458d3 Mon Sep 17 00:00:00 2001 From: Emilio Tagua Date: Fri, 31 Jul 2009 16:08:22 -0300 Subject: Added ActiveRecord::Relation tests. Allow Relation to accept conditional hashes and arrays like #find does. --- activerecord/lib/active_record/base.rb | 8 ++++++-- activerecord/lib/active_record/relation.rb | 15 ++++++++++++++- 2 files changed, 20 insertions(+), 3 deletions(-) (limited to 'activerecord/lib') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 7a34e345a2..9a3a02870a 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -663,7 +663,11 @@ module ActiveRecord #:nodoc: # This is an alias for find(:all). You can pass in all the same arguments to this method as you can # to find(:all) def all(*args) - construct_finder_arel(*args) + if args.empty? && !scoped?(:find) + arel_table + else + construct_finder_arel(*args) + end end # Executes a custom SQL query against your database and returns all the results. The results will @@ -2992,7 +2996,7 @@ module ActiveRecord #:nodoc: end def arel_table(reload = nil) - @arel_table = Relation.new(self, self.class.table_name) if reload || @arel_table.nil? + @arel_table = Relation.new(self.class, self.class.table_name) if reload || @arel_table.nil? @arel_table end diff --git a/activerecord/lib/active_record/relation.rb b/activerecord/lib/active_record/relation.rb index 36e8d98298..1c3a1dc53b 100644 --- a/activerecord/lib/active_record/relation.rb +++ b/activerecord/lib/active_record/relation.rb @@ -1,7 +1,7 @@ module ActiveRecord class Relation delegate :delete, :to_sql, :to => :relation - CLAUSES_METHODS = ["where", "join", "project", "group", "order", "take", "skip"].freeze + CLAUSES_METHODS = ["project", "group", "order", "take", "skip"].freeze attr_reader :relation, :klass def initialize(klass, table = nil) @@ -27,6 +27,19 @@ module ActiveRecord } end + def join(joins) + @relation = @relation.join(@klass.send(:construct_join, joins, nil)) if !joins.blank? + self + end + + def where(conditions) + if !conditions.blank? + conditions = @klass.send(:merge_conditions, conditions) if [String, Hash, Array].include?(conditions.class) + @relation = @relation.where(conditions) + end + self + end + private def method_missing(method, *args, &block) if @relation.respond_to?(method) -- cgit v1.2.3