diff options
author | Michael Koziarski <michael@koziarski.com> | 2006-03-27 06:19:31 +0000 |
---|---|---|
committer | Michael Koziarski <michael@koziarski.com> | 2006-03-27 06:19:31 +0000 |
commit | 445cb5c08d4eea7bd035e86d1f38ae258c635e88 (patch) | |
tree | 2cee2182724cb3725eaa41415949cb435f2c0e7a /activerecord/lib/active_record | |
parent | b8e23e37dd2342d156b36480662e6fe31b12d0ad (diff) | |
download | rails-445cb5c08d4eea7bd035e86d1f38ae258c635e88.tar.gz rails-445cb5c08d4eea7bd035e86d1f38ae258c635e88.tar.bz2 rails-445cb5c08d4eea7bd035e86d1f38ae258c635e88.zip |
Add support for :include to with_scope [andrew@redlinesoftware.com]
Remove overrated warning from adapter_test
git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@4064 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
Diffstat (limited to 'activerecord/lib/active_record')
-rwxr-xr-x | activerecord/lib/active_record/associations.rb | 18 | ||||
-rwxr-xr-x | activerecord/lib/active_record/base.rb | 30 | ||||
-rw-r--r-- | activerecord/lib/active_record/calculations.rb | 4 |
3 files changed, 36 insertions, 16 deletions
diff --git a/activerecord/lib/active_record/associations.rb b/activerecord/lib/active_record/associations.rb index ca5ae33b5a..dcb0f31684 100755 --- a/activerecord/lib/active_record/associations.rb +++ b/activerecord/lib/active_record/associations.rb @@ -929,12 +929,12 @@ module ActiveRecord end def count_with_associations(options = {}) - join_dependency = JoinDependency.new(self, options[:include], options[:joins]) + join_dependency = JoinDependency.new(self, merge_includes(scope(:find, :include), options[:include]), options[:joins]) return count_by_sql(construct_counter_sql_with_included_associations(options, join_dependency)) end def find_with_associations(options = {}) - join_dependency = JoinDependency.new(self, options[:include], options[:joins]) + join_dependency = JoinDependency.new(self, merge_includes(scope(:find, :include), options[:include]), options[:joins]) rows = select_all_rows(options, join_dependency) return join_dependency.instantiate(rows) end @@ -1084,10 +1084,10 @@ module ActiveRecord sql << " FROM #{table_name} " sql << join_dependency.join_associations.collect{|join| join.association_join }.join - sql << "#{options[:joins]} " if options[:joins] - + + add_joins!(sql, options) add_conditions!(sql, options[:conditions]) - add_limited_ids_condition!(sql, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && options[:limit] + add_limited_ids_condition!(sql, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && (scope(:find, :limit) || options[:limit]) add_limit!(sql, options) if using_limitable_reflections?(join_dependency.reflections) @@ -1099,10 +1099,10 @@ module ActiveRecord end def construct_finder_sql_with_included_associations(options, join_dependency) - sql = "SELECT #{column_aliases(join_dependency)} FROM #{options[:from] || table_name} " + sql = "SELECT #{column_aliases(join_dependency)} FROM #{scope(:find, :from) || options[:from] || table_name} " sql << join_dependency.join_associations.collect{|join| join.association_join }.join - sql << "#{options[:joins]} " if options[:joins] + add_joins!(sql, options) add_conditions!(sql, options[:conditions]) add_limited_ids_condition!(sql, options, join_dependency) if !using_limitable_reflections?(join_dependency.reflections) && options[:limit] @@ -1134,7 +1134,7 @@ module ActiveRecord if include_eager_conditions?(options) || include_eager_order?(options) sql << join_dependency.join_associations.collect{|join| join.association_join }.join - sql << "#{options[:joins]} " if options[:joins] + add_joins!(sql, options) end add_conditions!(sql, options[:conditions]) @@ -1144,7 +1144,7 @@ module ActiveRecord end def include_eager_conditions?(options) - conditions = options[:conditions] + conditions = scope(:find, :conditions) || options[:conditions] return false unless conditions conditions = conditions.first if conditions.is_a?(Array) conditions.scan(/(\w+)\.\w+/).flatten.any? do |condition_table_name| diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index c58a7bf5c2..2cdffba49f 100755 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -388,7 +388,7 @@ module ActiveRecord #:nodoc: when :first find(:all, options.merge(options[:include] ? { } : { :limit => 1 })).first when :all - records = options[:include] ? find_with_associations(options) : find_by_sql(construct_finder_sql(options)) + records = (scoped?(:find, :include) || options[:include]) ? find_with_associations(options) : find_by_sql(construct_finder_sql(options)) records.each { |record| record.readonly! } if options[:readonly] records else @@ -834,7 +834,7 @@ module ActiveRecord #:nodoc: # Scope parameters to method calls within the block. Takes a hash of method_name => parameters hash. # method_name may be :find or :create. :find parameters may include the <tt>:conditions</tt>, <tt>:joins</tt>, - # <tt>:offset</tt>, <tt>:limit</tt>, and <tt>:readonly</tt> options. :create parameters are an attributes hash. + # <tt>:include</tt>, <tt>:offset</tt>, <tt>:limit</tt>, and <tt>:readonly</tt> options. :create parameters are an attributes hash. # # Article.with_scope(:find => { :conditions => "blog_id = 1" }, :create => { :blog_id => 1 }) do # Article.find(1) # => SELECT * from articles WHERE blog_id = 1 AND id = 1 @@ -873,7 +873,7 @@ module ActiveRecord #:nodoc: method_scoping.assert_valid_keys([ :find, :create ]) if f = method_scoping[:find] - f.assert_valid_keys([ :conditions, :joins, :select, :from, :offset, :limit, :readonly ]) + f.assert_valid_keys([ :conditions, :joins, :select, :include, :from, :offset, :limit, :readonly ]) f[:readonly] = true if !f[:joins].blank? && !f.has_key?(:readonly) end @@ -882,10 +882,13 @@ module ActiveRecord #:nodoc: method_scoping = current_scoped_methods.inject(method_scoping) do |hash, (method, params)| case hash[method] when Hash - if method == :find && hash[method][:conditions] && params[:conditions] + if method == :find (hash[method].keys + params.keys).uniq.each do |key| - if key == :conditions + merge = hash[method][key] && params[key] # merge if both scopes have the same key + if key == :conditions && merge hash[method][key] = [params[key], hash[method][key]].collect{|sql| "( %s )" % sanitize_sql(sql)}.join(" AND ") + elsif key == :include && merge + hash[method][key] = merge_includes(hash[method][key], params[key]).uniq else hash[method][key] = hash[method][key] || params[key] end @@ -994,6 +997,23 @@ module ActiveRecord #:nodoc: sql end + # Merges includes so that the result is a valid +include+ + def merge_includes(first, second) + safe_to_array(first) + safe_to_array(second) + end + + # Object#to_a is deprecated, though it does have the desired behaviour + def safe_to_array(o) + case o + when NilClass + [] + when Array + o + else + [o] + end + end + def add_limit!(sql, options) options[:limit] ||= scope(:find, :limit) options[:offset] ||= scope(:find, :offset) diff --git a/activerecord/lib/active_record/calculations.rb b/activerecord/lib/active_record/calculations.rb index af976f532f..774926f409 100644 --- a/activerecord/lib/active_record/calculations.rb +++ b/activerecord/lib/active_record/calculations.rb @@ -61,7 +61,7 @@ module ActiveRecord raise(ArgumentError, "Unexpected parameters passed to count(*args): expected either count(conditions=nil, joins=nil) or count(options={})") end - options[:include] ? count_with_associations(options) : calculate(:count, :all, options) + (scope(:find, :include) || options[:include]) ? count_with_associations(options) : calculate(:count, :all, options) end # Calculates average value on a given column. The value is returned as a float. See #calculate for examples with options. @@ -221,4 +221,4 @@ module ActiveRecord end end end -end
\ No newline at end of file +end |