associationist 0.1.5 → 0.1.9
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/lib/associationist/associations/collection_association.rb +8 -0
- data/lib/associationist/associations/preloader/singular_association.rb +102 -14
- data/lib/associationist/associations/singular_association.rb +5 -1
- data/lib/associationist/builder/singular_association.rb +8 -0
- data/lib/associationist/config.rb +4 -0
- data/lib/associationist/reflection/collection_reflection.rb +1 -1
- data/lib/associationist/reflection/singular_reflection.rb +1 -1
- data/lib/associationist/version.rb +1 -1
- data/lib/associationist.rb +8 -2
- metadata +7 -7
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 187820657e6aa46774e5161d105e4ae1e939d062ec389be51a9e510b5eda7aed
|
4
|
+
data.tar.gz: 57b2cdc4270aaf9b75a3b52957ce272e8c4492087079c20b5771208b8a2a9fe0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 938dd23506837a38292a3ae074a72a2b40815ddae10f0f946e14e00514b8cd601baa006b4589f580406aad73ff7ed8065e0d710544ba90c3847c63e4916cf30e
|
7
|
+
data.tar.gz: 830e60e54c9c865ff97c5b6d57b92270b67625093a3c28828e6d89f9abb44cbd2565080c9b8fcc3bdd967cd4e9cbce1b0307728e6ee9000844777c7af808c297
|
@@ -27,6 +27,14 @@ module Associationist
|
|
27
27
|
true
|
28
28
|
end
|
29
29
|
end
|
30
|
+
|
31
|
+
def self.define_writers(mixin, name)
|
32
|
+
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
33
|
+
def #{name}=(value)
|
34
|
+
raise "Virtual associations are read-only."
|
35
|
+
end
|
36
|
+
CODE
|
37
|
+
end
|
30
38
|
end
|
31
39
|
end
|
32
40
|
end
|
@@ -2,13 +2,18 @@ module Associationist
|
|
2
2
|
module Associations
|
3
3
|
module Preloader
|
4
4
|
class SingularAssociation
|
5
|
-
|
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|
|
@@ -36,15 +41,43 @@ module Associationist
|
|
36
41
|
def preloaded_records
|
37
42
|
@owners.flat_map { |owner| owner.association(@reflection.name).target }
|
38
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
|
72
|
+
end
|
39
73
|
end
|
40
74
|
|
41
75
|
end
|
42
76
|
end
|
43
77
|
|
44
|
-
|
45
|
-
|
46
|
-
|
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
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
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
|
-
|
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
|
@@ -34,6 +34,14 @@ module Associationist
|
|
34
34
|
Reflection::SingularReflection.new(name, scope, options, model)
|
35
35
|
end
|
36
36
|
|
37
|
+
def self.define_writers(mixin, name)
|
38
|
+
mixin.class_eval <<-CODE, __FILE__, __LINE__ + 1
|
39
|
+
def #{name}=(value)
|
40
|
+
raise "Virtual associations are read-only."
|
41
|
+
end
|
42
|
+
CODE
|
43
|
+
end
|
44
|
+
|
37
45
|
end
|
38
46
|
end
|
39
47
|
end
|
data/lib/associationist.rb
CHANGED
@@ -5,7 +5,13 @@ Dir.glob("#{File.dirname(__FILE__)}/**/*.rb").each do |file|
|
|
5
5
|
end
|
6
6
|
|
7
7
|
module Associationist
|
8
|
-
|
9
|
-
|
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.
|
4
|
+
version: 0.1.9
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
|
-
-
|
7
|
+
- onyxblade
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date:
|
11
|
+
date: 2021-12-19 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: '
|
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: '
|
32
|
+
version: '7.1'
|
33
33
|
- !ruby/object:Gem::Dependency
|
34
34
|
name: pry
|
35
35
|
requirement: !ruby/object:Gem::Requirement
|
@@ -104,7 +104,7 @@ files:
|
|
104
104
|
- lib/associationist/reflection/collection_reflection.rb
|
105
105
|
- lib/associationist/reflection/singular_reflection.rb
|
106
106
|
- lib/associationist/version.rb
|
107
|
-
homepage: https://github.com/
|
107
|
+
homepage: https://github.com/onyxblade/associationist
|
108
108
|
licenses:
|
109
109
|
- MIT
|
110
110
|
metadata: {}
|
@@ -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.
|
126
|
+
rubygems_version: 3.2.15
|
127
127
|
signing_key:
|
128
128
|
specification_version: 4
|
129
129
|
summary: ''
|