active_type 0.6.0 → 0.6.1
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 +4 -4
- data/README.md +21 -0
- data/active_type.gemspec +1 -1
- data/gemfiles/Gemfile.3.2.mysql2.lock +1 -1
- data/gemfiles/Gemfile.3.2.sqlite3.lock +1 -1
- data/gemfiles/Gemfile.4.0.sqlite3.lock +1 -1
- data/gemfiles/Gemfile.4.1.sqlite3.lock +1 -1
- data/gemfiles/Gemfile.4.2.1.mysql2.lock +1 -1
- data/gemfiles/Gemfile.4.2.1.pg.lock +1 -1
- data/gemfiles/Gemfile.4.2.1.sqlite3.lock +1 -1
- data/gemfiles/Gemfile.5.0.0.mysql2.lock +1 -1
- data/gemfiles/Gemfile.5.0.0.pg.lock +1 -1
- data/gemfiles/Gemfile.5.0.0.sqlite3.lock +1 -1
- data/lib/active_type/version.rb +1 -1
- metadata +3 -53
- data/spec/active_type/extended_record/single_table_inheritance_spec.rb +0 -62
- data/spec/active_type/extended_record_spec.rb +0 -233
- data/spec/active_type/nested_attributes_spec.rb +0 -704
- data/spec/active_type/object_spec.rb +0 -463
- data/spec/active_type/record_spec.rb +0 -274
- data/spec/active_type/util_spec.rb +0 -142
- data/spec/integration/holidays_spec.rb +0 -102
- data/spec/integration/shape_spec.rb +0 -110
- data/spec/integration/sign_in_spec.rb +0 -101
- data/spec/integration/sign_up_spec.rb +0 -102
- data/spec/shared_examples/accessors.rb +0 -24
- data/spec/shared_examples/belongs_to.rb +0 -17
- data/spec/shared_examples/coercible_columns.rb +0 -248
- data/spec/shared_examples/constructor.rb +0 -30
- data/spec/shared_examples/defaults.rb +0 -60
- data/spec/shared_examples/dirty_tracking.rb +0 -40
- data/spec/shared_examples/dupable.rb +0 -31
- data/spec/shared_examples/mass_assignment.rb +0 -26
- data/spec/spec_helper.rb +0 -28
- data/spec/support/database.rb +0 -53
- data/spec/support/database.sample.yml +0 -3
- data/spec/support/error_on.rb +0 -12
- data/spec/support/i18n.rb +0 -1
- data/spec/support/protected_params.rb +0 -20
- data/spec/support/time_zone.rb +0 -1
@@ -1,463 +0,0 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
require 'ostruct'
|
3
|
-
|
4
|
-
module ObjectSpec
|
5
|
-
|
6
|
-
def self.type
|
7
|
-
if ActiveRecord::VERSION::MAJOR >= 5
|
8
|
-
@type ||= ActiveModel::Type::Value.new
|
9
|
-
end
|
10
|
-
end
|
11
|
-
|
12
|
-
class Object < ActiveType::Object
|
13
|
-
|
14
|
-
attribute :virtual_string, :string
|
15
|
-
attribute :virtual_integer, :integer
|
16
|
-
attribute :virtual_time, :datetime
|
17
|
-
attribute :virtual_date, :date
|
18
|
-
attribute :virtual_boolean, :boolean
|
19
|
-
attribute :virtual_attribute
|
20
|
-
attribute :virtual_type_attribute, ObjectSpec.type
|
21
|
-
|
22
|
-
end
|
23
|
-
|
24
|
-
class PlainObject < ActiveType::Object
|
25
|
-
end
|
26
|
-
|
27
|
-
|
28
|
-
class ObjectWithValidations < Object
|
29
|
-
|
30
|
-
validates :virtual_string, :presence => true
|
31
|
-
validates :virtual_boolean, :presence => true
|
32
|
-
|
33
|
-
end
|
34
|
-
|
35
|
-
|
36
|
-
class ObjectWithOverrides < Object
|
37
|
-
|
38
|
-
attribute :overridable_test, :string
|
39
|
-
|
40
|
-
def overridable_test
|
41
|
-
super + super
|
42
|
-
end
|
43
|
-
|
44
|
-
end
|
45
|
-
|
46
|
-
|
47
|
-
class InheritingObject < Object
|
48
|
-
attribute :another_virtual_string, :string
|
49
|
-
end
|
50
|
-
|
51
|
-
|
52
|
-
class IncludingObject < Object
|
53
|
-
|
54
|
-
module Module
|
55
|
-
extend ActiveSupport::Concern
|
56
|
-
|
57
|
-
included do
|
58
|
-
attribute :another_virtual_string, :string
|
59
|
-
end
|
60
|
-
end
|
61
|
-
|
62
|
-
include Module
|
63
|
-
end
|
64
|
-
|
65
|
-
|
66
|
-
class ObjectWithCallbacks < Object
|
67
|
-
|
68
|
-
before_save :before_save_callback
|
69
|
-
before_validation :before_validation_callback
|
70
|
-
after_save :after_save_callback
|
71
|
-
after_commit :after_commit_callback
|
72
|
-
after_rollback :after_rollback_callback
|
73
|
-
|
74
|
-
def before_save_callback
|
75
|
-
end
|
76
|
-
|
77
|
-
def before_validation_callback
|
78
|
-
end
|
79
|
-
|
80
|
-
def after_save_callback
|
81
|
-
end
|
82
|
-
|
83
|
-
def after_commit_callback
|
84
|
-
end
|
85
|
-
|
86
|
-
def after_rollback_callback
|
87
|
-
end
|
88
|
-
|
89
|
-
end
|
90
|
-
|
91
|
-
class Child < ActiveRecord::Base
|
92
|
-
end
|
93
|
-
|
94
|
-
class ObjectWithBelongsTo < Object
|
95
|
-
|
96
|
-
attribute :child_id, :integer
|
97
|
-
|
98
|
-
belongs_to :child
|
99
|
-
|
100
|
-
end
|
101
|
-
|
102
|
-
class ObjectWithUnsupportedTypes < Object
|
103
|
-
attribute :virtual_array, :array
|
104
|
-
attribute :virtual_hash, :hash
|
105
|
-
end
|
106
|
-
|
107
|
-
end
|
108
|
-
|
109
|
-
|
110
|
-
describe ActiveType::Object do
|
111
|
-
|
112
|
-
subject { ObjectSpec::Object.new }
|
113
|
-
|
114
|
-
describe 'constructors' do
|
115
|
-
subject { ObjectSpec::Object }
|
116
|
-
|
117
|
-
it_should_behave_like 'ActiveRecord-like constructors', { :virtual_string => "string", :virtual_integer => 100, :virtual_time => Time.now, :virtual_date => Date.today, :virtual_boolean => true }
|
118
|
-
|
119
|
-
end
|
120
|
-
|
121
|
-
describe 'mass assignment' do
|
122
|
-
it_should_behave_like 'ActiveRecord-like mass assignment', { :virtual_string => "string", :virtual_integer => 100, :virtual_time => Time.now, :virtual_date => Date.today, :virtual_boolean => true }
|
123
|
-
end
|
124
|
-
|
125
|
-
describe 'accessors' do
|
126
|
-
it_should_behave_like 'ActiveRecord-like accessors', { :virtual_string => "string", :virtual_integer => 100, :virtual_time => Time.now, :virtual_date => Date.today, :virtual_boolean => true }
|
127
|
-
end
|
128
|
-
|
129
|
-
describe 'unsupported types' do
|
130
|
-
subject { ObjectSpec::ObjectWithUnsupportedTypes.new }
|
131
|
-
|
132
|
-
it_should_behave_like 'ActiveRecord-like mass assignment', { :virtual_hash => {'foo' => 'bar'}, :virtual_array => ['foo', 'bar'] }
|
133
|
-
it_should_behave_like 'ActiveRecord-like accessors', { :virtual_hash => {'foo' => 'bar'}, :virtual_array => ['foo', 'bar'] }
|
134
|
-
end
|
135
|
-
|
136
|
-
describe 'overridable attributes' do
|
137
|
-
subject { ObjectSpec::ObjectWithOverrides.new }
|
138
|
-
|
139
|
-
it 'is possible to override attributes with super' do
|
140
|
-
subject.overridable_test = "test"
|
141
|
-
|
142
|
-
expect(subject.overridable_test).to eq("testtest")
|
143
|
-
end
|
144
|
-
end
|
145
|
-
|
146
|
-
describe 'attribute name validation' do
|
147
|
-
it 'crashes when trying to define an invalid attribute name' do
|
148
|
-
klass = Class.new(ActiveType::Object)
|
149
|
-
expect {
|
150
|
-
klass.class_eval do
|
151
|
-
attribute :"<attr>", :string
|
152
|
-
end
|
153
|
-
}.to raise_error(ActiveType::InvalidAttributeNameError)
|
154
|
-
end
|
155
|
-
end
|
156
|
-
|
157
|
-
context 'coercible' do
|
158
|
-
describe 'string columns' do
|
159
|
-
it_should_behave_like 'a coercible string column', :virtual_string
|
160
|
-
end
|
161
|
-
|
162
|
-
describe 'integer columns' do
|
163
|
-
it_should_behave_like 'a coercible integer column', :virtual_integer
|
164
|
-
end
|
165
|
-
|
166
|
-
describe 'date columns' do
|
167
|
-
it_should_behave_like 'a coercible date column', :virtual_date
|
168
|
-
end
|
169
|
-
|
170
|
-
describe 'time columns' do
|
171
|
-
it_should_behave_like 'a coercible time column', :virtual_time
|
172
|
-
end
|
173
|
-
|
174
|
-
describe 'boolean columns' do
|
175
|
-
it_should_behave_like 'a coercible boolean column', :virtual_boolean
|
176
|
-
end
|
177
|
-
|
178
|
-
describe 'untyped columns' do
|
179
|
-
it_should_behave_like 'an untyped column', :virtual_attribute
|
180
|
-
end
|
181
|
-
|
182
|
-
describe 'type columns' do
|
183
|
-
it_should_behave_like 'a coercible type column', :virtual_type_attribute, ObjectSpec.type
|
184
|
-
end
|
185
|
-
end
|
186
|
-
|
187
|
-
describe 'query methods' do
|
188
|
-
|
189
|
-
it 'returns true for true' do
|
190
|
-
subject.virtual_attribute = true
|
191
|
-
|
192
|
-
expect(subject.virtual_attribute?).to eq(true)
|
193
|
-
end
|
194
|
-
|
195
|
-
it 'returns false for false' do
|
196
|
-
subject.virtual_attribute = false
|
197
|
-
|
198
|
-
expect(subject.virtual_attribute?).to eq(false)
|
199
|
-
end
|
200
|
-
|
201
|
-
it 'returns false for nil' do
|
202
|
-
subject.virtual_attribute = nil
|
203
|
-
|
204
|
-
expect(subject.virtual_attribute?).to eq(false)
|
205
|
-
end
|
206
|
-
|
207
|
-
it 'returns true for 1' do
|
208
|
-
subject.virtual_attribute = 1
|
209
|
-
|
210
|
-
expect(subject.virtual_attribute?).to eq(true)
|
211
|
-
end
|
212
|
-
|
213
|
-
it 'returns true for an object' do
|
214
|
-
subject.virtual_attribute = Object.new
|
215
|
-
|
216
|
-
expect(subject.virtual_attribute?).to eq(true)
|
217
|
-
end
|
218
|
-
|
219
|
-
end
|
220
|
-
|
221
|
-
describe '#inspect' do
|
222
|
-
|
223
|
-
it 'returns the contents of the object as a nicely formatted string' do
|
224
|
-
t = Time.now
|
225
|
-
subject.virtual_string = "string"
|
226
|
-
subject.virtual_integer = 17
|
227
|
-
subject.virtual_time = t
|
228
|
-
subject.virtual_date = Date.today
|
229
|
-
subject.virtual_boolean = true
|
230
|
-
subject.virtual_attribute = OpenStruct.new({:test => "openstruct"})
|
231
|
-
|
232
|
-
expect(subject.inspect).to eq("#<ObjectSpec::Object virtual_attribute: #<OpenStruct test=\"openstruct\">, virtual_boolean: true, virtual_date: \"#{Date.today}\", virtual_integer: 17, virtual_string: \"string\", virtual_time: \"#{t.to_s(:db)}\", virtual_type_attribute: nil>")
|
233
|
-
end
|
234
|
-
|
235
|
-
end
|
236
|
-
|
237
|
-
describe '#attributes' do
|
238
|
-
|
239
|
-
it 'returns a hash of virtual attributes' do
|
240
|
-
subject.virtual_string = "string"
|
241
|
-
subject.virtual_integer = "17"
|
242
|
-
|
243
|
-
expect(subject.attributes).to eq({
|
244
|
-
"virtual_string" => "string",
|
245
|
-
"virtual_integer" => 17,
|
246
|
-
"virtual_time" => nil,
|
247
|
-
"virtual_date" => nil,
|
248
|
-
"virtual_boolean" => nil,
|
249
|
-
"virtual_attribute" => nil,
|
250
|
-
"virtual_type_attribute" => nil,
|
251
|
-
})
|
252
|
-
end
|
253
|
-
|
254
|
-
it 'also includes inherited attributes' do
|
255
|
-
object = ObjectSpec::InheritingObject.new
|
256
|
-
object.virtual_string = "string"
|
257
|
-
object.virtual_integer = "17"
|
258
|
-
|
259
|
-
expect(object.attributes).to eq({
|
260
|
-
"virtual_string" => "string",
|
261
|
-
"virtual_integer" => 17,
|
262
|
-
"virtual_time" => nil,
|
263
|
-
"virtual_date" => nil,
|
264
|
-
"virtual_boolean" => nil,
|
265
|
-
"virtual_attribute" => nil,
|
266
|
-
"another_virtual_string" => nil,
|
267
|
-
"virtual_type_attribute" => nil,
|
268
|
-
})
|
269
|
-
end
|
270
|
-
|
271
|
-
it 'also includes included attributes' do
|
272
|
-
object = ObjectSpec::IncludingObject.new
|
273
|
-
object.virtual_string = "string"
|
274
|
-
object.virtual_integer = "17"
|
275
|
-
|
276
|
-
expect(object.attributes).to eq({
|
277
|
-
"virtual_string" => "string",
|
278
|
-
"virtual_integer" => 17,
|
279
|
-
"virtual_time" => nil,
|
280
|
-
"virtual_date" => nil,
|
281
|
-
"virtual_boolean" => nil,
|
282
|
-
"virtual_attribute" => nil,
|
283
|
-
"another_virtual_string" => nil,
|
284
|
-
"virtual_type_attribute" => nil,
|
285
|
-
})
|
286
|
-
end
|
287
|
-
|
288
|
-
end
|
289
|
-
|
290
|
-
describe 'inherited classes' do
|
291
|
-
|
292
|
-
it 'sees attributes of both classes' do
|
293
|
-
object = ObjectSpec::InheritingObject.new
|
294
|
-
object.virtual_string = "string"
|
295
|
-
object.another_virtual_string = "another string"
|
296
|
-
|
297
|
-
expect(object.virtual_string).to eq("string")
|
298
|
-
expect(object.another_virtual_string).to eq("another string")
|
299
|
-
end
|
300
|
-
|
301
|
-
it 'does not define the attribute on the parent class' do
|
302
|
-
object = ObjectSpec::Object.new
|
303
|
-
expect(object).not_to respond_to(:another_virtual_string)
|
304
|
-
end
|
305
|
-
|
306
|
-
end
|
307
|
-
|
308
|
-
describe 'included modules' do
|
309
|
-
it 'sees attributes of the included module' do
|
310
|
-
object = ObjectSpec::IncludingObject.new
|
311
|
-
object.virtual_string = "string"
|
312
|
-
object.another_virtual_string = "another string"
|
313
|
-
|
314
|
-
expect(object.virtual_string).to eq("string")
|
315
|
-
expect(object.another_virtual_string).to eq("another string")
|
316
|
-
end
|
317
|
-
|
318
|
-
it 'does not define the attribute on the parent class' do
|
319
|
-
object = ObjectSpec::Object.new
|
320
|
-
expect(object).not_to respond_to(:another_virtual_string)
|
321
|
-
end
|
322
|
-
end
|
323
|
-
|
324
|
-
describe 'validations' do
|
325
|
-
subject { ObjectSpec::ObjectWithValidations.new }
|
326
|
-
|
327
|
-
it 'has 1 error_on' do
|
328
|
-
expect(subject.error_on(:virtual_string).size).to eq(1)
|
329
|
-
end
|
330
|
-
|
331
|
-
it 'validates the presence of boolean values' do
|
332
|
-
subject.virtual_boolean = false
|
333
|
-
expect(subject.error_on(:virtual_boolean).size).to eq(1)
|
334
|
-
subject.virtual_boolean = '0'
|
335
|
-
expect(subject.error_on(:virtual_boolean).size).to eq(1)
|
336
|
-
subject.virtual_boolean = 0
|
337
|
-
expect(subject.error_on(:virtual_boolean).size).to eq(1)
|
338
|
-
subject.virtual_boolean = true
|
339
|
-
expect(subject.errors_on(:virtual_boolean).size).to eq(0)
|
340
|
-
end
|
341
|
-
|
342
|
-
it 'has no errors if validations pass' do
|
343
|
-
subject.virtual_string = "foo"
|
344
|
-
subject.virtual_boolean = true
|
345
|
-
expect(subject).to be_valid
|
346
|
-
expect(subject.errors_on(:virtual_string).size).to eq(0)
|
347
|
-
end
|
348
|
-
|
349
|
-
it 'causes #save to return false' do
|
350
|
-
expect(subject.save).to be_falsey
|
351
|
-
end
|
352
|
-
end
|
353
|
-
|
354
|
-
describe 'defaults' do
|
355
|
-
it_should_behave_like "a class accepting attribute defaults", ActiveType::Object
|
356
|
-
end
|
357
|
-
|
358
|
-
describe 'duping' do
|
359
|
-
it_should_behave_like "a class supporting dup for attributes", ActiveType::Object
|
360
|
-
|
361
|
-
it 'can dup without attributes' do
|
362
|
-
expect {
|
363
|
-
ObjectSpec::PlainObject.new.dup
|
364
|
-
}.not_to raise_error
|
365
|
-
end
|
366
|
-
end
|
367
|
-
|
368
|
-
describe 'dirty tracking' do
|
369
|
-
it_should_behave_like 'a class supporting dirty tracking for virtual attributes', ActiveType::Object
|
370
|
-
end
|
371
|
-
|
372
|
-
describe '#belongs_to' do
|
373
|
-
subject { ObjectSpec::ObjectWithBelongsTo.new }
|
374
|
-
|
375
|
-
it_should_behave_like 'a belongs_to association', :child, ObjectSpec::Child
|
376
|
-
end
|
377
|
-
|
378
|
-
describe '#save' do
|
379
|
-
subject { ObjectSpec::ObjectWithCallbacks.new }
|
380
|
-
|
381
|
-
it "returns true" do
|
382
|
-
subject.save
|
383
|
-
end
|
384
|
-
|
385
|
-
%w[before_validation before_save after_save after_commit].each do |callback|
|
386
|
-
|
387
|
-
it "calls #{callback}", :rollback => false do
|
388
|
-
expect(subject).to receive("#{callback}_callback")
|
389
|
-
|
390
|
-
expect(subject.save).to eq(true)
|
391
|
-
end
|
392
|
-
|
393
|
-
end
|
394
|
-
|
395
|
-
%w[before_validation before_save].each do |callback|
|
396
|
-
|
397
|
-
it "aborts the chain when #{callback} returns false" do
|
398
|
-
if ActiveRecord::VERSION::MAJOR >= 5
|
399
|
-
allow(subject).to receive("#{callback}_callback") do
|
400
|
-
throw(:abort)
|
401
|
-
end
|
402
|
-
else
|
403
|
-
allow(subject).to receive_messages("#{callback}_callback" => false)
|
404
|
-
end
|
405
|
-
|
406
|
-
expect(subject.save).to be_falsey
|
407
|
-
end
|
408
|
-
|
409
|
-
end
|
410
|
-
|
411
|
-
it 'runs after_rollback callbacks if an after_save callback raises an error', :rollback => false do
|
412
|
-
expect(subject).to receive(:after_save_callback).ordered.and_raise(ActiveRecord::Rollback)
|
413
|
-
expect(subject).to receive(:after_rollback_callback).ordered
|
414
|
-
|
415
|
-
expect(subject.save).to be_falsey
|
416
|
-
end
|
417
|
-
|
418
|
-
it 'does not run after_rollback callbacks if after_save does not raise an error', :rollback => false do
|
419
|
-
expect(subject).to_not receive(:after_rollback_callback)
|
420
|
-
|
421
|
-
expect(subject.save).to be_truthy
|
422
|
-
|
423
|
-
end
|
424
|
-
|
425
|
-
end
|
426
|
-
|
427
|
-
describe '#id' do
|
428
|
-
|
429
|
-
it 'is nil' do
|
430
|
-
expect(subject.id).to eq nil
|
431
|
-
end
|
432
|
-
|
433
|
-
end
|
434
|
-
|
435
|
-
describe '.find' do
|
436
|
-
it 'raises an error' do
|
437
|
-
error = if ActiveRecord::VERSION::MAJOR == 4 && ActiveRecord::VERSION::MINOR >= 1
|
438
|
-
ActiveRecord::UnknownPrimaryKey
|
439
|
-
else
|
440
|
-
ActiveRecord::RecordNotFound
|
441
|
-
end
|
442
|
-
expect do
|
443
|
-
ObjectSpec::Object.find(1)
|
444
|
-
end.to raise_error(error)
|
445
|
-
end
|
446
|
-
end
|
447
|
-
|
448
|
-
describe '.all' do
|
449
|
-
it 'returns []' do
|
450
|
-
expect(ObjectSpec::Object.all).to eq([])
|
451
|
-
end
|
452
|
-
end
|
453
|
-
|
454
|
-
describe '.create' do
|
455
|
-
it 'returns an object' do
|
456
|
-
object = ObjectSpec::Object.create(:virtual_string => "string")
|
457
|
-
|
458
|
-
expect(object).to be_a(ObjectSpec::Object)
|
459
|
-
expect(object.virtual_string).to eq("string")
|
460
|
-
end
|
461
|
-
end
|
462
|
-
|
463
|
-
end
|