ree_lib 1.0.55 → 1.0.56
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +1 -1
- data/lib/ree_lib/packages/ree_dao/package/ree_dao/association.rb +19 -70
- data/lib/ree_lib/packages/ree_dao/package/ree_dao/associations.rb +54 -56
- data/lib/ree_lib/packages/ree_dao/package/ree_dao/functions/load_agg.rb +9 -3
- data/lib/ree_lib/packages/ree_dao/schemas/ree_dao/functions/load_agg.schema.json +6 -6
- data/lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/load_agg/load_agg_spec.rb +123 -18
- data/lib/ree_lib/packages/ree_dao/spec/ree_dao/functions/load_agg/ree_dao_load_agg_test.rb +19 -0
- data/lib/ree_lib/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 23c4603ab3ab820869ca4478b42b3d350c5484c8da7b21c99b469449f45764a1
|
4
|
+
data.tar.gz: 4293f4d083185f09f6098e11698b5f35981a748b00ff84e486389bab19445751
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: fe885988996c1f0b9aeba40c9f61e119ab4ffc02aa471afe3774b470192fc5601093c1b13673febebad76bf348baa0be61cb2e19a5abf81b1970b518d86ddc9d
|
7
|
+
data.tar.gz: acbdf84eacfa6c46c6997999af1e0eb5dc03eedc95b95d954927a3907048c9c28d924328041203d02db1bd5c9dc78310c2cc234db826c74e585a8701b7982ed9
|
data/Gemfile.lock
CHANGED
@@ -26,8 +26,12 @@ module ReeDao
|
|
26
26
|
load_association(assoc_type, assoc_name, **opts, &block)
|
27
27
|
end
|
28
28
|
|
29
|
+
def handle_field(assoc_name, proc)
|
30
|
+
proc.call
|
31
|
+
end
|
32
|
+
|
29
33
|
contract(
|
30
|
-
Or[:belongs_to, :has_one, :has_many
|
34
|
+
Or[:belongs_to, :has_one, :has_many],
|
31
35
|
Symbol,
|
32
36
|
Ksplat[RestKeys => Any],
|
33
37
|
Optblock => Nilor[Array]
|
@@ -87,8 +91,7 @@ module ReeDao
|
|
87
91
|
scope: opts[:scope],
|
88
92
|
primary_key: opts[:primary_key],
|
89
93
|
foreign_key: opts[:foreign_key],
|
90
|
-
setter: opts[:setter]
|
91
|
-
skip_dao: true
|
94
|
+
setter: opts[:setter]
|
92
95
|
)
|
93
96
|
end
|
94
97
|
end
|
@@ -112,7 +115,7 @@ module ReeDao
|
|
112
115
|
parent.local_vars,
|
113
116
|
autoload_children,
|
114
117
|
**global_opts
|
115
|
-
).instance_exec(assoc_list, &block).map(&:join)
|
118
|
+
).instance_exec(assoc_list, &block)[:association_threads].map(&:join)
|
116
119
|
end
|
117
120
|
end
|
118
121
|
|
@@ -127,20 +130,11 @@ module ReeDao
|
|
127
130
|
reverse: Bool
|
128
131
|
] => Or[Hash, Array]
|
129
132
|
)
|
130
|
-
def one_to_one(
|
131
|
-
assoc_name,
|
132
|
-
list,
|
133
|
-
scope: nil,
|
134
|
-
primary_key: :id,
|
135
|
-
foreign_key: nil,
|
136
|
-
setter: nil,
|
137
|
-
reverse: true
|
138
|
-
)
|
133
|
+
def one_to_one(assoc_name, list, scope: nil, primary_key: :id, foreign_key: nil, setter: nil, reverse: true)
|
139
134
|
return {} if list.empty?
|
140
135
|
|
141
136
|
primary_key ||= :id
|
142
137
|
|
143
|
-
# TODO: refactor
|
144
138
|
if scope.is_a?(Array)
|
145
139
|
items = scope
|
146
140
|
else
|
@@ -169,7 +163,9 @@ module ReeDao
|
|
169
163
|
end
|
170
164
|
end
|
171
165
|
|
172
|
-
default_scope =
|
166
|
+
default_scope = if !scope
|
167
|
+
assoc_dao&.where(foreign_key => root_ids)
|
168
|
+
end
|
173
169
|
|
174
170
|
items = add_scopes(default_scope, scope, global_opts[assoc_name])
|
175
171
|
end
|
@@ -199,35 +195,27 @@ module ReeDao
|
|
199
195
|
foreign_key: Nilor[Symbol],
|
200
196
|
primary_key: Nilor[Symbol],
|
201
197
|
scope: Nilor[Sequel::Dataset, Array],
|
202
|
-
setter: Nilor[Or[Symbol, Proc]]
|
203
|
-
skip_dao: Bool
|
198
|
+
setter: Nilor[Or[Symbol, Proc]]
|
204
199
|
] => Or[Hash, Array]
|
205
200
|
)
|
206
|
-
def one_to_many(
|
207
|
-
assoc_name,
|
208
|
-
list,
|
209
|
-
primary_key: nil,
|
210
|
-
foreign_key: nil,
|
211
|
-
scope: nil,
|
212
|
-
setter: nil,
|
213
|
-
skip_dao: false
|
214
|
-
)
|
201
|
+
def one_to_many(assoc_name, list, primary_key: nil, foreign_key: nil, scope: nil, setter: nil)
|
215
202
|
return {} if list.empty?
|
216
203
|
|
217
204
|
primary_key ||= :id
|
218
205
|
|
219
|
-
# TODO: refactor
|
220
206
|
if scope.is_a?(Array)
|
221
207
|
items = scope
|
222
208
|
else
|
223
209
|
assoc_dao = nil
|
224
|
-
assoc_dao = find_dao(assoc_name, parent, scope)
|
210
|
+
assoc_dao = find_dao(assoc_name, parent, scope)
|
225
211
|
|
226
212
|
foreign_key ||= "#{underscore(demodulize(list.first.class.name))}_id".to_sym
|
227
213
|
|
228
214
|
root_ids = list.map(&:"#{primary_key}")
|
229
215
|
|
230
|
-
default_scope =
|
216
|
+
default_scope = if !scope
|
217
|
+
assoc_dao&.where(foreign_key => root_ids)
|
218
|
+
end
|
231
219
|
|
232
220
|
items = add_scopes(default_scope, scope, global_opts[assoc_name])
|
233
221
|
end
|
@@ -259,14 +247,7 @@ module ReeDao
|
|
259
247
|
setter: Nilor[Or[Symbol, Proc]]
|
260
248
|
] => Any
|
261
249
|
)
|
262
|
-
def populate_association(
|
263
|
-
list,
|
264
|
-
association_index,
|
265
|
-
assoc_name,
|
266
|
-
primary_key: nil,
|
267
|
-
reverse: nil,
|
268
|
-
setter: nil
|
269
|
-
)
|
250
|
+
def populate_association(list, association_index, assoc_name, primary_key: nil, reverse: nil, setter: nil)
|
270
251
|
assoc_setter = if setter
|
271
252
|
setter
|
272
253
|
else
|
@@ -296,23 +277,7 @@ module ReeDao
|
|
296
277
|
|
297
278
|
contract(Nilor[Sequel::Dataset], Nilor[Sequel::Dataset], Nilor[Proc] => Array)
|
298
279
|
def add_scopes(default_scope, scope, named_scope)
|
299
|
-
|
300
|
-
res = default_scope
|
301
|
-
end
|
302
|
-
|
303
|
-
if default_scope && scope
|
304
|
-
if scope == []
|
305
|
-
res = default_scope
|
306
|
-
else
|
307
|
-
res = merge_scopes(default_scope, scope)
|
308
|
-
end
|
309
|
-
end
|
310
|
-
|
311
|
-
if !default_scope && scope
|
312
|
-
return [] if scope.empty?
|
313
|
-
|
314
|
-
res = scope
|
315
|
-
end
|
280
|
+
res = scope || default_scope
|
316
281
|
|
317
282
|
if named_scope
|
318
283
|
res = named_scope.call(res)
|
@@ -321,22 +286,6 @@ module ReeDao
|
|
321
286
|
res.all
|
322
287
|
end
|
323
288
|
|
324
|
-
def merge_scopes(s1, s2)
|
325
|
-
if s2.opts[:where]
|
326
|
-
s1 = s1.where(s2.opts[:where])
|
327
|
-
end
|
328
|
-
|
329
|
-
if s2.opts[:order]
|
330
|
-
s1 = s1.order(*s2.opts[:order])
|
331
|
-
end
|
332
|
-
|
333
|
-
if s1.opts[:schema_mapper] != s2.opts[:schema_mapper]
|
334
|
-
s1 = s1.with_mapper(s2.opts[:schema_mapper])
|
335
|
-
end
|
336
|
-
|
337
|
-
s1
|
338
|
-
end
|
339
|
-
|
340
289
|
def find_dao(assoc_name, parent, scope)
|
341
290
|
dao_from_name = parent.instance_variable_get("@#{assoc_name}") || parent.instance_variable_get("@#{assoc_name}s")
|
342
291
|
return dao_from_name if dao_from_name
|
@@ -8,14 +8,19 @@ module ReeDao
|
|
8
8
|
@agg_caller = agg_caller
|
9
9
|
@list = list
|
10
10
|
@local_vars = local_vars
|
11
|
-
@
|
12
|
-
@global_opts = opts
|
11
|
+
@global_opts = opts || {}
|
13
12
|
@only = opts[:only] if opts[:only]
|
14
13
|
@except = opts[:except] if opts[:except]
|
15
14
|
@autoload_children = autoload_children
|
16
15
|
|
17
16
|
raise ArgumentError.new("you can't use both :only and :except arguments at the same time") if @only && @except
|
18
17
|
|
18
|
+
|
19
|
+
if !self.class.sync_mode?
|
20
|
+
@assoc_threads = []
|
21
|
+
@field_threads = []
|
22
|
+
end
|
23
|
+
|
19
24
|
local_vars.each do |k, v|
|
20
25
|
instance_variable_set(k, v)
|
21
26
|
|
@@ -31,62 +36,34 @@ module ReeDao
|
|
31
36
|
|
32
37
|
contract(
|
33
38
|
Symbol,
|
34
|
-
|
35
|
-
scope?: Or[Sequel::Dataset, Array],
|
36
|
-
setter?: Or[Symbol, Proc],
|
37
|
-
primary_key?: Symbol,
|
38
|
-
foreign_key?: Symbol,
|
39
|
-
autoload_children?: Bool
|
40
|
-
],
|
39
|
+
Nilor[Proc, Sequel::Dataset],
|
41
40
|
Optblock => Any
|
42
41
|
)
|
43
|
-
def belongs_to(assoc_name,
|
44
|
-
association(__method__, assoc_name,
|
42
|
+
def belongs_to(assoc_name, opts = nil, &block)
|
43
|
+
association(__method__, assoc_name, opts, &block)
|
45
44
|
end
|
46
45
|
|
47
46
|
contract(
|
48
47
|
Symbol,
|
49
|
-
|
50
|
-
scope?: Or[Sequel::Dataset, Array],
|
51
|
-
setter?: Or[Symbol, Proc],
|
52
|
-
primary_key?: Symbol,
|
53
|
-
foreign_key?: Symbol,
|
54
|
-
autoload_children?: Bool
|
55
|
-
],
|
48
|
+
Nilor[Proc, Sequel::Dataset],
|
56
49
|
Optblock => Any
|
57
50
|
)
|
58
|
-
def has_one(assoc_name,
|
59
|
-
association(__method__, assoc_name,
|
51
|
+
def has_one(assoc_name, opts = nil, &block)
|
52
|
+
association(__method__, assoc_name, opts, &block)
|
60
53
|
end
|
61
54
|
|
62
55
|
contract(
|
63
56
|
Symbol,
|
64
|
-
|
65
|
-
scope?: Or[Sequel::Dataset, Array],
|
66
|
-
setter?: Or[Symbol, Proc],
|
67
|
-
primary_key?: Symbol,
|
68
|
-
foreign_key?: Symbol,
|
69
|
-
autoload_children?: Bool
|
70
|
-
],
|
57
|
+
Nilor[Proc, Sequel::Dataset],
|
71
58
|
Optblock => Any
|
72
59
|
)
|
73
|
-
def has_many(assoc_name,
|
74
|
-
association(__method__, assoc_name,
|
60
|
+
def has_many(assoc_name, opts = nil, &block)
|
61
|
+
association(__method__, assoc_name, opts, &block)
|
75
62
|
end
|
76
63
|
|
77
|
-
contract(
|
78
|
-
|
79
|
-
|
80
|
-
scope?: Or[Sequel::Dataset, Array],
|
81
|
-
setter?: Or[Symbol, Proc],
|
82
|
-
primary_key?: Symbol,
|
83
|
-
foreign_key?: Symbol,
|
84
|
-
autoload_children?: Bool
|
85
|
-
],
|
86
|
-
Optblock => Any
|
87
|
-
)
|
88
|
-
def field(assoc_name, **opts, &block)
|
89
|
-
association(__method__, assoc_name, **opts, &block)
|
64
|
+
contract(Symbol, Proc => Any)
|
65
|
+
def field(assoc_name, proc)
|
66
|
+
association(__method__, assoc_name, proc)
|
90
67
|
end
|
91
68
|
|
92
69
|
private
|
@@ -99,27 +76,40 @@ module ReeDao
|
|
99
76
|
:field
|
100
77
|
],
|
101
78
|
Symbol,
|
102
|
-
|
103
|
-
scope?: Or[Sequel::Dataset, Array],
|
104
|
-
setter?: Or[Symbol, Proc],
|
105
|
-
primary_key?: Symbol,
|
106
|
-
foreign_key?: Symbol,
|
107
|
-
autoload_children?: Bool
|
108
|
-
],
|
79
|
+
Nilor[Proc, Sequel::Dataset],
|
109
80
|
Optblock => Any
|
110
81
|
)
|
111
|
-
def association(assoc_type, assoc_name,
|
82
|
+
def association(assoc_type, assoc_name, opts, &block)
|
112
83
|
if self.class.sync_mode?
|
113
84
|
return if association_is_not_included?(assoc_name) || list.empty?
|
114
|
-
|
85
|
+
|
115
86
|
association = Association.new(self, list, **global_opts)
|
116
|
-
|
87
|
+
if assoc_type == :field
|
88
|
+
association.handle_field(assoc_name, opts)
|
89
|
+
else
|
90
|
+
association.load(assoc_type, assoc_name, **get_assoc_opts(opts), &block)
|
91
|
+
end
|
117
92
|
else
|
118
|
-
|
93
|
+
if association_is_not_included?(assoc_name) || list.empty?
|
94
|
+
return { association_threads: @assoc_threads, field_threads: @field_threads }
|
95
|
+
end
|
96
|
+
|
97
|
+
association = Association.new(self, list, **global_opts)
|
119
98
|
|
120
|
-
|
121
|
-
|
122
|
-
|
99
|
+
if assoc_type == :field
|
100
|
+
{
|
101
|
+
association_threads: @assoc_threads,
|
102
|
+
field_threads: @field_threads << Thread.new do
|
103
|
+
association.handle_field(assoc_name, opts)
|
104
|
+
end
|
105
|
+
}
|
106
|
+
else
|
107
|
+
{
|
108
|
+
association_threads: @assoc_threads << Thread.new do
|
109
|
+
association.load(assoc_type, assoc_name, **get_assoc_opts(opts), &block)
|
110
|
+
end,
|
111
|
+
field_threads: @field_threads
|
112
|
+
}
|
123
113
|
end
|
124
114
|
end
|
125
115
|
end
|
@@ -149,5 +139,13 @@ module ReeDao
|
|
149
139
|
|
150
140
|
agg_caller.send(method, *args, &block)
|
151
141
|
end
|
142
|
+
|
143
|
+
def get_assoc_opts(opts)
|
144
|
+
if opts.is_a?(Proc)
|
145
|
+
opts.call
|
146
|
+
else
|
147
|
+
{}
|
148
|
+
end
|
149
|
+
end
|
152
150
|
end
|
153
151
|
end
|
@@ -10,16 +10,17 @@ class ReeDao::LoadAgg
|
|
10
10
|
end
|
11
11
|
|
12
12
|
contract(
|
13
|
-
Or[Sequel::Dataset, ArrayOf[Integer], ArrayOf[EntityContract], Integer],
|
14
13
|
Nilor[DaoDatasetContract],
|
14
|
+
Or[Sequel::Dataset, ArrayOf[Integer], ArrayOf[EntityContract], Integer],
|
15
15
|
Ksplat[
|
16
16
|
only?: ArrayOf[Symbol],
|
17
17
|
except?: ArrayOf[Symbol],
|
18
|
+
to_dto?: Proc,
|
18
19
|
RestKeys => Any
|
19
20
|
],
|
20
21
|
Optblock => ArrayOf[Any]
|
21
22
|
)
|
22
|
-
def call(
|
23
|
+
def call(dao = nil, ids_or_scope, **opts, &block)
|
23
24
|
scope = if ids_or_scope.is_a?(Array) && ids_or_scope.any? { _1.is_a?(Integer) }
|
24
25
|
raise ArgumentError.new("Dao should be provided") if dao.nil?
|
25
26
|
return [] if ids_or_scope.empty?
|
@@ -35,6 +36,10 @@ class ReeDao::LoadAgg
|
|
35
36
|
|
36
37
|
list = scope.is_a?(Sequel::Dataset) ? scope.all : scope
|
37
38
|
|
39
|
+
if opts[:to_dto]
|
40
|
+
list = opts[:to_dto].call(list)
|
41
|
+
end
|
42
|
+
|
38
43
|
load_associations(list, **opts, &block) if block_given?
|
39
44
|
|
40
45
|
if ids_or_scope.is_a?(Array)
|
@@ -61,7 +66,8 @@ class ReeDao::LoadAgg
|
|
61
66
|
if ReeDao.load_sync_associations_enabled?
|
62
67
|
associations
|
63
68
|
else
|
64
|
-
associations.map(&:join)
|
69
|
+
associations[:association_threads].map(&:join)
|
70
|
+
associations[:field_threads].map(&:join)
|
65
71
|
end
|
66
72
|
end
|
67
73
|
end
|
@@ -14,20 +14,20 @@
|
|
14
14
|
],
|
15
15
|
"return": "ArrayOf[Any]",
|
16
16
|
"args": [
|
17
|
-
{
|
18
|
-
"arg": "ids_or_scope",
|
19
|
-
"arg_type": "req",
|
20
|
-
"type": "Or[Sequel::Dataset, ArrayOf[Integer], ArrayOf[PackageName::Entity], Integer]"
|
21
|
-
},
|
22
17
|
{
|
23
18
|
"arg": "dao",
|
24
19
|
"arg_type": "opt",
|
25
20
|
"type": "Nilor[PackageName::DaoName::Dao: \"SELECT * FROM `table`\"]"
|
26
21
|
},
|
22
|
+
{
|
23
|
+
"arg": "ids_or_scope",
|
24
|
+
"arg_type": "req",
|
25
|
+
"type": "Or[Sequel::Dataset, ArrayOf[Integer], ArrayOf[PackageName::Entity], Integer]"
|
26
|
+
},
|
27
27
|
{
|
28
28
|
"arg": "opts",
|
29
29
|
"arg_type": "keyrest",
|
30
|
-
"type": "Ksplat[:only? => ArrayOf[Symbol], :except? => ArrayOf[Symbol], \"RestKeys\" => Any]"
|
30
|
+
"type": "Ksplat[:only? => ArrayOf[Symbol], :except? => ArrayOf[Symbol], :to_dto? => Proc, \"RestKeys\" => Any]"
|
31
31
|
},
|
32
32
|
{
|
33
33
|
"arg": "block",
|
@@ -99,19 +99,67 @@ RSpec.describe :load_agg do
|
|
99
99
|
end
|
100
100
|
|
101
101
|
def call(ids_or_scope, **opts)
|
102
|
-
load_agg(
|
102
|
+
load_agg(users, ids_or_scope, **opts) do |users_list|
|
103
103
|
belongs_to :organization
|
104
|
-
has_many :books do
|
104
|
+
has_many :books do |books_list|
|
105
105
|
has_one :author
|
106
106
|
has_many :chapters
|
107
107
|
|
108
|
-
has_many :reviews do
|
108
|
+
has_many :reviews do |reviews_list|
|
109
109
|
has_one :review_author
|
110
|
+
|
111
|
+
field :review_calculatetable_field, -> { some_method(reviews_list) }
|
110
112
|
end
|
113
|
+
|
114
|
+
field :book_calculatetable_field, -> { change_book_titles(books_list) }
|
111
115
|
end
|
112
116
|
|
113
|
-
has_one :passport,
|
114
|
-
has_one :custom_field,
|
117
|
+
has_one :passport, -> { passport_opts }
|
118
|
+
has_one :custom_field, -> { custom_field_opts }
|
119
|
+
|
120
|
+
field :user_calculatetable_field, -> { some_method(users_list) }
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
private
|
125
|
+
|
126
|
+
def change_book_titles(books_list)
|
127
|
+
books_list.each do |book|
|
128
|
+
book.title = "#{book.title.upcase} changed"
|
129
|
+
end
|
130
|
+
end
|
131
|
+
|
132
|
+
def some_method(list)
|
133
|
+
puts list.map(&:id)
|
134
|
+
puts list.map { _1.class.name }
|
135
|
+
end
|
136
|
+
|
137
|
+
def passport_opts
|
138
|
+
{
|
139
|
+
foreign_key: :user_id,
|
140
|
+
scope: user_passports
|
141
|
+
}
|
142
|
+
end
|
143
|
+
|
144
|
+
def custom_field_opts
|
145
|
+
{
|
146
|
+
scope: books.where(title: "1984")
|
147
|
+
}
|
148
|
+
end
|
149
|
+
end
|
150
|
+
|
151
|
+
class ReeDaoLoadAggTest::UsersAggWithDto
|
152
|
+
include ReeDao::AggregateDSL
|
153
|
+
|
154
|
+
aggregate :users_agg_with_dto do
|
155
|
+
link :users, from: :ree_dao_load_agg_test
|
156
|
+
link :organizations, from: :ree_dao_load_agg_test
|
157
|
+
link :load_agg, from: :ree_dao
|
158
|
+
end
|
159
|
+
|
160
|
+
def call(ids_or_scope, **opts)
|
161
|
+
load_agg(users, ids_or_scope, **opts) do
|
162
|
+
belongs_to :organization
|
115
163
|
end
|
116
164
|
end
|
117
165
|
end
|
@@ -130,9 +178,9 @@ RSpec.describe :load_agg do
|
|
130
178
|
end
|
131
179
|
|
132
180
|
def call(ids_or_scope, **opts)
|
133
|
-
load_agg(
|
181
|
+
load_agg(users, ids_or_scope, **opts) do
|
134
182
|
belongs_to :organization
|
135
|
-
has_many :books,
|
183
|
+
has_many :books, -> { books_opts } do
|
136
184
|
has_one :author
|
137
185
|
has_many :chapters
|
138
186
|
|
@@ -142,6 +190,12 @@ RSpec.describe :load_agg do
|
|
142
190
|
end
|
143
191
|
end
|
144
192
|
end
|
193
|
+
|
194
|
+
private
|
195
|
+
|
196
|
+
def books_opts
|
197
|
+
{ autoload_children: true }
|
198
|
+
end
|
145
199
|
end
|
146
200
|
|
147
201
|
class ReeDaoLoadAggTest::UsersAggAutoloadReviewsChildren
|
@@ -158,18 +212,24 @@ RSpec.describe :load_agg do
|
|
158
212
|
end
|
159
213
|
|
160
214
|
def call(ids_or_scope, **opts)
|
161
|
-
load_agg(
|
215
|
+
load_agg(users, ids_or_scope, **opts) do
|
162
216
|
belongs_to :organization
|
163
217
|
has_many :books do
|
164
218
|
has_one :author
|
165
219
|
has_many :chapters
|
166
220
|
|
167
|
-
has_many :reviews, autoload_children: true do
|
221
|
+
has_many :reviews, -> { { autoload_children: true } } do
|
168
222
|
has_one :review_author
|
169
223
|
end
|
170
224
|
end
|
171
225
|
end
|
172
226
|
end
|
227
|
+
|
228
|
+
private
|
229
|
+
|
230
|
+
def reviews_opts
|
231
|
+
{ autoload_children: true }
|
232
|
+
end
|
173
233
|
end
|
174
234
|
|
175
235
|
class ReeDaoLoadAggTest::UsersAggBlockTest
|
@@ -183,13 +243,21 @@ RSpec.describe :load_agg do
|
|
183
243
|
end
|
184
244
|
|
185
245
|
def call(ids_or_scope, **opts)
|
186
|
-
load_agg(
|
246
|
+
load_agg(users, ids_or_scope, **opts) do
|
187
247
|
belongs_to :organization
|
188
|
-
has_many :books,
|
248
|
+
has_many :books, -> { books_opts }
|
249
|
+
end
|
250
|
+
end
|
251
|
+
|
252
|
+
private
|
253
|
+
|
254
|
+
def books_opts
|
255
|
+
{
|
256
|
+
setter: -> (item, items_index) {
|
189
257
|
b = items_index[item.id].each { |b| b.title = "Changed" }
|
190
258
|
item.set_books(b)
|
191
259
|
}
|
192
|
-
|
260
|
+
}
|
193
261
|
end
|
194
262
|
end
|
195
263
|
|
@@ -204,17 +272,21 @@ RSpec.describe :load_agg do
|
|
204
272
|
end
|
205
273
|
|
206
274
|
def call(ids_or_scope, **opts)
|
207
|
-
load_agg(
|
275
|
+
load_agg(users, ids_or_scope, **opts) do |agg_list|
|
208
276
|
some_id = agg_list.first.id
|
209
277
|
title = "1984"
|
210
278
|
belongs_to :organization
|
211
279
|
|
212
|
-
has_many :books,
|
280
|
+
has_many :books, -> { books_opts(title) }
|
213
281
|
end
|
214
282
|
end
|
215
283
|
|
216
284
|
private
|
217
285
|
|
286
|
+
def books_opts(title)
|
287
|
+
{ scope: books_scope(title) }
|
288
|
+
end
|
289
|
+
|
218
290
|
def books_scope(title)
|
219
291
|
books.where(title: title)
|
220
292
|
end
|
@@ -231,7 +303,7 @@ RSpec.describe :load_agg do
|
|
231
303
|
end
|
232
304
|
|
233
305
|
def call(ids_or_scope, **opts)
|
234
|
-
load_agg(
|
306
|
+
load_agg(users, ids_or_scope, **opts) do
|
235
307
|
has_many :something
|
236
308
|
end
|
237
309
|
end
|
@@ -243,6 +315,7 @@ RSpec.describe :load_agg do
|
|
243
315
|
let(:users_agg_autoload_books_children) { ReeDaoLoadAggTest::UsersAggAutoloadBooksChildren.new }
|
244
316
|
let(:users_agg_autoload_reviews_children) { ReeDaoLoadAggTest::UsersAggAutoloadReviewsChildren.new }
|
245
317
|
let(:users_agg_without_dao) { ReeDaoLoadAggTest::UsersAggWithoutDao.new }
|
318
|
+
let(:users_agg_with_dto) { ReeDaoLoadAggTest::UsersAggWithDto.new }
|
246
319
|
let(:organizations) { ReeDaoLoadAggTest::Organizations.new }
|
247
320
|
let(:users) { ReeDaoLoadAggTest::Users.new }
|
248
321
|
let(:user_passports) { ReeDaoLoadAggTest::UserPassports.new }
|
@@ -267,6 +340,37 @@ RSpec.describe :load_agg do
|
|
267
340
|
}.to raise_error(ArgumentError)
|
268
341
|
}
|
269
342
|
|
343
|
+
it {
|
344
|
+
organizations.delete_all
|
345
|
+
users.delete_all
|
346
|
+
|
347
|
+
organization = ReeDaoLoadAggTest::Organization.new(name: "Test Org")
|
348
|
+
organizations.put(organization)
|
349
|
+
|
350
|
+
user_1 = ReeDaoLoadAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
|
351
|
+
user_2 = ReeDaoLoadAggTest::User.new(name: "Sam", age: 21, organization_id: organization.id)
|
352
|
+
users.put(user_1)
|
353
|
+
users.put(user_2)
|
354
|
+
|
355
|
+
res = users_agg_with_dto.call(
|
356
|
+
users.all,
|
357
|
+
to_dto: -> (users) {
|
358
|
+
users.map do |user|
|
359
|
+
ReeDaoLoadAggTest::UserDto.new(
|
360
|
+
id: user.id,
|
361
|
+
name: user.name,
|
362
|
+
organization_id: user.organization_id,
|
363
|
+
full_name: user.name
|
364
|
+
)
|
365
|
+
end
|
366
|
+
}
|
367
|
+
)
|
368
|
+
|
369
|
+
res_user = res[0]
|
370
|
+
|
371
|
+
expect(res_user.class).to eq(ReeDaoLoadAggTest::UserDto)
|
372
|
+
}
|
373
|
+
|
270
374
|
it {
|
271
375
|
organizations.delete_all
|
272
376
|
users.delete_all
|
@@ -317,6 +421,7 @@ RSpec.describe :load_agg do
|
|
317
421
|
expect(res_user.passport).to eq(passport_1)
|
318
422
|
expect(res_user.passport.info).to eq("some info")
|
319
423
|
expect(res_user.books.count).to eq(2)
|
424
|
+
expect(res_user.books.map(&:title)).to eq(["1984 changed", "1408 changed"])
|
320
425
|
expect(res_user.books[0].author.name).to eq("George Orwell")
|
321
426
|
expect(res_user.books[0].chapters.map(&:title)).to eq(["beginning"])
|
322
427
|
expect(res_user.books[0].reviews[0].review_author.name).to eq("John Review")
|
@@ -595,7 +700,7 @@ RSpec.describe :load_agg do
|
|
595
700
|
|
596
701
|
ids = [user_1, user_2].map(&:id)
|
597
702
|
|
598
|
-
res = load_agg(
|
703
|
+
res = load_agg(users, ids)
|
599
704
|
expect(res.count).to eq(2)
|
600
705
|
}
|
601
706
|
|
@@ -609,7 +714,7 @@ RSpec.describe :load_agg do
|
|
609
714
|
user_1 = ReeDaoLoadAggTest::User.new(name: "John", age: 33, organization_id: organization.id)
|
610
715
|
users.put(user_1)
|
611
716
|
|
612
|
-
res = load_agg(user_1.id
|
717
|
+
res = load_agg(users, user_1.id)
|
613
718
|
expect(res.count).to eq(1)
|
614
719
|
}
|
615
720
|
|
@@ -625,7 +730,7 @@ RSpec.describe :load_agg do
|
|
625
730
|
users.put(user_1)
|
626
731
|
users.put(user_2)
|
627
732
|
|
628
|
-
res = load_agg(users.where(organization_id: organization.id)
|
733
|
+
res = load_agg(users, users.where(organization_id: organization.id))
|
629
734
|
expect(res.count).to eq(2)
|
630
735
|
}
|
631
736
|
end
|
@@ -134,6 +134,25 @@ class ReeDaoLoadAggTest::User
|
|
134
134
|
attr_accessor :name, :age, :organization_id
|
135
135
|
end
|
136
136
|
|
137
|
+
class ReeDaoLoadAggTest::UserDto
|
138
|
+
include ReeDto::EntityDSL
|
139
|
+
|
140
|
+
properties(
|
141
|
+
id: Integer,
|
142
|
+
organization_id: Integer,
|
143
|
+
name: String,
|
144
|
+
full_name: String,
|
145
|
+
)
|
146
|
+
|
147
|
+
def set_organization(org)
|
148
|
+
@organization = org; nil
|
149
|
+
end
|
150
|
+
|
151
|
+
def organization
|
152
|
+
@organization
|
153
|
+
end
|
154
|
+
end
|
155
|
+
|
137
156
|
class ReeDaoLoadAggTest::Organization
|
138
157
|
include ReeDto::EntityDSL
|
139
158
|
|
data/lib/ree_lib/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: ree_lib
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 1.0.
|
4
|
+
version: 1.0.56
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Ruslan Gatiyatov
|
8
8
|
autorequire:
|
9
9
|
bindir: exe
|
10
10
|
cert_chain: []
|
11
|
-
date: 2023-07-
|
11
|
+
date: 2023-07-31 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: ree
|