associationist 0.1.8 → 0.2.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 8685abcebf828aabc6f3337bfcbf4baac04c73dc847b5a235933c5b2e3e8e487
4
- data.tar.gz: 21983405667e166041b1ad1670cdf399813d83579c4408ba0aa0c9e3d5dd8359
3
+ metadata.gz: bbd3506c4a0c0b1c488c490432f4c247ec5906b00ab41ccb0b45b83faad36344
4
+ data.tar.gz: 90f64a090cd9e3dad4db7dd270ad6bd4c743644e65515e7373dc0879617ea060
5
5
  SHA512:
6
- metadata.gz: 8d77f497b70cf83ff9912af6cd80c3608de1593461fe0c1f30dbcfe459fe17fd118b94383ca78a37d38fbab5d5e1d2eb697fbbb8f0938e88b6ac7b78f62b180e
7
- data.tar.gz: 71f89b2f935278cf9d044152d6fa92895af98e05a9f49523d0031ab39d0dca1d81f7546bfdbbf338c124c187d94100c10f6981e18451acb317e09c2063465f4e
6
+ metadata.gz: a99ccc641e5ab5e551763e2294f5dc45f497fcfa328a82d0c2a43591642cfd7f1e385aa2ca5c1e41bdc54fab7a2886fe8f0a2cb854ccce177d4da422f69a3d49
7
+ data.tar.gz: 97d4a871f91aade776e983fc6b09882f902dae9279d15cfdb2b55f63ae9aef4fc6572a34bdc763892b48937fa383f6c95683e17d37fd3eb357fa85a0e6db4e1f
@@ -2,13 +2,18 @@ module Associationist
2
2
  module Associations
3
3
  module Preloader
4
4
  class SingularAssociation
5
- def initialize klass, owners, reflection, preload_scope, associate_by_default = true
5
+ attr_reader :klass
6
+ def initialize klass, owners, reflection, preload_scope, reflection_scope = nil, associate_by_default = true
7
+ @klass = klass
6
8
  @owners = owners
7
9
  @reflection = reflection
10
+ @run = false
8
11
  end
9
12
 
10
13
  # handle >= 6.0
11
14
  def run preloader = nil
15
+ return self if @run
16
+ @run = true
12
17
  case
13
18
  when @reflection.config.preloader_proc
14
19
  @reflection.config.preloader_proc.call(@owners).each do |record, value|
@@ -34,17 +39,45 @@ module Associationist
34
39
  end
35
40
 
36
41
  def preloaded_records
37
- @owners.flat_map { |owner| owner.association(@reflection.name).target }
42
+ @owners.flat_map { |owner| owner.association(@reflection.name).target }.compact
43
+ end
44
+
45
+ # handle >=7.0
46
+ def runnable_loaders
47
+ [self]
48
+ end
49
+
50
+ def run?
51
+ @run
52
+ end
53
+
54
+ def future_classes
55
+ if run?
56
+ []
57
+ else
58
+ if @klass <= ActiveRecord::Base
59
+ [@klass]
60
+ else
61
+ []
62
+ end
63
+ end
64
+ end
65
+
66
+ def table_name
67
+ if @klass <= ActiveRecord::Base
68
+ @klass.table_name
69
+ else
70
+ nil
71
+ end
38
72
  end
39
73
  end
40
74
 
41
75
  end
42
76
  end
43
77
 
44
- module ActiveRecordPreloaderPatch
45
- case
46
- when ActiveRecord.version < Gem::Version.new('5.2.0')
47
- def preloader_for(reflection, owners, rhs_klass)
78
+ if ActiveRecord.version >= Gem::Version.new('7.0.0')
79
+ module ActiveRecordPreloaderBranchPatch
80
+ def preloader_for(reflection)
48
81
  config = reflection.options[:associationist]
49
82
  if config
50
83
  Associationist::Associations::Preloader::SingularAssociation
@@ -52,17 +85,72 @@ module Associationist
52
85
  super
53
86
  end
54
87
  end
