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 | |
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')
10 files changed, 377 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 diff --git a/activerecord/lib/active_record/associations/preloader/belongs_to.rb b/activerecord/lib/active_record/associations/preloader/belongs_to.rb new file mode 100644 index 0000000000..5091d4717a --- /dev/null +++ b/activerecord/lib/active_record/associations/preloader/belongs_to.rb @@ -0,0 +1,17 @@ +module ActiveRecord + module Associations + class Preloader + class BelongsTo < SingularAssociation #:nodoc: + + def association_key_name + reflection.options[:primary_key] || klass && klass.primary_key + end + + def owner_key_name + reflection.foreign_key + end + + end + end + end +end diff --git a/activerecord/lib/active_record/associations/preloader/collection_association.rb b/activerecord/lib/active_record/associations/preloader/collection_association.rb new file mode 100644 index 0000000000..c248aeaaf6 --- /dev/null +++ b/activerecord/lib/active_record/associations/preloader/collection_association.rb @@ -0,0 +1,24 @@ +module ActiveRecord + module Associations + class Preloader + class CollectionAssociation < Association #:nodoc: + + private + + def build_scope + super.order(preload_options[:order] || options[:order]) + end + + def preload + associated_records_by_owner.each do |owner, records| + association = owner.association(reflection.name) + association.loaded! + association.target.concat(records) + records.each { |record| association.set_inverse_instance(record) } + end + end + + end + end + end +end diff --git a/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb b/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb new file mode 100644 index 0000000000..e794f05340 --- /dev/null +++ b/activerecord/lib/active_record/associations/preloader/has_and_belongs_to_many.rb @@ -0,0 +1,58 @@ +module ActiveRecord + module Associations + class Preloader + class HasAndBelongsToMany < CollectionAssociation #:nodoc: + attr_reader :join_table + + def initialize(klass, records, reflection, preload_options) + super + @join_table = Arel::Table.new(options[:join_table]).alias('t0') + end + + # Unlike the other associations, we want to get a raw array of rows so that we can + # access the aliased column on the join table + def records_for(ids) + scope = super + klass.connection.select_all(scope.arel.to_sql, 'SQL', scope.bind_values) + end + + def owner_key_name + reflection.active_record_primary_key + end + + def association_key_name + 'ar_association_key_name' + end + + def association_key + join_table[reflection.foreign_key] + end + + private + + # Once we have used the join table column (in super), we manually instantiate the + # actual records + def associated_records_by_owner + super.each do |owner_key, rows| + rows.map! { |row| klass.instantiate(row) } + end + end + + def build_scope + super.joins(join).select(join_select) + end + + def join_select + association_key.as(Arel.sql(association_key_name)) + end + + def join + condition = table[reflection.association_primary_key].eq( + join_table[reflection.association_foreign_key]) + + table.create_join(join_table, table.create_on(condition)) + end + end + end + end +end diff --git a/activerecord/lib/active_record/associations/preloader/has_many.rb b/activerecord/lib/active_record/associations/preloader/has_many.rb new file mode 100644 index 0000000000..3ea91a8c11 --- /dev/null +++ b/activerecord/lib/active_record/associations/preloader/has_many.rb @@ -0,0 +1,17 @@ +module ActiveRecord + module Associations + class Preloader + class HasMany < CollectionAssociation #:nodoc: + + def association_key_name + reflection.foreign_key + end + + def owner_key_name + reflection.active_record_primary_key + end + + end + end + end +end diff --git a/activerecord/lib/active_record/associations/preloader/has_many_through.rb b/activerecord/lib/active_record/associations/preloader/has_many_through.rb new file mode 100644 index 0000000000..c6e9ede356 --- /dev/null +++ b/activerecord/lib/active_record/associations/preloader/has_many_through.rb @@ -0,0 +1,15 @@ +module ActiveRecord + module Associations + class Preloader + class HasManyThrough < CollectionAssociation #:nodoc: + include ThroughAssociation + + def associated_records_by_owner + super.each do |owner, records| + records.uniq! if options[:uniq] + end + end + end + end + end +end diff --git a/activerecord/lib/active_record/associations/preloader/has_one.rb b/activerecord/lib/active_record/associations/preloader/has_one.rb new file mode 100644 index 0000000000..848448bb48 --- /dev/null +++ b/activerecord/lib/active_record/associations/preloader/has_one.rb @@ -0,0 +1,23 @@ +module ActiveRecord + module Associations + class Preloader + class HasOne < SingularAssociation #:nodoc: + + def association_key_name + reflection.foreign_key + end + + def owner_key_name + reflection.active_record_primary_key + end + + private + + def build_scope + super.order(preload_options[:order] || options[:order]) + end + + end + end + end +end diff --git a/activerecord/lib/active_record/associations/preloader/has_one_through.rb b/activerecord/lib/active_record/associations/preloader/has_one_through.rb new file mode 100644 index 0000000000..f063f85574 --- /dev/null +++ b/activerecord/lib/active_record/associations/preloader/has_one_through.rb @@ -0,0 +1,9 @@ +module ActiveRecord + module Associations + class Preloader + class HasOneThrough < SingularAssociation #:nodoc: + include ThroughAssociation + end + end + end +end diff --git a/activerecord/lib/active_record/associations/preloader/singular_association.rb b/activerecord/lib/active_record/associations/preloader/singular_association.rb new file mode 100644 index 0000000000..44e804d785 --- /dev/null +++ b/activerecord/lib/active_record/associations/preloader/singular_association.rb @@ -0,0 +1,21 @@ +module ActiveRecord + module Associations + class Preloader + class SingularAssociation < Association #:nodoc: + + private + + def preload + associated_records_by_owner.each do |owner, associated_records| + record = associated_records.first + + association = owner.association(reflection.name) + association.target = record + association.set_inverse_instance(record) + end + end + + end + end + end +end diff --git a/activerecord/lib/active_record/associations/preloader/through_association.rb b/activerecord/lib/active_record/associations/preloader/through_association.rb new file mode 100644 index 0000000000..ad6374d09a --- /dev/null +++ b/activerecord/lib/active_record/associations/preloader/through_association.rb @@ -0,0 +1,67 @@ +module ActiveRecord + module Associations + class Preloader + module ThroughAssociation #:nodoc: + + def through_reflection + reflection.through_reflection + end + + def source_reflection + reflection.source_reflection + end + + def associated_records_by_owner + through_records = through_records_by_owner + + ActiveRecord::Associations::Preloader.new( + through_records.values.flatten, + source_reflection.name, options + ).run + + through_records.each do |owner, records| + records.map! { |r| r.send(source_reflection.name) }.flatten! + records.compact! + end + end + + private + + def through_records_by_owner + ActiveRecord::Associations::Preloader.new( + owners, through_reflection.name, + through_options + ).run + + Hash[owners.map do |owner| + through_records = Array.wrap(owner.send(through_reflection.name)) + + # Dont cache the association - we would only be caching a subset + if reflection.options[:source_type] && through_reflection.collection? + owner.association(through_reflection.name).reset + end + + [owner, through_records] + end] + end + + def through_options + through_options = {} + + if options[:source_type] + through_options[:conditions] = { reflection.foreign_type => options[:source_type] } + else + if options[:conditions] + through_options[:include] = options[:include] || options[:source] + through_options[:conditions] = options[:conditions] + end + + through_options[:order] = options[:order] + end + + through_options + end + end + end + end +end |