jit_preloader 0.3.0 → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +5 -5
 - data/jit_preloader.gemspec +1 -1
 - data/lib/jit_preloader/active_record/associations/preloader/ar5_association.rb +6 -13
 - data/lib/jit_preloader/active_record/associations/preloader/ar6_association.rb +16 -30
 - data/lib/jit_preloader/active_record/base.rb +47 -25
 - data/lib/jit_preloader/version.rb +1 -1
 - metadata +5 -6
 
    
        checksums.yaml
    CHANGED
    
    | 
         @@ -1,7 +1,7 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            ---
         
     | 
| 
       2 
     | 
    
         
            -
             
     | 
| 
       3 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       4 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 2 
     | 
    
         
            +
            SHA256:
         
     | 
| 
      
 3 
     | 
    
         
            +
              metadata.gz: 9a3ebb3488bb221f516f32ebd2fbbedcee866b57d3ccc3c171f3843b9cc0af45
         
     | 
| 
      
 4 
     | 
    
         
            +
              data.tar.gz: 7bcaa0e57a6ee5541ca0fac7013f4188113f3425675df4ad29cab7cba3c8a540
         
     | 
| 
       5 
5 
     | 
    
         
             
            SHA512:
         
     | 
| 
       6 
     | 
    
         
            -
              metadata.gz:  
     | 
| 
       7 
     | 
    
         
            -
              data.tar.gz:  
     | 
| 
      
 6 
     | 
    
         
            +
              metadata.gz: 7d696d0f3465be1aa319ae86c335e087b967847fefcd49a926e27c1cc24681be824ecc2a95a6365a129ae668f2abb341d570841f242cbb4b47295f974a3bf71e
         
     | 
| 
      
 7 
     | 
    
         
            +
              data.tar.gz: 5ac6f866cc9ee0fbab43e36d8bd2134e42782e6a6e9e4e635bd9b15e5951a02ed2d1d86b003afdbf5aeac736ffd3a23aadc7e9f75db1679cfd0768e132e2d5b4
         
     | 
    
        data/jit_preloader.gemspec
    CHANGED
    
    | 
         @@ -18,7 +18,7 @@ Gem::Specification.new do |spec| 
     | 
|
| 
       18 
18 
     | 
    
         
             
              spec.test_files    = spec.files.grep(%r{^(test|spec|features)/})
         
     | 
| 
       19 
19 
     | 
    
         
             
              spec.require_paths = ["lib"]
         
     | 
| 
       20 
20 
     | 
    
         | 
| 
       21 
     | 
    
         
            -
              spec.add_dependency "activerecord", ">  
     | 
| 
      
 21 
     | 
    
         
            +
              spec.add_dependency "activerecord", "> 5.0", "< 7"
         
     | 
| 
       22 
22 
     | 
    
         
             
              spec.add_dependency "activesupport"
         
     | 
| 
       23 
23 
     | 
    
         | 
| 
       24 
24 
     | 
    
         
             
              spec.add_development_dependency "bundler"
         
     | 
| 
         @@ -18,20 +18,11 @@ module JitPreloader 
     | 
|
| 
       18 
18 
     | 
    
         
             
                # end
         
     | 
| 
       19 
19 
     | 
    
         | 
| 
       20 
20 
     | 
    
         
             
                def run(preloader)
         
     | 
| 
       21 
     | 
    
         
            -
                   
     | 
| 
       22 
     | 
    
         
            -
             
     | 
| 
       23 
     | 
    
         
            -
             
     | 
| 
       24 
     | 
    
         
            -
                     
     | 
| 
       25 
     | 
    
         
            -
                    association.set_inverse_instance(record)
         
     | 
| 
      
 21 
     | 
    
         
            +
                  super.tap do
         
     | 
| 
      
 22 
     | 
    
         
            +
                    if preloaded_records.any? && preloaded_records.none?(&:jit_preloader)
         
     | 
| 
      
 23 
     | 
    
         
            +
                      JitPreloader::Preloader.attach(preloaded_records) if owners.any?(&:jit_preloader) || JitPreloader.globally_enabled?
         
     | 
