anonymous_active_record 1.0.3 → 1.0.8
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CODE_OF_CONDUCT.md +105 -46
- data/README.md +52 -46
- data/lib/anonymous_active_record.rb +57 -12
- data/lib/anonymous_active_record/factory.rb +2 -0
- data/lib/anonymous_active_record/generator.rb +10 -6
- data/lib/anonymous_active_record/version.rb +3 -1
- data/spec/anonymous_active_record_spec.rb +1025 -0
- data/spec/db/database.yml +4 -0
- data/spec/spec_helper.rb +22 -0
- metadata +115 -32
- data/.gitignore +0 -13
- data/.rspec +0 -3
- data/.travis.yml +0 -32
- data/Gemfile +0 -13
- data/Rakefile +0 -6
- data/anonymous_active_record.gemspec +0 -33
- data/bin/console +0 -14
- data/bin/setup +0 -8
@@ -1,3 +1,5 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
1
3
|
# Usage:
|
2
4
|
#
|
3
5
|
# klass = AnonymousActiveRecord.generate(columns: ['name']) do
|
@@ -12,16 +14,18 @@
|
|
12
14
|
module AnonymousActiveRecord
|
13
15
|
# Generator defines a pseudo anonymous class in a particular namespace of your choosing.
|
14
16
|
class Generator
|
15
|
-
DEFAULT_NAME = 'anons'
|
17
|
+
DEFAULT_NAME = 'anons'
|
16
18
|
|
17
|
-
attr_reader :klass_namespaces, :table_name, :klass_name
|
19
|
+
attr_reader :klass_namespaces, :table_name, :klass_name, :parent_klass
|
18
20
|
|
19
|
-
def initialize(table_name, klass_namespaces = [], klass_basename = nil)
|
21
|
+
def initialize(table_name, klass_namespaces = [], klass_basename = nil, parent_klass = 'ActiveRecord::Base')
|
20
22
|
@klass_namespaces = Array(klass_namespaces).map(&:to_s).map { |x| x.gsub(/[^a-z0-9]+/i, '_') }
|
21
|
-
klass_context = [(klass_basename || DEFAULT_NAME).gsub(/[^a-z0-9]+/i, '_').capitalize,
|
23
|
+
klass_context = [(klass_basename || DEFAULT_NAME).gsub(/[^a-z0-9]+/i, '_').capitalize,
|
24
|
+
SecureRandom.uuid.gsub(/[^a-z0-9]+/i, '_')].join('_')
|
22
25
|
@table_name = (table_name || klass_context).downcase.to_s
|
23
26
|
# String#capitalize is Ruby >= 2.4, https://stackoverflow.com/a/3725154/213191
|
24
27
|
@klass_name = (klass_namespaces << klass_context).join('::')
|
28
|
+
@parent_klass = parent_klass
|
25
29
|
end
|
26
30
|
|
27
31
|
def generate(&block)
|
@@ -29,9 +33,9 @@ module AnonymousActiveRecord
|
|
29
33
|
# Class.new(ActiveRecord::Base)
|
30
34
|
# which was useful for specs, and other use cases. But it is now explicitly not supported, see
|
31
35
|
# https://github.com/rails/rails/issues/8934
|
32
|
-
eval "class ::#{klass_name} <
|
36
|
+
eval "class ::#{klass_name} < #{parent_klass}; end"
|
33
37
|
klass = Kernel.const_get(klass_name, true)
|
34
|
-
klass.class_eval(&block) if
|
38
|
+
klass.class_eval(&block) if block
|
35
39
|
klass.table_name = table_name
|
36
40
|
klass
|
37
41
|
end
|
@@ -0,0 +1,1025 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
RSpec.describe AnonymousActiveRecord do
|
4
|
+
it 'has a version number' do
|
5
|
+
expect(AnonymousActiveRecord::VERSION).not_to be nil
|
6
|
+
end
|
7
|
+
|
8
|
+
describe '.generate' do
|
9
|
+
context 'minimal params' do
|
10
|
+
subject { described_class.generate }
|
11
|
+
|
12
|
+
it 'does not error' do
|
13
|
+
expect { subject }.not_to raise_error
|
14
|
+
end
|
15
|
+
|
16
|
+
context 'instance' do
|
17
|
+
subject { super().new }
|
18
|
+
|
19
|
+
it 'can be instantiated' do
|
20
|
+
expect(subject).to be_a(ActiveRecord::Base)
|
21
|
+
end
|
22
|
+
|
23
|
+
context 'timestamps' do
|
24
|
+
it 'has' do
|
25
|
+
expect(subject.created_at).to be_nil
|
26
|
+
expect(subject.updated_at).to be_nil
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'saving' do
|
31
|
+
subject do
|
32
|
+
i = super()
|
33
|
+
i.save
|
34
|
+
i
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'does not error' do
|
38
|
+
expect { subject }.not_to raise_error
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'does not have name' do
|
42
|
+
expect(subject).not_to respond_to(:name)
|
43
|
+
end
|
44
|
+
|
45
|
+
it 'sets timestamps' do
|
46
|
+
expect(subject.created_at).not_to be_nil
|
47
|
+
expect(subject.updated_at).not_to be_nil
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
context 'all params' do
|
54
|
+
subject do
|
55
|
+
described_class.generate(
|
56
|
+
table_name: table_name,
|
57
|
+
klass_namespaces: klass_namespaces,
|
58
|
+
klass_basename: klass_basename,
|
59
|
+
columns: columns,
|
60
|
+
indexes: indexes,
|
61
|
+
timestamps: timestamps,
|
62
|
+
connection_params: connection_params
|
63
|
+
)
|
64
|
+
end
|
65
|
+
|
66
|
+
let!(:farm_animal) do
|
67
|
+
module Farm
|
68
|
+
module Animal
|
69
|
+
end
|
70
|
+
end
|
71
|
+
end
|
72
|
+
let(:table_name) { 'dogs' }
|
73
|
+
let(:klass_namespaces) { %w[Farm Animal] }
|
74
|
+
let(:klass_basename) { 'my' }
|
75
|
+
let(:columns) { ['name'] }
|
76
|
+
let(:indexes) { [{ columns: ['name'] }] }
|
77
|
+
let(:timestamps) { true }
|
78
|
+
let(:connection_params) { AnonymousActiveRecord::DEFAULT_CONNECTION_PARAMS }
|
79
|
+
|
80
|
+
it 'does not error' do
|
81
|
+
expect { subject }.not_to raise_error
|
82
|
+
end
|
83
|
+
|
84
|
+
context 'instance' do
|
85
|
+
subject { super().new }
|
86
|
+
|
87
|
+
it 'can be instantiated' do
|
88
|
+
expect(subject).to be_a(ActiveRecord::Base)
|
89
|
+
end
|
90
|
+
|
91
|
+
context 'name' do
|
92
|
+
it 'has' do
|
93
|
+
expect(subject.name).to be_nil
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
context 'timestamps' do
|
98
|
+
it 'has' do
|
99
|
+
expect(subject.created_at).to be_nil
|
100
|
+
expect(subject.updated_at).to be_nil
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
context 'saving' do
|
105
|
+
subject do
|
106
|
+
i = super()
|
107
|
+
i.name = 'Bobo'
|
108
|
+
i.save
|
109
|
+
i
|
110
|
+
end
|
111
|
+
|
112
|
+
it 'does not error' do
|
113
|
+
expect { subject }.not_to raise_error
|
114
|
+
end
|
115
|
+
|
116
|
+
it 'sets name' do
|
117
|
+
expect(subject.name).to eq('Bobo')
|
118
|
+
end
|
119
|
+
|
120
|
+
it 'sets timestamps' do
|
121
|
+
expect(subject.created_at).not_to be_nil
|
122
|
+
expect(subject.updated_at).not_to be_nil
|
123
|
+
end
|
124
|
+
end
|
125
|
+
end
|
126
|
+
end
|
127
|
+
|
128
|
+
context 'designating type' do
|
129
|
+
subject do
|
130
|
+
described_class.generate(
|
131
|
+
table_name: table_name,
|
132
|
+
klass_namespaces: klass_namespaces,
|
133
|
+
klass_basename: klass_basename,
|
134
|
+
columns: columns,
|
135
|
+
indexes: indexes,
|
136
|
+
timestamps: timestamps,
|
137
|
+
connection_params: connection_params
|
138
|
+
)
|
139
|
+
end
|
140
|
+
|
141
|
+
let!(:farm_animal) do
|
142
|
+
module Farm
|
143
|
+
module Animal
|
144
|
+
end
|
145
|
+
end
|
146
|
+
end
|
147
|
+
let(:table_name) { 'dogs' }
|
148
|
+
let(:klass_namespaces) { %w[Farm Animal] }
|
149
|
+
let(:klass_basename) { 'my' }
|
150
|
+
let(:columns) { [{ name: 'name', type: 'string' }, { name: 'baked_at', type: 'time' }] }
|
151
|
+
let(:indexes) { [] }
|
152
|
+
let(:timestamps) { true }
|
153
|
+
let(:connection_params) { AnonymousActiveRecord::DEFAULT_CONNECTION_PARAMS }
|
154
|
+
|
155
|
+
it 'does not error' do
|
156
|
+
expect { subject }.not_to raise_error
|
157
|
+
end
|
158
|
+
|
159
|
+
context 'instance' do
|
160
|
+
subject { super().new }
|
161
|
+
|
162
|
+
it 'can be instantiated' do
|
163
|
+
expect(subject).to be_a(ActiveRecord::Base)
|
164
|
+
end
|
165
|
+
|
166
|
+
context 'name' do
|
167
|
+
it 'has' do
|
168
|
+
expect(subject.name).to be_nil
|
169
|
+
end
|
170
|
+
end
|
171
|
+
|
172
|
+
context 'baked_at' do
|
173
|
+
it 'has' do
|
174
|
+
expect(subject.baked_at).to be_nil
|
175
|
+
end
|
176
|
+
end
|
177
|
+
|
178
|
+
context 'timestamps' do
|
179
|
+
it 'has' do
|
180
|
+
expect(subject.created_at).to be_nil
|
181
|
+
expect(subject.updated_at).to be_nil
|
182
|
+
end
|
183
|
+
end
|
184
|
+
|
185
|
+
context 'saving' do
|
186
|
+
subject do
|
187
|
+
i = super()
|
188
|
+
i.name = 'Bobo'
|
189
|
+
i.baked_at = Time.now
|
190
|
+
i.save
|
191
|
+
i
|
192
|
+
end
|
193
|
+
|
194
|
+
it 'does not error' do
|
195
|
+
expect { subject }.not_to raise_error
|
196
|
+
end
|
197
|
+
|
198
|
+
it 'sets name' do
|
199
|
+
expect(subject.name).to eq('Bobo')
|
200
|
+
end
|
201
|
+
|
202
|
+
it 'sets baked_at' do
|
203
|
+
expect([ActiveRecord::Type::Time::Value, Time]).to include(subject.baked_at.class)
|
204
|
+
end
|
205
|
+
|
206
|
+
it 'sets timestamps' do
|
207
|
+
expect(subject.created_at).not_to be_nil
|
208
|
+
expect(subject.updated_at).not_to be_nil
|
209
|
+
end
|
210
|
+
end
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
context 'designating default' do
|
215
|
+
subject do
|
216
|
+
described_class.generate(
|
217
|
+
table_name: table_name,
|
218
|
+
klass_namespaces: klass_namespaces,
|
219
|
+
klass_basename: klass_basename,
|
220
|
+
columns: columns,
|
221
|
+
indexes: indexes,
|
222
|
+
timestamps: timestamps,
|
223
|
+
connection_params: connection_params
|
224
|
+
)
|
225
|
+
end
|
226
|
+
|
227
|
+
let!(:farm_animal) do
|
228
|
+
module Farm
|
229
|
+
module Animal
|
230
|
+
end
|
231
|
+
end
|
232
|
+
end
|
233
|
+
let(:table_name) { 'dogs' }
|
234
|
+
let(:klass_namespaces) { %w[Farm Animal] }
|
235
|
+
let(:klass_basename) { 'my' }
|
236
|
+
let(:columns) do
|
237
|
+
[{ name: 'name', type: 'string', default: 'Bird Man' }, { name: 'number', type: 'integer', default: 0 }]
|
238
|
+
end
|
239
|
+
let(:indexes) { [] }
|
240
|
+
let(:timestamps) { true }
|
241
|
+
let(:connection_params) { AnonymousActiveRecord::DEFAULT_CONNECTION_PARAMS }
|
242
|
+
|
243
|
+
it 'does not error' do
|
244
|
+
expect { subject }.not_to raise_error
|
245
|
+
end
|
246
|
+
|
247
|
+
context 'instance' do
|
248
|
+
subject { super().new }
|
249
|
+
|
250
|
+
it 'can be instantiated' do
|
251
|
+
expect(subject).to be_a(ActiveRecord::Base)
|
252
|
+
end
|
253
|
+
|
254
|
+
context 'name' do
|
255
|
+
it 'has' do
|
256
|
+
expect(subject.name).to eq('Bird Man')
|
257
|
+
end
|
258
|
+
end
|
259
|
+
|
260
|
+
context 'number' do
|
261
|
+
it 'has' do
|
262
|
+
expect(subject.number).to eq(0)
|
263
|
+
end
|
264
|
+
end
|
265
|
+
|
266
|
+
context 'timestamps' do
|
267
|
+
it 'has' do
|
268
|
+
expect(subject.created_at).to be_nil
|
269
|
+
expect(subject.updated_at).to be_nil
|
270
|
+
end
|
271
|
+
end
|
272
|
+
|
273
|
+
context 'saving' do
|
274
|
+
subject do
|
275
|
+
i = super()
|
276
|
+
i.name = 'Bobo'
|
277
|
+
i.number += 111
|
278
|
+
i.save
|
279
|
+
i
|
280
|
+
end
|
281
|
+
|
282
|
+
it 'does not error' do
|
283
|
+
expect { subject }.not_to raise_error
|
284
|
+
end
|
285
|
+
|
286
|
+
it 'sets name' do
|
287
|
+
expect(subject.name).to eq('Bobo')
|
288
|
+
end
|
289
|
+
|
290
|
+
it 'sets number' do
|
291
|
+
expect(subject.number).to eq(111)
|
292
|
+
end
|
293
|
+
|
294
|
+
it 'sets timestamps' do
|
295
|
+
expect(subject.created_at).not_to be_nil
|
296
|
+
expect(subject.updated_at).not_to be_nil
|
297
|
+
end
|
298
|
+
end
|
299
|
+
end
|
300
|
+
end
|
301
|
+
|
302
|
+
context 'with unique index as options' do
|
303
|
+
subject do
|
304
|
+
described_class.generate(
|
305
|
+
table_name: table_name,
|
306
|
+
klass_namespaces: klass_namespaces,
|
307
|
+
klass_basename: klass_basename,
|
308
|
+
columns: columns,
|
309
|
+
indexes: indexes,
|
310
|
+
timestamps: timestamps,
|
311
|
+
connection_params: connection_params
|
312
|
+
)
|
313
|
+
end
|
314
|
+
|
315
|
+
let!(:farm_animal) do
|
316
|
+
module Farm
|
317
|
+
module Animal
|
318
|
+
end
|
319
|
+
end
|
320
|
+
end
|
321
|
+
let(:table_name) { 'dogs' }
|
322
|
+
let(:klass_namespaces) { %w[Farm Animal] }
|
323
|
+
let(:klass_basename) { 'my' }
|
324
|
+
let(:columns) { [{ name: 'name', type: 'string' }, { name: 'baked_at', type: 'time' }] }
|
325
|
+
let(:indexes) { [{ columns: ['name'], unique: true }, { columns: ['baked_at'] }] }
|
326
|
+
let(:timestamps) { true }
|
327
|
+
let(:connection_params) { AnonymousActiveRecord::DEFAULT_CONNECTION_PARAMS }
|
328
|
+
|
329
|
+
it 'does not error' do
|
330
|
+
expect { subject }.not_to raise_error
|
331
|
+
end
|
332
|
+
|
333
|
+
context 'instance' do
|
334
|
+
subject { super().new }
|
335
|
+
|
336
|
+
it 'can be instantiated' do
|
337
|
+
expect(subject).to be_a(ActiveRecord::Base)
|
338
|
+
end
|
339
|
+
|
340
|
+
context 'name' do
|
341
|
+
it 'has' do
|
342
|
+
expect(subject.name).to be_nil
|
343
|
+
end
|
344
|
+
end
|
345
|
+
|
346
|
+
context 'baked_at' do
|
347
|
+
it 'has' do
|
348
|
+
expect(subject.baked_at).to be_nil
|
349
|
+
end
|
350
|
+
end
|
351
|
+
|
352
|
+
context 'timestamps' do
|
353
|
+
it 'has' do
|
354
|
+
expect(subject.created_at).to be_nil
|
355
|
+
expect(subject.updated_at).to be_nil
|
356
|
+
end
|
357
|
+
end
|
358
|
+
|
359
|
+
context 'saving' do
|
360
|
+
subject do
|
361
|
+
i = super()
|
362
|
+
i.name = 'Bobo'
|
363
|
+
i.baked_at = Time.now
|
364
|
+
i.save
|
365
|
+
i
|
366
|
+
end
|
367
|
+
|
368
|
+
it 'does not error' do
|
369
|
+
expect { subject }.not_to raise_error
|
370
|
+
end
|
371
|
+
|
372
|
+
it 'sets name' do
|
373
|
+
expect(subject.name).to eq('Bobo')
|
374
|
+
end
|
375
|
+
|
376
|
+
it 'sets baked_at' do
|
377
|
+
expect([ActiveRecord::Type::Time::Value, Time]).to include(subject.baked_at.class)
|
378
|
+
end
|
379
|
+
|
380
|
+
it 'sets timestamps' do
|
381
|
+
expect(subject.created_at).not_to be_nil
|
382
|
+
expect(subject.updated_at).not_to be_nil
|
383
|
+
end
|
384
|
+
end
|
385
|
+
|
386
|
+
context 'uniqueness enforced by index' do
|
387
|
+
subject do
|
388
|
+
i = model.new
|
389
|
+
i.name = 'Bobo'
|
390
|
+
i.baked_at = Time.now
|
391
|
+
i.save!
|
392
|
+
end
|
393
|
+
|
394
|
+
let(:model) do
|
395
|
+
described_class.generate(
|
396
|
+
table_name: table_name,
|
397
|
+
klass_namespaces: klass_namespaces,
|
398
|
+
klass_basename: klass_basename,
|
399
|
+
columns: columns,
|
400
|
+
indexes: indexes,
|
401
|
+
timestamps: timestamps,
|
402
|
+
connection_params: connection_params
|
403
|
+
)
|
404
|
+
end
|
405
|
+
|
406
|
+
before do
|
407
|
+
i = model.new
|
408
|
+
i.name = 'Bobo'
|
409
|
+
i.baked_at = Time.now
|
410
|
+
i.save
|
411
|
+
end
|
412
|
+
|
413
|
+
it 'raises error' do
|
414
|
+
block_is_expected.to raise_error(ActiveRecord::RecordNotUnique)
|
415
|
+
end
|
416
|
+
end
|
417
|
+
end
|
418
|
+
end
|
419
|
+
|
420
|
+
context 'with unique index as implicit' do
|
421
|
+
subject do
|
422
|
+
described_class.generate(
|
423
|
+
table_name: table_name,
|
424
|
+
klass_namespaces: klass_namespaces,
|
425
|
+
klass_basename: klass_basename,
|
426
|
+
columns: columns,
|
427
|
+
indexes: indexes,
|
428
|
+
timestamps: timestamps,
|
429
|
+
connection_params: connection_params
|
430
|
+
)
|
431
|
+
end
|
432
|
+
|
433
|
+
let!(:farm_animal) do
|
434
|
+
module Farm
|
435
|
+
module Animal
|
436
|
+
end
|
437
|
+
end
|
438
|
+
end
|
439
|
+
let(:table_name) { 'dogs' }
|
440
|
+
let(:klass_namespaces) { %w[Farm Animal] }
|
441
|
+
let(:klass_basename) { 'my' }
|
442
|
+
let(:columns) { [{ name: 'name', type: 'string' }, { name: 'baked_at', type: 'time' }] }
|
443
|
+
let(:indexes) { [[['name'], { unique: true }], 'baked_at'] }
|
444
|
+
let(:timestamps) { true }
|
445
|
+
let(:connection_params) { AnonymousActiveRecord::DEFAULT_CONNECTION_PARAMS }
|
446
|
+
|
447
|
+
it 'does not error' do
|
448
|
+
expect { subject }.not_to raise_error
|
449
|
+
end
|
450
|
+
|
451
|
+
context 'instance' do
|
452
|
+
subject { super().new }
|
453
|
+
|
454
|
+
it 'can be instantiated' do
|
455
|
+
expect(subject).to be_a(ActiveRecord::Base)
|
456
|
+
end
|
457
|
+
|
458
|
+
context 'name' do
|
459
|
+
it 'has' do
|
460
|
+
expect(subject.name).to be_nil
|
461
|
+
end
|
462
|
+
end
|
463
|
+
|
464
|
+
context 'baked_at' do
|
465
|
+
it 'has' do
|
466
|
+
expect(subject.baked_at).to be_nil
|
467
|
+
end
|
468
|
+
end
|
469
|
+
|
470
|
+
context 'timestamps' do
|
471
|
+
it 'has' do
|
472
|
+
expect(subject.created_at).to be_nil
|
473
|
+
expect(subject.updated_at).to be_nil
|
474
|
+
end
|
475
|
+
end
|
476
|
+
|
477
|
+
context 'saving' do
|
478
|
+
subject do
|
479
|
+
i = super()
|
480
|
+
i.name = 'Bobo'
|
481
|
+
i.baked_at = Time.now
|
482
|
+
i.save
|
483
|
+
i
|
484
|
+
end
|
485
|
+
|
486
|
+
it 'does not error' do
|
487
|
+
expect { subject }.not_to raise_error
|
488
|
+
end
|
489
|
+
|
490
|
+
it 'sets name' do
|
491
|
+
expect(subject.name).to eq('Bobo')
|
492
|
+
end
|
493
|
+
|
494
|
+
it 'sets baked_at' do
|
495
|
+
expect([ActiveRecord::Type::Time::Value, Time]).to include(subject.baked_at.class)
|
496
|
+
end
|
497
|
+
|
498
|
+
it 'sets timestamps' do
|
499
|
+
expect(subject.created_at).not_to be_nil
|
500
|
+
expect(subject.updated_at).not_to be_nil
|
501
|
+
end
|
502
|
+
end
|
503
|
+
|
504
|
+
context 'uniqueness enforced by index' do
|
505
|
+
subject do
|
506
|
+
i = model.new
|
507
|
+
i.name = 'Bobo'
|
508
|
+
i.baked_at = Time.now
|
509
|
+
i.save!
|
510
|
+
end
|
511
|
+
|
512
|
+
let(:model) do
|
513
|
+
described_class.generate(
|
514
|
+
table_name: table_name,
|
515
|
+
klass_namespaces: klass_namespaces,
|
516
|
+
klass_basename: klass_basename,
|
517
|
+
columns: columns,
|
518
|
+
indexes: indexes,
|
519
|
+
timestamps: timestamps,
|
520
|
+
connection_params: connection_params
|
521
|
+
)
|
522
|
+
end
|
523
|
+
|
524
|
+
before do
|
525
|
+
i = model.new
|
526
|
+
i.name = 'Bobo'
|
527
|
+
i.baked_at = Time.now
|
528
|
+
i.save
|
529
|
+
end
|
530
|
+
|
531
|
+
it 'raises error' do
|
532
|
+
block_is_expected.to raise_error(ActiveRecord::RecordNotUnique)
|
533
|
+
end
|
534
|
+
end
|
535
|
+
end
|
536
|
+
end
|
537
|
+
|
538
|
+
context 'no timestamps' do
|
539
|
+
subject do
|
540
|
+
described_class.generate(
|
541
|
+
table_name: table_name,
|
542
|
+
klass_basename: klass_basename,
|
543
|
+
columns: columns,
|
544
|
+
timestamps: timestamps,
|
545
|
+
connection_params: connection_params
|
546
|
+
)
|
547
|
+
end
|
548
|
+
|
549
|
+
let(:table_name) { 'dogs' }
|
550
|
+
let(:klass_basename) { 'my' }
|
551
|
+
let(:columns) { ['name'] }
|
552
|
+
let(:timestamps) { false }
|
553
|
+
let(:connection_params) { AnonymousActiveRecord::DEFAULT_CONNECTION_PARAMS }
|
554
|
+
|
555
|
+
it 'does not error' do
|
556
|
+
expect { subject }.not_to raise_error
|
557
|
+
end
|
558
|
+
|
559
|
+
context 'instance' do
|
560
|
+
subject { super().new }
|
561
|
+
|
562
|
+
it 'can be instantiated' do
|
563
|
+
expect(subject).to be_a(ActiveRecord::Base)
|
564
|
+
end
|
565
|
+
|
566
|
+
context 'timestamps' do
|
567
|
+
it 'has not' do
|
568
|
+
expect(subject).not_to respond_to(:created_at)
|
569
|
+
expect(subject).not_to respond_to(:updated_at)
|
570
|
+
end
|
571
|
+
end
|
572
|
+
|
573
|
+
context 'saving' do
|
574
|
+
subject do
|
575
|
+
i = super()
|
576
|
+
i.name = 'Bobo'
|
577
|
+
i.save
|
578
|
+
i
|
579
|
+
end
|
580
|
+
|
581
|
+
it 'does not error' do
|
582
|
+
expect { subject }.not_to raise_error
|
583
|
+
end
|
584
|
+
|
585
|
+
it 'sets name' do
|
586
|
+
expect(subject.name).to eq('Bobo')
|
587
|
+
end
|
588
|
+
|
589
|
+
it 'has no timestamps' do
|
590
|
+
expect(subject).not_to respond_to(:created_at)
|
591
|
+
expect(subject).not_to respond_to(:updated_at)
|
592
|
+
end
|
593
|
+
end
|
594
|
+
end
|
595
|
+
end
|
596
|
+
|
597
|
+
context 'with block' do
|
598
|
+
subject do
|
599
|
+
described_class.generate(
|
600
|
+
table_name: table_name,
|
601
|
+
klass_basename: klass_basename,
|
602
|
+
columns: columns,
|
603
|
+
timestamps: timestamps,
|
604
|
+
connection_params: connection_params
|
605
|
+
) do
|
606
|
+
def eat_pie
|
607
|
+
'eating'
|
608
|
+
end
|
609
|
+
|
610
|
+
def flowery_name
|
611
|
+
"🌸#{name}🌸"
|
612
|
+
end
|
613
|
+
end
|
614
|
+
end
|
615
|
+
|
616
|
+
let(:table_name) { 'dogs' }
|
617
|
+
let(:klass_basename) { 'my' }
|
618
|
+
let(:columns) { ['name'] }
|
619
|
+
let(:timestamps) { false }
|
620
|
+
let(:connection_params) { AnonymousActiveRecord::DEFAULT_CONNECTION_PARAMS }
|
621
|
+
|
622
|
+
it 'does not error' do
|
623
|
+
expect { subject }.not_to raise_error
|
624
|
+
end
|
625
|
+
|
626
|
+
context 'instance' do
|
627
|
+
subject { super().new(name: 'Marty McFly') }
|
628
|
+
|
629
|
+
it 'can be instantiated' do
|
630
|
+
expect(subject).to be_a(ActiveRecord::Base)
|
631
|
+
end
|
632
|
+
|
633
|
+
context 'block' do
|
634
|
+
it 'defines method' do
|
635
|
+
expect(subject.eat_pie).to eq('eating')
|
636
|
+
end
|
637
|
+
|
638
|
+
it 'has access to class context' do
|
639
|
+
expect(subject.flowery_name).to eq('🌸Marty McFly🌸')
|
640
|
+
end
|
641
|
+
end
|
642
|
+
|
643
|
+
context 'timestamps' do
|
644
|
+
it 'has not' do
|
645
|
+
expect(subject).not_to respond_to(:created_at)
|
646
|
+
expect(subject).not_to respond_to(:updated_at)
|
647
|
+
end
|
648
|
+
end
|
649
|
+
|
650
|
+
context 'saving' do
|
651
|
+
subject do
|
652
|
+
i = super()
|
653
|
+
i.name = 'Bobo'
|
654
|
+
i.save
|
655
|
+
i
|
656
|
+
end
|
657
|
+
|
658
|
+
it 'does not error' do
|
659
|
+
expect { subject }.not_to raise_error
|
660
|
+
end
|
661
|
+
|
662
|
+
it 'sets name' do
|
663
|
+
expect(subject.name).to eq('Bobo')
|
664
|
+
end
|
665
|
+
|
666
|
+
it 'has access to class context' do
|
667
|
+
expect(subject.flowery_name).to eq('🌸Bobo🌸')
|
668
|
+
end
|
669
|
+
|
670
|
+
it 'has no timestamps' do
|
671
|
+
expect(subject).not_to respond_to(:created_at)
|
672
|
+
expect(subject).not_to respond_to(:updated_at)
|
673
|
+
end
|
674
|
+
end
|
675
|
+
end
|
676
|
+
end
|
677
|
+
|
678
|
+
context 'testing a module' do
|
679
|
+
let!(:has_balloon) do
|
680
|
+
module HasBalloon
|
681
|
+
def has_balloon?
|
682
|
+
name == 'Spot' # only Spot has a balloon
|
683
|
+
end
|
684
|
+
end
|
685
|
+
end
|
686
|
+
let(:ar_with_balloon) do
|
687
|
+
described_class.generate(columns: ['name']) do
|
688
|
+
include HasBalloon
|
689
|
+
def flowery_name
|
690
|
+
"#{b_f}#{name}#{b_f}"
|
691
|
+
end
|
692
|
+
|
693
|
+
def b_f
|
694
|
+
has_balloon? ? '🎈' : '🌸'
|
695
|
+
end
|
696
|
+
end
|
697
|
+
end
|
698
|
+
|
699
|
+
it 'can test the module' do
|
700
|
+
expect(ar_with_balloon.new(name: 'Spot').flowery_name).to eq('🎈Spot🎈')
|
701
|
+
expect(ar_with_balloon.new(name: 'Not Spot').flowery_name).to eq('🌸Not Spot🌸')
|
702
|
+
end
|
703
|
+
end
|
704
|
+
end
|
705
|
+
|
706
|
+
describe '.factory' do
|
707
|
+
context 'minimal params' do
|
708
|
+
context 'returns array' do
|
709
|
+
subject { described_class.factory }
|
710
|
+
|
711
|
+
it 'be an array' do
|
712
|
+
expect(subject).to be_a(Array)
|
713
|
+
end
|
714
|
+
|
715
|
+
it 'has length 0' do
|
716
|
+
expect(subject.length).to eq(0)
|
717
|
+
end
|
718
|
+
end
|
719
|
+
end
|
720
|
+
|
721
|
+
context 'all params' do
|
722
|
+
subject do
|
723
|
+
described_class.factory(
|
724
|
+
source_data: source_data,
|
725
|
+
table_name: table_name,
|
726
|
+
klass_namespaces: klass_namespaces,
|
727
|
+
klass_basename: klass_basename,
|
728
|
+
columns: columns,
|
729
|
+
indexes: indexes,
|
730
|
+
timestamps: timestamps,
|
731
|
+
connection_params: connection_params
|
732
|
+
)
|
733
|
+
end
|
734
|
+
|
735
|
+
let!(:farm_animal) do
|
736
|
+
module Zoo
|
737
|
+
module Animal
|
738
|
+
end
|
739
|
+
end
|
740
|
+
end
|
741
|
+
let(:table_name) { 'dogs' }
|
742
|
+
let(:klass_namespaces) { %w[Zoo Animal] }
|
743
|
+
let(:klass_basename) { 'my' }
|
744
|
+
let(:columns) { ['name'] }
|
745
|
+
let(:indexes) { [{ columns: ['name'] }] }
|
746
|
+
let(:timestamps) { true }
|
747
|
+
let(:source_data) { [{ name: 'Gru Banksy' }, { name: 'Herlina Termalina' }] }
|
748
|
+
let(:connection_params) { AnonymousActiveRecord::DEFAULT_CONNECTION_PARAMS }
|
749
|
+
|
750
|
+
context 'returns array' do
|
751
|
+
it 'be an array' do
|
752
|
+
expect(subject).to be_a(Array)
|
753
|
+
end
|
754
|
+
|
755
|
+
it 'has length 2' do
|
756
|
+
expect(subject.length).to eq(2)
|
757
|
+
end
|
758
|
+
end
|
759
|
+
|
760
|
+
context 'sets attributes' do
|
761
|
+
subject { super().map(&:name) }
|
762
|
+
|
763
|
+
it 'be an array' do
|
764
|
+
expect(subject).to eq(['Gru Banksy', 'Herlina Termalina'])
|
765
|
+
end
|
766
|
+
end
|
767
|
+
end
|
768
|
+
|
769
|
+
context 'no timestamps' do
|
770
|
+
subject do
|
771
|
+
described_class.factory(
|
772
|
+
source_data: source_data,
|
773
|
+
table_name: table_name,
|
774
|
+
klass_basename: klass_basename,
|
775
|
+
columns: columns,
|
776
|
+
indexes: indexes,
|
777
|
+
timestamps: timestamps,
|
778
|
+
connection_params: connection_params
|
779
|
+
)
|
780
|
+
end
|
781
|
+
|
782
|
+
let(:table_name) { 'dogs' }
|
783
|
+
let(:klass_basename) { 'my' }
|
784
|
+
let(:columns) { ['name'] }
|
785
|
+
let(:indexes) { [{ columns: ['name'] }] }
|
786
|
+
let(:timestamps) { false }
|
787
|
+
let(:source_data) { [{ name: 'Gru Banksy' }, { name: 'Herlina Termalina' }] }
|
788
|
+
let(:connection_params) { AnonymousActiveRecord::DEFAULT_CONNECTION_PARAMS }
|
789
|
+
|
790
|
+
context 'returns array' do
|
791
|
+
it 'be an array' do
|
792
|
+
expect(subject).to be_a(Array)
|
793
|
+
end
|
794
|
+
|
795
|
+
it 'has length 2' do
|
796
|
+
expect(subject.length).to eq(2)
|
797
|
+
end
|
798
|
+
end
|
799
|
+
|
800
|
+
context 'does not have timestamps' do
|
801
|
+
subject { super().map { |anon| anon.respond_to?(:created_at) } }
|
802
|
+
|
803
|
+
it 'be an array' do
|
804
|
+
expect(subject).to eq([false, false])
|
805
|
+
end
|
806
|
+
end
|
807
|
+
end
|
808
|
+
|
809
|
+
context 'with block' do
|
810
|
+
subject do
|
811
|
+
described_class.factory(
|
812
|
+
source_data: source_data,
|
813
|
+
table_name: table_name,
|
814
|
+
klass_basename: klass_basename,
|
815
|
+
columns: columns,
|
816
|
+
indexes: indexes,
|
817
|
+
timestamps: timestamps,
|
818
|
+
connection_params: connection_params
|
819
|
+
) do
|
820
|
+
def eat_pie
|
821
|
+
'eating'
|
822
|
+
end
|
823
|
+
|
824
|
+
def flowery_name
|
825
|
+
"🌸#{name}🌸"
|
826
|
+
end
|
827
|
+
end
|
828
|
+
end
|
829
|
+
|
830
|
+
let(:table_name) { 'dogs' }
|
831
|
+
let(:klass_basename) { 'my' }
|
832
|
+
let(:columns) { ['name'] }
|
833
|
+
let(:indexes) { [{ columns: ['name'] }] }
|
834
|
+
let(:timestamps) { false }
|
835
|
+
let(:source_data) { [{ name: 'Gru Banksy' }, { name: 'Herlina Termalina' }] }
|
836
|
+
let(:connection_params) { AnonymousActiveRecord::DEFAULT_CONNECTION_PARAMS }
|
837
|
+
|
838
|
+
context 'returns array' do
|
839
|
+
it 'be an array' do
|
840
|
+
expect(subject).to be_a(Array)
|
841
|
+
end
|
842
|
+
|
843
|
+
it 'has length 2' do
|
844
|
+
expect(subject.length).to eq(2)
|
845
|
+
end
|
846
|
+
end
|
847
|
+
|
848
|
+
context 'defines method' do
|
849
|
+
subject { super().map(&:eat_pie) }
|
850
|
+
|
851
|
+
it 'defines method' do
|
852
|
+
expect(subject).to eq(%w[eating eating])
|
853
|
+
end
|
854
|
+
end
|
855
|
+
|
856
|
+
context 'sets attributes' do
|
857
|
+
subject { super().map(&:flowery_name) }
|
858
|
+
|
859
|
+
it 'be an array' do
|
860
|
+
expect(subject).to eq(['🌸Gru Banksy🌸', '🌸Herlina Termalina🌸'])
|
861
|
+
end
|
862
|
+
end
|
863
|
+
end
|
864
|
+
end
|
865
|
+
|
866
|
+
describe '.factory!' do
|
867
|
+
context 'minimal params' do
|
868
|
+
context 'returns array' do
|
869
|
+
subject { described_class.factory! }
|
870
|
+
|
871
|
+
it 'be an array' do
|
872
|
+
expect(subject).to be_a(Array)
|
873
|
+
end
|
874
|
+
|
875
|
+
it 'has length 0' do
|
876
|
+
expect(subject.length).to eq(0)
|
877
|
+
end
|
878
|
+
end
|
879
|
+
end
|
880
|
+
|
881
|
+
context 'all params' do
|
882
|
+
subject do
|
883
|
+
described_class.factory!(
|
884
|
+
source_data: source_data,
|
885
|
+
table_name: table_name,
|
886
|
+
klass_namespaces: klass_namespaces,
|
887
|
+
klass_basename: klass_basename,
|
888
|
+
columns: columns,
|
889
|
+
indexes: indexes,
|
890
|
+
timestamps: timestamps,
|
891
|
+
connection_params: connection_params
|
892
|
+
)
|
893
|
+
end
|
894
|
+
|
895
|
+
let!(:farm_animal) do
|
896
|
+
module Zoo
|
897
|
+
module Animal
|
898
|
+
end
|
899
|
+
end
|
900
|
+
end
|
901
|
+
let(:table_name) { 'dogs' }
|
902
|
+
let(:klass_namespaces) { %w[Zoo Animal] }
|
903
|
+
let(:klass_basename) { 'my' }
|
904
|
+
let(:columns) { ['name'] }
|
905
|
+
let(:indexes) { [{ columns: ['name'] }] }
|
906
|
+
let(:timestamps) { true }
|
907
|
+
let(:source_data) { [{ name: 'Gru Banksy' }, { name: 'Herlina Termalina' }] }
|
908
|
+
let(:connection_params) { AnonymousActiveRecord::DEFAULT_CONNECTION_PARAMS }
|
909
|
+
|
910
|
+
context 'returns array' do
|
911
|
+
it 'be an array' do
|
912
|
+
expect(subject).to be_a(Array)
|
913
|
+
end
|
914
|
+
|
915
|
+
it 'has length 2' do
|
916
|
+
expect(subject.length).to eq(2)
|
917
|
+
end
|
918
|
+
end
|
919
|
+
|
920
|
+
context 'sets attributes' do
|
921
|
+
subject { super().map(&:name) }
|
922
|
+
|
923
|
+
it 'be an array' do
|
924
|
+
expect(subject).to eq(['Gru Banksy', 'Herlina Termalina'])
|
925
|
+
end
|
926
|
+
end
|
927
|
+
end
|
928
|
+
|
929
|
+
context 'no timestamps' do
|
930
|
+
subject do
|
931
|
+
described_class.factory!(
|
932
|
+
source_data: source_data,
|
933
|
+
table_name: table_name,
|
934
|
+
klass_basename: klass_basename,
|
935
|
+
columns: columns,
|
936
|
+
indexes: indexes,
|
937
|
+
timestamps: timestamps,
|
938
|
+
connection_params: connection_params
|
939
|
+
)
|
940
|
+
end
|
941
|
+
|
942
|
+
let(:table_name) { 'dogs' }
|
943
|
+
let(:klass_basename) { 'my' }
|
944
|
+
let(:columns) { ['name'] }
|
945
|
+
let(:indexes) { [{ columns: ['name'] }] }
|
946
|
+
let(:timestamps) { false }
|
947
|
+
let(:source_data) { [{ name: 'Gru Banksy' }, { name: 'Herlina Termalina' }] }
|
948
|
+
let(:connection_params) { AnonymousActiveRecord::DEFAULT_CONNECTION_PARAMS }
|
949
|
+
|
950
|
+
context 'returns array' do
|
951
|
+
it 'be an array' do
|
952
|
+
expect(subject).to be_a(Array)
|
953
|
+
end
|
954
|
+
|
955
|
+
it 'has length 2' do
|
956
|
+
expect(subject.length).to eq(2)
|
957
|
+
end
|
958
|
+
end
|
959
|
+
|
960
|
+
context 'does not have timestamps' do
|
961
|
+
subject { super().map { |anon| anon.respond_to?(:created_at) } }
|
962
|
+
|
963
|
+
it 'be an array' do
|
964
|
+
expect(subject).to eq([false, false])
|
965
|
+
end
|
966
|
+
end
|
967
|
+
end
|
968
|
+
|
969
|
+
context 'with block' do
|
970
|
+
subject do
|
971
|
+
described_class.factory!(
|
972
|
+
source_data: source_data,
|
973
|
+
table_name: table_name,
|
974
|
+
klass_basename: klass_basename,
|
975
|
+
columns: columns,
|
976
|
+
indexes: indexes,
|
977
|
+
timestamps: timestamps,
|
978
|
+
connection_params: connection_params
|
979
|
+
) do
|
980
|
+
def eat_pie
|
981
|
+
'eating'
|
982
|
+
end
|
983
|
+
|
984
|
+
def flowery_name
|
985
|
+
"🌸#{name}🌸"
|
986
|
+
end
|
987
|
+
end
|
988
|
+
end
|
989
|
+
|
990
|
+
let(:table_name) { 'dogs' }
|
991
|
+
let(:klass_basename) { 'my' }
|
992
|
+
let(:columns) { ['name'] }
|
993
|
+
let(:indexes) { [{ columns: ['name'] }] }
|
994
|
+
let(:timestamps) { false }
|
995
|
+
let(:source_data) { [{ name: 'Gru Banksy' }, { name: 'Herlina Termalina' }] }
|
996
|
+
let(:connection_params) { AnonymousActiveRecord::DEFAULT_CONNECTION_PARAMS }
|
997
|
+
|
998
|
+
context 'returns array' do
|
999
|
+
it 'be an array' do
|
1000
|
+
expect(subject).to be_a(Array)
|
1001
|
+
end
|
1002
|
+
|
1003
|
+
it 'has length 2' do
|
1004
|
+
expect(subject.length).to eq(2)
|
1005
|
+
end
|
1006
|
+
end
|
1007
|
+
|
1008
|
+
context 'defines method' do
|
1009
|
+
subject { super().map(&:eat_pie) }
|
1010
|
+
|
1011
|
+
it 'defines method' do
|
1012
|
+
expect(subject).to eq(%w[eating eating])
|
1013
|
+
end
|
1014
|
+
end
|
1015
|
+
|
1016
|
+
context 'sets attributes' do
|
1017
|
+
subject { super().map(&:flowery_name) }
|
1018
|
+
|
1019
|
+
it 'be an array' do
|
1020
|
+
expect(subject).to eq(['🌸Gru Banksy🌸', '🌸Herlina Termalina🌸'])
|
1021
|
+
end
|
1022
|
+
end
|
1023
|
+
end
|
1024
|
+
end
|
1025
|
+
end
|