Skip to content

Commit ca65acc

Browse files
committed
fix issue where preload opts from polymorphic_belongs_to were not passed down in to individual include_in calls
1 parent 4360507 commit ca65acc

File tree

3 files changed

+63
-6
lines changed

3 files changed

+63
-6
lines changed

lapis/db/model/relations.lua

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -678,11 +678,28 @@ polymorphic_belongs_to = function(self, name, opts)
678678
end
679679
filtered = _accum_0
680680
end
681-
model:include_in(filtered, id_col, {
681+
local include_opts = {
682682
for_relation = name,
683683
as = name,
684684
fields = fields and fields[type_name]
685-
})
685+
}
686+
if preload_opts then
687+
for k, v in pairs(preload_opts) do
688+
local _continue_0 = false
689+
repeat
690+
if k == "fields" then
691+
_continue_0 = true
692+
break
693+
end
694+
include_opts[k] = v
695+
_continue_0 = true
696+
until true
697+
if not _continue_0 then
698+
break
699+
end
700+
end
701+
end
702+
model:include_in(filtered, id_col, include_opts)
686703
end
687704
return objs
688705
end

lapis/db/model/relations.moon

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -517,12 +517,19 @@ polymorphic_belongs_to = (name, opts) =>
517517
for {type_name, model_name} in *types
518518
model = assert_model @@, model_name
519519
filtered = [o for o in *objs when o[type_col] == @@[enum_name][type_name]]
520-
model\include_in filtered, id_col, {
520+
include_opts = {
521521
for_relation: name
522522
as: name
523523
fields: fields and fields[type_name]
524524
}
525525

526+
if preload_opts
527+
for k, v in pairs preload_opts
528+
continue if k == "fields"
529+
include_opts[k] = v
530+
531+
model\include_in filtered, id_col, include_opts
532+
526533
objs
527534

528535
-- TODO: deprecate this for use of `preload` or `Model:preload_relation` method

spec/relations_spec.moon

Lines changed: 36 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -803,9 +803,10 @@ describe "lapis.db.model.relations", ->
803803
assert.same nil, rawget Child, "get_user"
804804

805805
describe "polymorphic belongs to", ->
806-
local Foos, Bars, Bazs, Items
806+
local Foos, Bars, Bazs, Items, preload
807807

808808
before_each ->
809+
import preload from require "lapis.db.model"
809810
models.Foos = class Foos extends Model
810811
models.Bars = class Bars extends Model
811812
@primary_key: "frog_index"
@@ -944,6 +945,39 @@ describe "lapis.db.model.relations", ->
944945
'SELECT c, d FROM "bazs" WHERE "id" IN (113)'
945946
}
946947

948+
it "preloads with skip_included option", ->
949+
foo = models.Foos\load {
950+
id: 111
951+
}
952+
953+
items = {
954+
Items\load {
955+
object_type: 1
956+
object_id: 111
957+
object: foo
958+
}
959+
960+
Items\load {
961+
object_type: 1
962+
object_id: 222
963+
}
964+
965+
Items\load {
966+
object_type: 2
967+
object_id: 333
968+
}
969+
}
970+
971+
assert_queries {
972+
'SELECT * FROM "foos" WHERE "id" IN (222)'
973+
'SELECT * FROM "bars" WHERE "frog_index" IN (333)'
974+
}, ->
975+
preload items, object: {
976+
[preload]: {
977+
skip_included: true
978+
}
979+
}
980+
947981
it "finds relation", ->
948982
import find_relation from require "lapis.db.model.relations"
949983

@@ -1872,7 +1906,7 @@ describe "lapis.db.model.relations", ->
18721906
random: "option"
18731907
}, preload_opts
18741908

1875-
it "with skip_included preload option #ddd", ->
1909+
it "with skip_included preload option", ->
18761910
models.Items = class Items extends Model
18771911
@relations: {
18781912
{"parents", has_many: "Items", key: "parent_id"}
@@ -2222,4 +2256,3 @@ describe "lapis.db.model.relations", ->
22222256
{ id: 201, user_id: 20 }
22232257
}, users[3].tags
22242258

2225-

0 commit comments

Comments
 (0)