| 
      
 24 
     | 
    
         
            +
                    end
         
     | 
| 
       26 
25 
     | 
    
         
             
                  end
         
     | 
| 
       27 
     | 
    
         
            -
             
     | 
| 
       28 
     | 
    
         
            -
                  owners.each do |owner|
         
     | 
| 
       29 
     | 
    
         
            -
                    owned_records = records[convert_key(owner[owner_key_name])] || []
         
     | 
| 
       30 
     | 
    
         
            -
                    all_records.concat(Array(owned_records)) if owner.jit_preloader || JitPreloader.globally_enabled?
         
     | 
| 
       31 
     | 
    
         
            -
                    associate_records_to_owner(owner, owned_records)
         
     | 
| 
       32 
     | 
    
         
            -
                  end
         
     | 
| 
       33 
     | 
    
         
            -
             
     | 
| 
       34 
     | 
    
         
            -
                  JitPreloader::Preloader.attach(all_records) if all_records.any?
         
     | 
| 
       35 
26 
     | 
    
         
             
                end
         
     | 
| 
       36 
27 
     | 
    
         | 
| 
       37 
28 
     | 
    
         
             
                # Original method:
         
     | 
| 
         @@ -54,6 +45,7 @@ module JitPreloader 
     | 
|
| 
       54 
45 
     | 
    
         
             
                    # the original copy so that we don't blow away in-memory changes.
         
     | 
| 
       55 
46 
     | 
    
         
             
                    new_records = association.target.any? ? records - association.target : records
         
     | 
| 
       56 
47 
     | 
    
         
             
                    association.target.concat(new_records)
         
     | 
| 
      
 48 
     | 
    
         
            +
                    association.loaded!
         
     | 
| 
       57 
49 
     | 
    
         
             
                  else
         
     | 
| 
       58 
50 
     | 
    
         
             
                    association.target ||= records.first unless records.empty?
         
     | 
| 
       59 
51 
     | 
    
         
             
                  end
         
     | 
| 
         @@ -69,3 +61,4 @@ module JitPreloader 
     | 
|
| 
       69 
61 
     | 
    
         
             
            end
         
     | 
| 
       70 
62 
     | 
    
         | 
| 
       71 
63 
     | 
    
         
             
            ActiveRecord::Associations::Preloader::Association.prepend(JitPreloader::PreloaderAssociation)
         
     | 
| 
      
 64 
     | 
    
         
            +
            ActiveRecord::Associations::Preloader::ThroughAssociation.prepend(JitPreloader::PreloaderAssociation)
         
     | 
| 
         @@ -6,58 +6,43 @@ module JitPreloader 
     | 
|
| 
       6 
6 
     | 
    
         
             
                # part of the target, then attach all of the records to a new jit preloader.
         
     | 
| 
       7 
7 
     | 
    
         
             
                #
         
     | 
| 
       8 
8 
     | 
    
         
             
                # def run
         
     | 
| 
       9 
     | 
    
         
            -
                #    
     | 
| 
       10 
     | 
    
         
            -
             
     | 
| 
       11 
     | 
    
         
            -
                # 
     | 
| 
       12 
     | 
    
         
            -
                #      
     | 
| 
       13 
     | 
    
         
            -
                #    
     | 
| 
       14 
     | 
    
         
            -
             
     | 
| 
       15 
     | 
    
         
            -
                #     # the association can not be marked as loaded
         
     | 
| 
       16 
     | 
    
         
            -
                #     # Loading into a Hash instead
         
     | 
| 
       17 
     | 
    
         
            -
                #     records_by_owner
         
     | 
| 
       18 
     | 
    
         
            -
                #   end
         
     | 
| 
      
 9 
     | 
    
         
            +
                #   records = records_by_owner
         
     | 
| 
      
 10 
     | 
    
         
            +
             
     | 
| 
      
 11 
     | 
    
         
            +
                #   owners.each do |owner|
         
     | 
| 
      
 12 
     | 
    
         
            +
                #     associate_records_to_owner(owner, records[owner] || [])
         
     | 
| 
      
 13 
     | 
    
         
            +
                #   end if @associate
         
     | 
| 
      
 14 
     | 
    
         
            +
             
     | 