55
- when ActiveRecord.version >= Gem::Version.new('5.2.0')
56
- def preloader_for(reflection, owners)
57
- config = reflection.options[:associationist]
58
- if config
59
- Associationist::Associations::Preloader::SingularAssociation
60
- else
61
- super
88
+ end
89
+
90
+ ActiveRecord::Associations::Preloader::Branch.prepend ActiveRecordPreloaderBranchPatch
91
+
92
+ module ActiveRecordPreloaderPatch
93
+ end
94
+
95
+ ActiveRecord::Associations::Preloader.prepend ActiveRecordPreloaderPatch
96
+
97
+ module ActiveRecordPreloaderBatchPatch
98
+ def call
99
+ branches = @preloaders.flat_map(&:branches)
100
+ until branches.empty?
101
+ loaders = branches.flat_map(&:runnable_loaders)
102
+ loaders.each do |loader|
103
+ if loader.is_a? Associationist::Associations::Preloader::SingularAssociation
104
+ else
105
+ loader.associate_records_from_unscoped(@available_records[loader.klass.base_class])
106
+ end
107
+ end
108
+
109
+ if loaders.any?
110
+ future_tables = branches.flat_map do |branch|
111
+ branch.future_classes - branch.runnable_loaders.map(&:klass)
112
+ end.map(&:table_name).uniq
113
+
114
+ target_loaders = loaders.reject { |l| future_tables.include?(l.table_name) }
115
+ target_loaders = loaders if target_loaders.empty?
116
+
117
+ non_associationist_loaders = target_loaders.reject{|x| x.is_a? Associationist::Associations::Preloader::SingularAssociation}
118
+ group_and_load_similar(non_associationist_loaders)
119
+ target_loaders.each(&:run)
120
+ end
121
+
122
+ finished, in_progress = branches.partition(&:done?)
123
+
124
+ branches = in_progress + finished.flat_map(&:children)
62
125
  end
63
126
  end
64
127
  end
65
- end
66
128
 
67
- ActiveRecord::Associations::Preloader.prepend ActiveRecordPreloaderPatch
129
+ ActiveRecord::Associations::Preloader::Batch.prepend ActiveRecordPreloaderBatchPatch
130
+ else
131
+ module ActiveRecordPreloaderPatch
132
+ case
133
+ when ActiveRecord.version < Gem::Version.new('5.2.0')
134
+ def preloader_for(reflection, owners, rhs_klass)
135
+ config = reflection.options[:associationist]
136
+ if config
137
+ Associationist::Associations::Preloader::SingularAssociation
138
+ else
139
+ super
140
+ end
141
+ end
142
+ when ActiveRecord.version >= Gem::Version.new('5.2.0')
143
+ def preloader_for(reflection, owners)
144
+ config = reflection.options[:associationist]
145
+ if config
146
+ Associationist::Associations::Preloader::SingularAssociation
147
+ else
148
+ super
149
+ end
150
+ end
151
+ end
152
+ end
153
+
154
+ ActiveRecord::Associations::Preloader.prepend ActiveRecordPreloaderPatch
155
+ end
68
156
  end
@@ -25,7 +25,11 @@ module Associationist
25
25
  if reflection.config.scope_proc
26
26
  association_scope.klass
27
27
  else
28
- Object
28
+ if reflection.config.class_name
29
+ reflection.config.class_name.constantize
30
+ else
31
+ Object
32
+ end
29
33
  end
30
34
  end
31
35
 
@@ -30,6 +30,10 @@ module Associationist
30
30
  @config[:scope]
31
31
  end
32
32
 
33
+ def class_name
34
+ @config[:class_name]
35
+ end
36
+
33
37
  def check
34
38
  if @config[:loader] && @config[:scope]
35
39
  raise "cannot define both loader and scope"
@@ -1,3 +1,3 @@
1
1
  module Associationist
2
- VERSION = '0.1.8'
2
+ VERSION = '0.2.0'
3
3
  end
@@ -5,7 +5,13 @@ Dir.glob("#{File.dirname(__FILE__)}/**/*.rb").each do |file|
5
5
  end
6
6
 
7
7
  module Associationist
8
- def self.preload records, associations
9
- ActiveRecord::Associations::Preloader.new.preload records, associations
8
+ if ActiveRecord.version >= Gem::Version.new('7.0.0')
9
+ def self.preload records, associations
10
+ ActiveRecord::Associations::Preloader.new(records: records, associations: associations).call
11
+ end
12
+ else
13
+ def self.preload records, associations
14
+ ActiveRecord::Associations::Preloader.new.preload records, associations
15
+ end
10
16
  end
11
17
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: associationist
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.8
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - onyxblade
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2021-07-28 00:00:00.000000000 Z
11
+ date: 2023-02-23 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord
@@ -19,7 +19,7 @@ dependencies:
19
19
  version: '5.0'
20
20
  - - "<"
21
21
  - !ruby/object:Gem::Version
22
- version: '6.2'
22
+ version: '7.1'
23
23
  type: :runtime
24
24
  prerelease: false
25
25
  version_requirements: !ruby/object:Gem::Requirement
@@ -29,7 +29,7 @@ dependencies:
29
29
  version: '5.0'
30
30
  - - "<"
31
31
  - !ruby/object:Gem::Version
32
- version: '6.2'
32
+ version: '7.1'
33
33
  - !ruby/object:Gem::Dependency
34
34
  name: pry
35
35
  requirement: !ruby/object:Gem::Requirement
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
123
123
  - !ruby/object:Gem::Version
124
124
  version: '0'
125
125
  requirements: []
126
- rubygems_version: 3.2.15
126
+ rubygems_version: 3.2.33
127
127
  signing_key:
128
128
  specification_version: 4
129
129
  summary: ''