diff options
author | Jon Leighton <j@jonathanleighton.com> | 2011-03-02 21:24:56 +0000 |
---|---|---|
committer | Jon Leighton <j@jonathanleighton.com> | 2011-03-04 09:30:27 +0000 |
commit | 735844db712c511dd8abf36a5279318fbc0ff9d0 (patch) | |
tree | 5fbd5d224ef85d8c878bf221db98b422c9345466 /activerecord/lib/active_record/associations/preloader/association.rb | |
parent | 9a98c766e045aebc2ef6d5b716936b73407f095d (diff) | |
parent | b171b9e73dcc6a89b1da652da61c5127fe605b51 (diff) | |
download | rails-735844db712c511dd8abf36a5279318fbc0ff9d0.tar.gz rails-735844db712c511dd8abf36a5279318fbc0ff9d0.tar.bz2 rails-735844db712c511dd8abf36a5279318fbc0ff9d0.zip |
Merge branch 'master' into nested_has_many_through
Conflicts:
activerecord/CHANGELOG
activerecord/lib/active_record/association_preload.rb
activerecord/lib/active_record/associations.rb
activerecord/lib/active_record/associations/class_methods/join_dependency.rb
activerecord/lib/active_record/associations/class_methods/join_dependency/join_association.rb
activerecord/lib/active_record/associations/has_many_association.rb
activerecord/lib/active_record/associations/has_many_through_association.rb
activerecord/lib/active_record/associations/has_one_association.rb
activerecord/lib/active_record/associations/has_one_through_association.rb
activerecord/lib/active_record/associations/through_association_scope.rb
activerecord/lib/active_record/reflection.rb
activerecord/test/cases/associations/has_many_through_associations_test.rb
activerecord/test/cases/associations/has_one_through_associations_test.rb
activerecord/test/cases/reflection_test.rb
activerecord/test/cases/relations_test.rb
activerecord/test/fixtures/memberships.yml
activerecord/test/models/categorization.rb
activerecord/test/models/category.rb
activerecord/test/models/member.rb
activerecord/test/models/reference.rb
activerecord/test/models/tagging.rb
Diffstat (limited to 'activerecord/lib/active_record/associations/preloader/association.rb')
-rw-r--r-- | activerecord/lib/active_record/associations/preloader/association.rb | 126 |
1 files changed, 126 insertions, 0 deletions
diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb new file mode 100644 index 0000000000..7256dd5288 --- /dev/null +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -0,0 +1,126 @@ +module ActiveRecord + module Associations + class Preloader + class Association #:nodoc: + attr_reader :owners, :reflection, :preload_options, :model, :klass + + def initialize(klass, owners, reflection, preload_options) + @klass = klass + @owners = owners + @reflection = reflection + @preload_options = preload_options || {} + @model = owners.first && owners.first.class + @scoped = nil + @owners_by_key = nil + end + + def run + unless owners.first.association(reflection.name).loaded? + preload + end + end + + def preload + raise NotImplementedError + end + + def scoped + @scoped ||= build_scope + end + + def records_for(ids) + scoped.where(association_key.in(ids)) + end + + def table + klass.arel_table + end + + # The name of the key on the associated records + def association_key_name + raise NotImplementedError + end + + # This is overridden by HABTM as the condition should be on the foreign_key column in + # the join table + def association_key + table[association_key_name] + end + + # The name of the key on the model which declares the association + def owner_key_name + raise NotImplementedError + end + + # We're converting to a string here because postgres will return the aliased association + # key in a habtm as a string (for whatever reason) + def owners_by_key + @owners_by_key ||= owners.group_by do |owner| + key = owner[owner_key_name] + key && key.to_s + end + end + + def options + reflection.options + end + + private + + def associated_records_by_owner + owner_keys = owners.map { |owner| owner[owner_key_name] }.compact.uniq + + if klass.nil? || owner_keys.empty? + records = [] + else + # Some databases impose a limit on the number of ids in a list (in Oracle it's 1000) + # Make several smaller queries if necessary or make one query if the adapter supports it + sliced = owner_keys.each_slice(model.connection.in_clause_length || owner_keys.size) + records = sliced.map { |slice| records_for(slice) }.flatten + end + + # Each record may have multiple owners, and vice-versa + records_by_owner = Hash[owners.map { |owner| [owner, []] }] + records.each do |record| + owner_key = record[association_key_name].to_s + + owners_by_key[owner_key].each do |owner| + records_by_owner[owner] << record + end + end + records_by_owner + end + + def build_scope + scope = klass.scoped + + scope = scope.where(process_conditions(options[:conditions])) + scope = scope.where(process_conditions(preload_options[:conditions])) + + scope = scope.select(preload_options[:select] || options[:select] || table[Arel.star]) + scope = scope.includes(preload_options[:include] || options[:include]) + + if options[:as] + scope = scope.where( + klass.table_name => { + reflection.type => model.base_class.sti_name + } + ) + end + + scope + end + + def process_conditions(conditions) + if conditions.respond_to?(:to_proc) + conditions = klass.send(:instance_eval, &conditions) + end + + if conditions + klass.send(:sanitize_sql, conditions) + end + end + end + end + end +end |