| 
       19 
15 
     | 
    
         
             
                #   self
         
     | 
| 
       20 
16 
     | 
    
         
             
                # end
         
     | 
| 
       21 
     | 
    
         
            -
                def run
         
     | 
| 
       22 
     | 
    
         
            -
                  all_records = []
         
     | 
| 
       23 
17 
     | 
    
         | 
| 
       24 
     | 
    
         
            -
             
     | 
| 
       25 
     | 
    
         
            -
             
     | 
| 
       26 
     | 
    
         
            -
                     
     | 
| 
       27 
     | 
    
         
            -
             
     | 
| 
      
 18 
     | 
    
         
            +
                def run
         
     | 
| 
      
 19 
     | 
    
         
            +
                  super.tap do
         
     | 
| 
      
 20 
     | 
    
         
            +
                    if preloaded_records.any? && preloaded_records.none?(&:jit_preloader)
         
     | 
| 
      
 21 
     | 
    
         
            +
                      JitPreloader::Preloader.attach(preloaded_records) if owners.any?(&:jit_preloader) || JitPreloader.globally_enabled?
         
     | 
| 
      
 22 
     | 
    
         
            +
                    end
         
     | 
| 
       28 
23 
     | 
    
         
             
                  end
         
     | 
| 
       29 
     | 
    
         
            -
             
     | 
| 
       30 
     | 
    
         
            -
                  JitPreloader::Preloader.attach(all_records) if all_records.any?
         
     | 
| 
       31 
     | 
    
         
            -
             
     | 
| 
       32 
     | 
    
         
            -
                  self
         
     | 
| 
       33 
24 
     | 
    
         
             
                end
         
     | 
| 
       34 
25 
     | 
    
         | 
| 
       35 
26 
     | 
    
         
             
                # Original method:
         
     | 
| 
       36 
27 
     | 
    
         
             
                # def associate_records_to_owner(owner, records)
         
     | 
| 
       37 
28 
     | 
    
         
             
                #   association = owner.association(reflection.name)
         
     | 
| 
       38 
     | 
    
         
            -
                #   association.loaded!
         
     | 
| 
       39 
29 
     | 
    
         
             
                #   if reflection.collection?
         
     | 
| 
       40 
     | 
    
         
            -
                #     association.target 
     | 
| 
      
 30 
     | 
    
         
            +
                #     association.target = records
         
     | 
| 
       41 
31 
     | 
    
         
             
                #   else
         
     | 
| 
       42 
     | 
    
         
            -
                #     association.target = records.first 
     | 
| 
      
 32 
     | 
    
         
            +
                #     association.target = records.first
         
     | 
| 
       43 
33 
     | 
    
         
             
                #   end
         
     | 
| 
       44 
34 
     | 
    
         
             
                # end
         
     | 
| 
       45 
35 
     | 
    
         
             
                def associate_records_to_owner(owner, records)
         
     | 
| 
       46 
36 
     | 
    
         
             
                  association = owner.association(reflection.name)
         
     | 
| 
       47 
     | 
    
         
            -
                  association.loaded!
         
     | 
| 
       48 
     | 
    
         
            -
             
     | 
| 
       49 
37 
     | 
    
         
             
                  if reflection.collection?
         
     | 
| 
       50 
     | 
    
         
            -
                    # It is possible that some of the records are loaded already.
         
     | 
| 
       51 
     | 
    
         
            -
                    # We don't want to duplicate them, but we also want to preserve
         
     | 
| 
       52 
     | 
    
         
            -
                    # the original copy so that we don't blow away in-memory changes.
         
     | 
| 
       53 
38 
     | 
    
         
             
                    new_records = association.target.any? ? records - association.target : records
         
     | 
| 
       54 
39 
     | 
    
         
             
                    association.target.concat(new_records)
         
     | 
| 
      
 40 
     | 
    
         
            +
                    association.loaded!
         
     | 
| 
       55 
41 
     | 
    
         
             
                  else
         
     | 
| 
       56 
     | 
    
         
            -
                    association.target  
     | 
| 
      
 42 
     | 
    
         
            +
                    association.target = records.first
         
     | 
