occams-record 0.33.0 → 0.34.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
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 2b133d571ce7f9e293908fe1a19e7b835032daa6df97c8b24ecd5a62aba90d59
|
|
4
|
+
data.tar.gz: db6b2825f6a449be9fa4354d062c45b7705a00a99faefe3ec3b6549e852f59d2
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 60ebbebfea762188eb0b05e6d80821299f243e91a58ff9b4feea538e3363a6975ef07268c8fef4233442c92f6ff6283c02b884f4c030cfe0f4ea4ceb95fdf429
|
|
7
|
+
data.tar.gz: e6d2b6e806a238fb910a2abc4cf3415f4376e2c169039f9ab276b17fb833582ea4222b842e5b887150d97441be04772cb47c4544bf36ada165ef593d00c97dd2
|
|
@@ -18,9 +18,11 @@ module OccamsRecord
|
|
|
18
18
|
# Initialize a new eager loading context.
|
|
19
19
|
#
|
|
20
20
|
# @param mode [ActiveRecord::Base] the model that contains the associations that will be referenced.
|
|
21
|
+
# @param polymorphic [Boolean] When true, model is allowed to change, and it's assumed that not every loader
|
|
22
|
+
# is applicable to every model.
|
|
21
23
|
#
|
|
22
|
-
def initialize(model = nil)
|
|
23
|
-
@model = model
|
|
24
|
+
def initialize(model = nil, polymorphic: false)
|
|
25
|
+
@model, @polymorphic = model, polymorphic
|
|
24
26
|
@loaders = []
|
|
25
27
|
@dynamic_loaders = []
|
|
26
28
|
end
|
|
@@ -33,8 +35,8 @@ module OccamsRecord
|
|
|
33
35
|
def model=(model)
|
|
34
36
|
@model = model
|
|
35
37
|
@loaders = @loaders + @dynamic_loaders.map { |args|
|
|
36
|
-
build_loader(*args)
|
|
37
|
-
}
|
|
38
|
+
@polymorphic ? build_loader(*args) : build_loader!(*args)
|
|
39
|
+
}.compact
|
|
38
40
|
@dynamic_loaders = []
|
|
39
41
|
end
|
|
40
42
|
|
|
@@ -75,7 +77,7 @@ module OccamsRecord
|
|
|
75
77
|
#
|
|
76
78
|
def add(assoc, scope = nil, select: nil, use: nil, as: nil, optimizer: :select, &builder)
|
|
77
79
|
if @model
|
|
78
|
-
loader = build_loader(assoc, scope, select, use, as, optimizer, builder)
|
|
80
|
+
loader = build_loader!(assoc, scope, select, use, as, optimizer, builder)
|
|
79
81
|
@loaders << loader
|
|
80
82
|
loader
|
|
81
83
|
else
|
|
@@ -100,10 +102,16 @@ module OccamsRecord
|
|
|
100
102
|
|
|
101
103
|
private
|
|
102
104
|
|
|
105
|
+
def build_loader!(assoc, scope, select, use, as, optimizer, builder)
|
|
106
|
+
build_loader(assoc, scope, select, use, as, optimizer, builder) ||
|
|
107
|
+
raise("OccamsRecord: No assocation `:#{assoc}` on `#{@model.name}` or subclasses")
|
|
108
|
+
end
|
|
109
|
+
|
|
103
110
|
def build_loader(assoc, scope, select, use, as, optimizer, builder)
|
|
104
|
-
ref = @model.reflections[assoc.to_s]
|
|
105
|
-
|
|
106
|
-
|
|
111
|
+
ref = @model.reflections[assoc.to_s] ||
|
|
112
|
+
@model.subclasses.map(&:reflections).detect { |x| x.has_key? assoc.to_s }&.[](assoc.to_s)
|
|
113
|
+
return nil if ref.nil?
|
|
114
|
+
|
|
107
115
|
scope ||= ->(q) { q.select select } if select
|
|
108
116
|
loader_class = !!ref.through_reflection ? EagerLoaders::Through : EagerLoaders.fetch!(ref)
|
|
109
117
|
loader_class.new(ref, scope, use: use, as: as, optimizer: optimizer, &builder)
|
|
@@ -21,7 +21,7 @@ module OccamsRecord
|
|
|
21
21
|
@name = (as || ref.name).to_s
|
|
22
22
|
@foreign_type = @ref.foreign_type.to_sym
|
|
23
23
|
@foreign_key = @ref.foreign_key.to_sym
|
|
24
|
-
@eager_loaders = EagerLoaders::Context.new
|
|
24
|
+
@eager_loaders = EagerLoaders::Context.new(nil, polymorphic: true)
|
|
25
25
|
instance_eval(&builder) if builder
|
|
26
26
|
end
|
|
27
27
|
|