diff options
author | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-24 11:36:14 -0700 |
---|---|---|
committer | Aaron Patterson <aaron.patterson@gmail.com> | 2013-09-24 11:36:14 -0700 |
commit | 2075f39d726cef361170218fd16421fc52bed5a8 (patch) | |
tree | 77de22bb625bf26927cdebb2d2c399c5d0707a02 /activerecord/lib/active_record | |
parent | b93d09dbc59b3b85e7208cd57c92be9d86fd51df (diff) | |
download | rails-2075f39d726cef361170218fd16421fc52bed5a8.tar.gz rails-2075f39d726cef361170218fd16421fc52bed5a8.tar.bz2 rails-2075f39d726cef361170218fd16421fc52bed5a8.zip |
eliminate the `loaded?` conditional
Diffstat (limited to 'activerecord/lib/active_record')
3 files changed, 7 insertions, 12 deletions
diff --git a/activerecord/lib/active_record/associations/preloader.rb b/activerecord/lib/active_record/associations/preloader.rb index 8a975468f2..9f03d60368 100644 --- a/activerecord/lib/active_record/associations/preloader.rb +++ b/activerecord/lib/active_record/associations/preloader.rb @@ -174,14 +174,18 @@ module ActiveRecord end class NullPreloader - attr_reader :owners + attr_reader :owners, :reflection def initialize(klass, owners, reflection, preload_scope) @owners = owners + @reflection = reflection end def run(preloader); end - def loaded?; false; end + + def preloaded_records + owners.flat_map { |owner| owner.read_attribute reflection.name } + end end def preloader_for(reflection, owners) diff --git a/activerecord/lib/active_record/associations/preloader/association.rb b/activerecord/lib/active_record/associations/preloader/association.rb index cdf4a27341..d02384e221 100644 --- a/activerecord/lib/active_record/associations/preloader/association.rb +++ b/activerecord/lib/active_record/associations/preloader/association.rb @@ -17,7 +17,6 @@ module ActiveRecord @owners_by_key = nil @type_caster = IDENTITY_CASTER @associated_records_by_owner = nil - @loaded = false end def run(preloader) @@ -74,15 +73,9 @@ module ActiveRecord @associated_records_by_owner.values.flatten end - def loaded? - @loaded - end - private def associated_records_by_owner(preloader) - @loaded = true - return @associated_records_by_owner if @associated_records_by_owner owners_map = owners_by_key diff --git a/activerecord/lib/active_record/associations/preloader/through_association.rb b/activerecord/lib/active_record/associations/preloader/through_association.rb index 0c0501fd58..15fdba51f3 100644 --- a/activerecord/lib/active_record/associations/preloader/through_association.rb +++ b/activerecord/lib/active_record/associations/preloader/through_association.rb @@ -12,8 +12,6 @@ module ActiveRecord end def associated_records_by_owner(preloader) - @loaded = true - return @associated_records_by_owner if @associated_records_by_owner preloader.preload(owners, @@ -54,7 +52,7 @@ module ActiveRecord r.send(source_reflection.name) }.compact - if pl && pl.loaded? + if pl loaded_records = pl.preloaded_records i = 0 record_index = loaded_records.each_with_object({}) { |r,indexes| |