| 
       57 
43 
     | 
    
         
             
                  end
         
     | 
| 
       58 
44 
     | 
    
         
             
                end
         
     | 
| 
       59 
45 
     | 
    
         | 
| 
       60 
     | 
    
         
            -
             
     | 
| 
       61 
46 
     | 
    
         
             
                def build_scope
         
     | 
| 
       62 
47 
     | 
    
         
             
                  super.tap do |scope|
         
     | 
| 
       63 
48 
     | 
    
         
             
                    scope.jit_preload! if owners.any?(&:jit_preloader) || JitPreloader.globally_enabled?
         
     | 
| 
         @@ -67,3 +52,4 @@ module JitPreloader 
     | 
|
| 
       67 
52 
     | 
    
         
             
            end
         
     | 
| 
       68 
53 
     | 
    
         | 
| 
       69 
54 
     | 
    
         
             
            ActiveRecord::Associations::Preloader::Association.prepend(JitPreloader::PreloaderAssociation)
         
     | 
| 
      
 55 
     | 
    
         
            +
            ActiveRecord::Associations::Preloader::ThroughAssociation.prepend(JitPreloader::PreloaderAssociation)
         
     | 
| 
         @@ -18,37 +18,59 @@ module JitPreloadExtension 
     | 
|
| 
       18 
18 
     | 
    
         
             
                end
         
     | 
| 
       19 
19 
     | 
    
         
             
              end
         
     | 
| 
       20 
20 
     | 
    
         | 
| 
       21 
     | 
    
         
            -
               
     | 
| 
       22 
     | 
    
         
            -
                 
     | 
| 
      
 21 
     | 
    
         
            +
              if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new("6.0.0")
         
     | 
| 
      
 22 
     | 
    
         
            +
                def preload_scoped_relation(name:, base_association:, preload_scope: nil)
         
     | 
| 
      
 23 
     | 
    
         
            +
                  return jit_preload_scoped_relations[name] if jit_preload_scoped_relations&.key?(name)
         
     | 
| 
      
 24 
     | 
    
         
            +
             
     | 
| 
      
 25 
     | 
    
         
            +
                  records = jit_preloader&.records || [self]
         
     | 
| 
      
 26 
     | 
    
         
            +
             
     | 
| 
      
 27 
     | 
    
         
            +
                  preloader_association = ActiveRecord::Associations::Preloader.new.preload(
         
     | 
| 
      
 28 
     | 
    
         
            +
                    records,
         
     | 
| 
      
 29 
     | 
    
         
            +
                    base_association,
         
     | 
| 
      
 30 
     | 
    
         
            +
                    preload_scope
         
     | 
| 
      
 31 
     | 
    
         
            +
                  ).first
         
     | 
| 
      
 32 
     | 
    
         
            +
             
     | 
| 
      
 33 
     | 
    
         
            +
                  records.each do |record|
         
     | 
| 
      
 34 
     | 
    
         
            +
                    record.jit_preload_scoped_relations ||= {}
         
     | 
| 
      
 35 
     | 
    
         
            +
                    association = record.association(base_association)
         
     | 
| 
      
 36 
     | 
    
         
            +
                    record.jit_preload_scoped_relations[name] = preloader_association.records_by_owner[record]
         
     | 
| 
      
 37 
     | 
    
         
            +
                  end
         
     | 
| 
      
 38 
     | 
    
         
            +
             
     | 
| 
      
 39 
     | 
    
         
            +
                  jit_preload_scoped_relations[name]
         
     | 
| 
      
 40 
     | 
    
         
            +
                end
         
     | 
| 
      
 41 
     | 
    
         
            +
              else
         
     | 
| 
      
 42 
     | 
    
         
            +
                def preload_scoped_relation(name:, base_association:, preload_scope: nil)
         
     | 
| 
      
 43 
     | 
    
         
            +
                  return jit_preload_scoped_relations[name] if jit_preload_scoped_relations&.key?(name)
         
     | 
| 
      
 44 
     | 
    
         
            +
             
     | 
| 
      
 45 
     | 
    
         
            +
                  records = jit_preloader&.records || [self]
         
     | 
| 
      
 46 
     | 
    
         
            +
                  previous_association_values = {}
         
     | 
| 
      
 47 
     | 
    
         
            +
             
     | 
| 
      
 48 
     | 
    
         
            +
                  records.each do |record|
         
     | 
| 
      
 49 
     | 
    
         
            +
                    association = record.association(base_association)
         
     | 
| 
      
 50 
     | 
    
         
            +
                    if association.loaded?
         
     | 
| 
      
 51 
     | 
    
         
            +
                      previous_association_values[record] = association.target
         
     | 
| 
      
 52 
     | 
    
         
            +
                      association.reset
         
     | 
| 
      
 53 
     | 
    
         
            +
                    end
         
     | 
| 
      
 54 
     | 
    
         
            +
                  end
         
     | 
| 
       23 
55 
     | 
    
         | 
| 
       24 
     | 
    
         
            -
             
     | 
| 
       25 
     | 
    
         
            -
             
     | 
| 
      
 56 
     | 
    
         
            +
                  ActiveRecord::Associations::Preloader.new.preload(
         
     | 
| 
      
 57 
     | 
    
         
            +
                    records,
         
     | 
| 
      
 58 
     | 
    
         
            +
                    base_association,
         
     | 
| 
      
 59 
     | 
    
         
            +
                    preload_scope
         
     | 
| 
      
 60 
     | 
    
         
            +
                  )
         
     | 
| 
       26 
61 
     | 
    
         | 
| 
       27 
     | 
    
         
            -
             
     | 
| 
       28 
     | 
    
         
            -
             
     | 
| 
       29 
     | 
    
         
            -
             
     | 
| 
       30 
     | 
    
         
            -
                     
     | 
| 
      
 62 
     | 
    
         
            +
                  records.each do |record|
         
     | 
| 
      
 63 
     | 
    
         
            +
                    record.jit_preload_scoped_relations ||= {}
         
     | 
| 
      
 64 
     | 
    
         
            +
                    association = record.association(base_association)
         
     | 
| 
      
 65 
     | 
    
         
            +
                    record.jit_preload_scoped_relations[name] = association.target
         
     | 
| 
       31 
66 
     | 
    
         
             
                    association.reset
         
     | 
| 
      
 67 
     | 
    
         
            +
                    if previous_association_values.key?(record)
         
     | 
| 
      
 68 
     | 
    
         
            +
                      association.target = previous_association_values[record]
         
     | 
| 
      
 69 
     | 
    
         
            +
                    end
         
     | 
| 
       32 
70 
     | 
    
         
             
                  end
         
     | 
| 
       33 
     | 
    
         
            -
                end
         
     | 
| 
       34 
71 
     | 
    
         | 
| 
       35 
     | 
    
         
            -
             
     | 
| 
       36 
     | 
    
         
            -
                  records,
         
     | 
| 
       37 
     | 
    
         
            -
                  base_association,
         
     | 
| 
       38 
     | 
    
         
            -
                  preload_scope
         
     | 
| 
       39 
     | 
    
         
            -
                )
         
     | 
| 
       40 
     | 
    
         
            -
             
     | 
| 
       41 
     | 
    
         
            -
                records.each do |record|
         
     | 
| 
       42 
     | 
    
         
            -
                  record.jit_preload_scoped_relations ||= {}
         
     | 
| 
       43 
     | 
    
         
            -
                  association = record.association(base_association)
         
     | 
| 
       44 
     | 
    
         
            -
                  record.jit_preload_scoped_relations[name] = association.target
         
     | 
| 
       45 
     | 
    
         
            -
                  association.reset
         
     | 
| 
       46 
     | 
    
         
            -
                  if previous_association_values.key?(record)
         
     | 
| 
       47 
     | 
    
         
            -
                    association.target = previous_association_values[record]
         
     | 
| 
       48 
     | 
    
         
            -
                  end
         
     | 
| 
      
 72 
     | 
    
         
            +
                  jit_preload_scoped_relations[name]
         
     | 
| 
       49 
73 
     | 
    
         
             
                end
         
     | 
| 
       50 
     | 
    
         
            -
             
     | 
| 
       51 
     | 
    
         
            -
                jit_preload_scoped_relations[name]
         
     | 
| 
       52 
74 
     | 
    
         
             
              end
         
     | 
| 
       53 
75 
     | 
    
         | 
| 
       54 
76 
     | 
    
         
             
              def self.prepended(base)
         
     | 
    
        metadata
    CHANGED
    
    | 
         @@ -1,14 +1,14 @@ 
     | 
|
| 
       1 
1 
     | 
    
         
             
            --- !ruby/object:Gem::Specification
         
     | 
| 
       2 
2 
     | 
    
         
             
            name: jit_preloader
         
     | 
| 
       3 
3 
     | 
    
         
             
            version: !ruby/object:Gem::Version
         
     | 
| 
       4 
     | 
    
         
            -
              version: 0. 
     | 
| 
      
 4 
     | 
    
         
            +
              version: 1.0.0
         
     | 
| 
       5 
5 
     | 
    
         
             
            platform: ruby
         
     | 
| 
       6 
6 
     | 
    
         
             
            authors:
         
     | 
| 
       7 
7 
     | 
    
         
             
            - Kyle d'Oliveira
         
     | 
| 
       8 
8 
     | 
    
         
             
            autorequire: 
         
     | 
| 
       9 
9 
     | 
    
         
             
            bindir: bin
         
     | 
| 
       10 
10 
     | 
    
         
             
            cert_chain: []
         
     | 
| 
       11 
     | 
    
         
            -
            date: 2020- 
     | 
| 
      
 11 
     | 
    
         
            +
            date: 2020-11-04 00:00:00.000000000 Z
         
     | 
| 
       12 
12 
     | 
    
         
             
            dependencies:
         
     | 
| 
       13 
13 
     | 
    
         
             
            - !ruby/object:Gem::Dependency
         
     | 
| 
       14 
14 
     | 
    
         
             
              name: activerecord
         
     | 
| 
         @@ -16,7 +16,7 @@ dependencies: 
     | 
|
| 
       16 
16 
     | 
    
         
             
                requirements:
         
     | 
| 
       17 
17 
     | 
    
         
             
                - - ">"
         
     | 
| 
       18 
18 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       19 
     | 
    
         
            -
                    version: ' 
     | 
| 
      
 19 
     | 
    
         
            +
                    version: '5.0'
         
     | 
| 
       20 
20 
     | 
    
         
             
                - - "<"
         
     | 
| 
       21 
21 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       22 
22 
     | 
    
         
             
                    version: '7'
         
     | 
| 
         @@ -26,7 +26,7 @@ dependencies: 
     | 
|
| 
       26 
26 
     | 
    
         
             
                requirements:
         
     | 
| 
       27 
27 
     | 
    
         
             
                - - ">"
         
     | 
| 
       28 
28 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       29 
     | 
    
         
            -
                    version: ' 
     | 
| 
      
 29 
     | 
    
         
            +
                    version: '5.0'
         
     | 
| 
       30 
30 
     | 
    
         
             
                - - "<"
         
     | 
| 
       31 
31 
     | 
    
         
             
                  - !ruby/object:Gem::Version
         
     | 
| 
       32 
32 
     | 
    
         
             
                    version: '7'
         
     | 
| 
         @@ -193,8 +193,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement 
     | 
|
| 
       193 
193 
     | 
    
         
             
                - !ruby/object:Gem::Version
         
     | 
| 
       194 
194 
     | 
    
         
             
                  version: '0'
         
     | 
| 
       195 
195 
     | 
    
         
             
            requirements: []
         
     | 
| 
       196 
     | 
    
         
            -
             
     | 
| 
       197 
     | 
    
         
            -
            rubygems_version: 2.6.14
         
     | 
| 
      
 196 
     | 
    
         
            +
            rubygems_version: 3.1.4
         
     | 
| 
       198 
197 
     | 
    
         
             
            signing_key: 
         
     | 
| 
       199 
198 
     | 
    
         
             
            specification_version: 4
         
     | 
| 
       200 
199 
     | 
    
         
             
            summary: Tool to understand N+1 queries and to remove them
         
     |