active_data 0.3.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
Files changed (142) hide show
  1. checksums.yaml +4 -4
  2. data/.gitignore +1 -1
  3. data/.rspec +0 -1
  4. data/.rvmrc +1 -1
  5. data/.travis.yml +13 -6
  6. data/Appraisals +7 -0
  7. data/Gemfile +1 -5
  8. data/Guardfile +68 -15
  9. data/README.md +144 -2
  10. data/active_data.gemspec +19 -11
  11. data/gemfiles/rails.4.0.gemfile +14 -0
  12. data/gemfiles/rails.4.1.gemfile +14 -0
  13. data/gemfiles/rails.4.2.gemfile +14 -0
  14. data/gemfiles/rails.5.0.gemfile +14 -0
  15. data/lib/active_data.rb +120 -3
  16. data/lib/active_data/active_record/associations.rb +50 -0
  17. data/lib/active_data/active_record/nested_attributes.rb +24 -0
  18. data/lib/active_data/config.rb +40 -0
  19. data/lib/active_data/errors.rb +93 -0
  20. data/lib/active_data/extensions.rb +33 -0
  21. data/lib/active_data/model.rb +16 -74
  22. data/lib/active_data/model/associations.rb +84 -15
  23. data/lib/active_data/model/associations/base.rb +79 -0
  24. data/lib/active_data/model/associations/collection/embedded.rb +12 -0
  25. data/lib/active_data/model/associations/collection/proxy.rb +32 -0
  26. data/lib/active_data/model/associations/collection/referenced.rb +26 -0
  27. data/lib/active_data/model/associations/embeds_many.rb +124 -18
  28. data/lib/active_data/model/associations/embeds_one.rb +90 -15
  29. data/lib/active_data/model/associations/nested_attributes.rb +180 -0
  30. data/lib/active_data/model/associations/references_many.rb +96 -0
  31. data/lib/active_data/model/associations/references_one.rb +83 -0
  32. data/lib/active_data/model/associations/reflections/base.rb +100 -0
  33. data/lib/active_data/model/associations/reflections/embeds_many.rb +25 -0
  34. data/lib/active_data/model/associations/reflections/embeds_one.rb +49 -0
  35. data/lib/active_data/model/associations/reflections/reference_reflection.rb +45 -0
  36. data/lib/active_data/model/associations/reflections/references_many.rb +28 -0
  37. data/lib/active_data/model/associations/reflections/references_one.rb +28 -0
  38. data/lib/active_data/model/associations/validations.rb +63 -0
  39. data/lib/active_data/model/attributes.rb +247 -0
  40. data/lib/active_data/model/attributes/attribute.rb +73 -0
  41. data/lib/active_data/model/attributes/base.rb +116 -0
  42. data/lib/active_data/model/attributes/collection.rb +17 -0
  43. data/lib/active_data/model/attributes/dictionary.rb +26 -0
  44. data/lib/active_data/model/attributes/localized.rb +42 -0
  45. data/lib/active_data/model/attributes/reference_many.rb +21 -0
  46. data/lib/active_data/model/attributes/reference_one.rb +42 -0
  47. data/lib/active_data/model/attributes/reflections/attribute.rb +55 -0
  48. data/lib/active_data/model/attributes/reflections/base.rb +62 -0
  49. data/lib/active_data/model/attributes/reflections/collection.rb +10 -0
  50. data/lib/active_data/model/attributes/reflections/dictionary.rb +13 -0
  51. data/lib/active_data/model/attributes/reflections/localized.rb +43 -0
  52. data/lib/active_data/model/attributes/reflections/reference_many.rb +10 -0
  53. data/lib/active_data/model/attributes/reflections/reference_one.rb +58 -0
  54. data/lib/active_data/model/attributes/reflections/represents.rb +55 -0
  55. data/lib/active_data/model/attributes/represents.rb +64 -0
  56. data/lib/active_data/model/callbacks.rb +71 -0
  57. data/lib/active_data/model/conventions.rb +35 -0
  58. data/lib/active_data/model/dirty.rb +77 -0
  59. data/lib/active_data/model/lifecycle.rb +307 -0
  60. data/lib/active_data/model/localization.rb +21 -0
  61. data/lib/active_data/model/persistence.rb +57 -0
  62. data/lib/active_data/model/primary.rb +51 -0
  63. data/lib/active_data/model/scopes.rb +77 -0
  64. data/lib/active_data/model/validations.rb +27 -0
  65. data/lib/active_data/model/validations/associated.rb +19 -0
  66. data/lib/active_data/model/validations/nested.rb +39 -0
  67. data/lib/active_data/railtie.rb +7 -0
  68. data/lib/active_data/version.rb +1 -1
  69. data/spec/lib/active_data/active_record/associations_spec.rb +149 -0
  70. data/spec/lib/active_data/active_record/nested_attributes_spec.rb +16 -0
  71. data/spec/lib/active_data/config_spec.rb +44 -0
  72. data/spec/lib/active_data/model/associations/embeds_many_spec.rb +362 -52
  73. data/spec/lib/active_data/model/associations/embeds_one_spec.rb +250 -31
  74. data/spec/lib/active_data/model/associations/nested_attributes_spec.rb +23 -0
  75. data/spec/lib/active_data/model/associations/references_many_spec.rb +196 -0
  76. data/spec/lib/active_data/model/associations/references_one_spec.rb +134 -0
  77. data/spec/lib/active_data/model/associations/reflections/embeds_many_spec.rb +144 -0
  78. data/spec/lib/active_data/model/associations/reflections/embeds_one_spec.rb +116 -0
  79. data/spec/lib/active_data/model/associations/reflections/references_many_spec.rb +255 -0
  80. data/spec/lib/active_data/model/associations/reflections/references_one_spec.rb +208 -0
  81. data/spec/lib/active_data/model/associations/validations_spec.rb +153 -0
  82. data/spec/lib/active_data/model/associations_spec.rb +189 -0
  83. data/spec/lib/active_data/model/attributes/attribute_spec.rb +144 -0
  84. data/spec/lib/active_data/model/attributes/base_spec.rb +82 -0
  85. data/spec/lib/active_data/model/attributes/collection_spec.rb +73 -0
  86. data/spec/lib/active_data/model/attributes/dictionary_spec.rb +93 -0
  87. data/spec/lib/active_data/model/attributes/localized_spec.rb +88 -33
  88. data/spec/lib/active_data/model/attributes/reflections/attribute_spec.rb +72 -0
  89. data/spec/lib/active_data/model/attributes/reflections/base_spec.rb +56 -0
  90. data/spec/lib/active_data/model/attributes/reflections/collection_spec.rb +37 -0
  91. data/spec/lib/active_data/model/attributes/reflections/dictionary_spec.rb +43 -0
  92. data/spec/lib/active_data/model/attributes/reflections/localized_spec.rb +37 -0
  93. data/spec/lib/active_data/model/attributes/reflections/represents_spec.rb +70 -0
  94. data/spec/lib/active_data/model/attributes/represents_spec.rb +153 -0
  95. data/spec/lib/active_data/model/attributes_spec.rb +243 -0
  96. data/spec/lib/active_data/model/callbacks_spec.rb +338 -0
  97. data/spec/lib/active_data/model/conventions_spec.rb +12 -0
  98. data/spec/lib/active_data/model/dirty_spec.rb +75 -0
  99. data/spec/lib/active_data/model/lifecycle_spec.rb +330 -0
  100. data/spec/lib/active_data/model/nested_attributes.rb +202 -0
  101. data/spec/lib/active_data/model/persistence_spec.rb +47 -0
  102. data/spec/lib/active_data/model/primary_spec.rb +84 -0
  103. data/spec/lib/active_data/model/scopes_spec.rb +88 -0
  104. data/spec/lib/active_data/model/typecasting_spec.rb +192 -0
  105. data/spec/lib/active_data/model/validations/associated_spec.rb +94 -0
  106. data/spec/lib/active_data/model/validations/nested_spec.rb +93 -0
  107. data/spec/lib/active_data/model/validations_spec.rb +31 -0
  108. data/spec/lib/active_data/model_spec.rb +1 -32
  109. data/spec/lib/active_data_spec.rb +12 -0
  110. data/spec/spec_helper.rb +39 -0
  111. data/spec/support/model_helpers.rb +10 -0
  112. metadata +246 -54
  113. data/gemfiles/Gemfile.rails-3 +0 -14
  114. data/lib/active_data/attributes/base.rb +0 -69
  115. data/lib/active_data/attributes/localized.rb +0 -42
  116. data/lib/active_data/model/associations/association.rb +0 -30
  117. data/lib/active_data/model/attributable.rb +0 -122
  118. data/lib/active_data/model/collectionizable.rb +0 -55
  119. data/lib/active_data/model/collectionizable/proxy.rb +0 -42
  120. data/lib/active_data/model/extensions.rb +0 -9
  121. data/lib/active_data/model/extensions/array.rb +0 -24
  122. data/lib/active_data/model/extensions/big_decimal.rb +0 -17
  123. data/lib/active_data/model/extensions/boolean.rb +0 -38
  124. data/lib/active_data/model/extensions/date.rb +0 -17
  125. data/lib/active_data/model/extensions/date_time.rb +0 -17
  126. data/lib/active_data/model/extensions/float.rb +0 -17
  127. data/lib/active_data/model/extensions/hash.rb +0 -22
  128. data/lib/active_data/model/extensions/integer.rb +0 -17
  129. data/lib/active_data/model/extensions/localized.rb +0 -22
  130. data/lib/active_data/model/extensions/object.rb +0 -17
  131. data/lib/active_data/model/extensions/string.rb +0 -17
  132. data/lib/active_data/model/extensions/time.rb +0 -17
  133. data/lib/active_data/model/localizable.rb +0 -31
  134. data/lib/active_data/model/nested_attributes.rb +0 -58
  135. data/lib/active_data/model/parameterizable.rb +0 -29
  136. data/lib/active_data/validations.rb +0 -7
  137. data/lib/active_data/validations/associated.rb +0 -17
  138. data/spec/lib/active_data/model/attributable_spec.rb +0 -191
  139. data/spec/lib/active_data/model/collectionizable_spec.rb +0 -60
  140. data/spec/lib/active_data/model/nested_attributes_spec.rb +0 -67
  141. data/spec/lib/active_data/model/type_cast_spec.rb +0 -116
  142. data/spec/lib/active_data/validations/associated_spec.rb +0 -88
@@ -0,0 +1,47 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe ActiveData::Model::Persistence do
5
+ let(:model) do
6
+ stub_model do
7
+ include ActiveData::Model::Persistence
8
+
9
+ attribute :name
10
+ attribute :count, default: 0
11
+ end
12
+ end
13
+
14
+ specify { expect(model.new).not_to be_persisted }
15
+ specify { expect(model.new).not_to be_destroyed }
16
+
17
+ describe '#instantiate' do
18
+ specify { expect(model.instantiate({})).to be_an_instance_of model }
19
+ specify { expect(model.instantiate({})).to be_persisted }
20
+ specify { expect(model.instantiate({})).not_to be_destroyed }
21
+
22
+ context do
23
+ subject(:instance) { model.instantiate(name: 'Hello', foo: 'Bar') }
24
+
25
+ specify { expect(subject.instance_variable_get(:@initial_attributes)).to eq({ name: 'Hello' }.stringify_keys) }
26
+ end
27
+ end
28
+
29
+ describe '#instantiate_collection' do
30
+ context do
31
+ subject(:instances) { model.instantiate_collection(name: 'Hello', foo: 'Bar') }
32
+
33
+ specify { expect(subject).to be_a Array }
34
+ specify { expect(subject.first.instance_variable_get(:@initial_attributes)).to eq({ name: 'Hello' }.stringify_keys) }
35
+ end
36
+
37
+ context do
38
+ before { model.send(:include, ActiveData::Model::Scopes) }
39
+ subject(:instances) { model.instantiate_collection([{ name: 'Hello', foo: 'Bar' }, {name: 'World'}]) }
40
+
41
+ specify { expect(subject).to be_a ActiveData::Model::Scopes::ScopeProxy }
42
+ specify { expect(subject.count).to eq(2) }
43
+ specify { expect(subject.first.instance_variable_get(:@initial_attributes)).to eq({ name: 'Hello' }.stringify_keys) }
44
+ specify { expect(subject.second.instance_variable_get(:@initial_attributes)).to eq({ name: 'World' }.stringify_keys) }
45
+ end
46
+ end
47
+ end
@@ -0,0 +1,84 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveData::Model::Primary do
4
+ context 'undefined' do
5
+ let(:model) do
6
+ stub_model do
7
+ include ActiveData::Model::Primary
8
+
9
+ attribute :name, String
10
+ end
11
+ end
12
+
13
+ specify { expect(model.has_primary_attribute?).to eq(false) }
14
+ specify { expect(model.new.has_primary_attribute?).to eq(false) }
15
+ specify { expect { model.new.primary_attribute }.to raise_error NoMethodError }
16
+ specify { expect(model.new(name: 'Hello')).to eq(model.new(name: 'Hello')) }
17
+ specify { expect(model.new(name: 'Hello')).to eql(model.new(name: 'Hello')) }
18
+
19
+ context do
20
+ let(:object) { model.new(name: 'Hello') }
21
+ specify { expect(object).not_to eq(object.clone.tap { |o| o.update(name: 'World') }) }
22
+ specify { expect(object).not_to eql(object.clone.tap { |o| o.update(name: 'World') }) }
23
+ end
24
+ end
25
+
26
+ context 'defined' do
27
+ let(:model) do
28
+ stub_model do
29
+ include ActiveData::Model::Primary
30
+
31
+ primary_attribute
32
+ attribute :name, String
33
+ end
34
+ end
35
+
36
+ specify { expect(model.has_primary_attribute?).to eq(true) }
37
+ specify { expect(model.new.has_primary_attribute?).to eq(true) }
38
+ specify { expect(model.new.primary_attribute).to be_a(ActiveData::UUID) }
39
+ specify { expect(model.new(name: 'Hello')).not_to eq(model.new(name: 'Hello')) }
40
+ specify { expect(model.new(name: 'Hello')).not_to eql(model.new(name: 'Hello')) }
41
+ specify { expect(model.new(id: 0).id).not_to eq(ActiveData::UUID.parse_int(0)) }
42
+ specify { expect(model.new.tap { |o| o.id = 0 }.id).to eq(ActiveData::UUID.parse_int(0)) }
43
+
44
+ context do
45
+ let(:object) { model.new(name: 'Hello') }
46
+ specify { expect(object).to eq(object.clone.tap { |o| o.update(name: 'World') }) }
47
+ specify { expect(object).to eql(object.clone.tap { |o| o.update(name: 'World') }) }
48
+ end
49
+ end
50
+
51
+ context 'defined' do
52
+ let(:model) do
53
+ stub_model do
54
+ include ActiveData::Model::Primary
55
+
56
+ primary_attribute type: Integer
57
+ attribute :name
58
+ end
59
+ end
60
+
61
+ specify { expect(model.has_primary_attribute?).to eq(true) }
62
+ specify { expect(model.new.has_primary_attribute?).to eq(true) }
63
+ specify { expect(model.new.primary_attribute).to be_nil }
64
+ specify { expect(model.new(name: 'Hello')).not_to eq(model.new(name: 'Hello')) }
65
+ specify { expect(model.new(name: 'Hello')).not_to eql(model.new(name: 'Hello')) }
66
+ specify { expect(model.new(name: 'Hello').tap { |o| o.id = 1 }).not_to eq(model.new(name: 'Hello').tap { |o| o.id = 2 }) }
67
+ specify { expect(model.new(name: 'Hello').tap { |o| o.id = 1 }).not_to eql(model.new(name: 'Hello').tap { |o| o.id = 2 }) }
68
+ specify { expect(model.new(id: 1).id).to be_nil }
69
+ specify { expect(model.new.tap { |o| o.assign_attributes(id: 1) }.id).to be_nil }
70
+ specify { expect(model.new.tap { |o| o.id = 1 }.id).to eq(1) }
71
+
72
+ context do
73
+ let(:object) { model.new(name: 'Hello').tap { |o| o.id = 1 } }
74
+ specify { expect(object).to eq(object.clone.tap { |o| o.update(name: 'World') }) }
75
+ specify { expect(object).to eql(object.clone.tap { |o| o.update(name: 'World') }) }
76
+ end
77
+
78
+ context do
79
+ let(:object) { model.new(name: 'Hello') }
80
+ specify { expect(object).to eq(object) }
81
+ specify { expect(object).to eql(object) }
82
+ end
83
+ end
84
+ end
@@ -0,0 +1,88 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe ActiveData::Model::Scopes do
5
+ let(:model) do
6
+ stub_model do
7
+ include ActiveData::Model::Scopes
8
+
9
+ attribute :name, String
10
+
11
+ class << self
12
+ def except_first
13
+ scope[1..-1]
14
+ end
15
+
16
+ def no_mars
17
+ scope.delete_if { |i| i.name == 'Mars' }
18
+ end
19
+
20
+ private
21
+
22
+ def hidden_method
23
+ end
24
+ end
25
+ end
26
+ end
27
+
28
+ let(:hash_scoped_model) do
29
+ stub_model do
30
+ include ActiveData::Model::Scopes
31
+ scopify Hash
32
+ end
33
+ end
34
+
35
+ describe '.scope_class' do
36
+ specify { expect(model.scope_class).to be < Array }
37
+ specify { expect(model.scope_class).to eq(model::ScopeProxy) }
38
+ specify { expect(model.scope_class._scope_model).to eq(model) }
39
+ specify { expect(model.scope_class.new).to be_empty }
40
+
41
+ specify { expect(hash_scoped_model.scope_class).to be < Hash }
42
+ specify { expect(hash_scoped_model.scope_class).to eq(hash_scoped_model::ScopeProxy) }
43
+ specify { expect(hash_scoped_model.scope_class._scope_model).to eq(hash_scoped_model) }
44
+ specify { expect(hash_scoped_model.scope_class.new).to be_empty }
45
+
46
+ context do
47
+ let(:scope) { model.scope([model.new(name: 'Hello'), model.new(name: 'World'), model.new(name: 'Mars')]) }
48
+
49
+ specify { expect(scope).to be_instance_of model.scope_class }
50
+ specify { expect { model.scope([model.new(name: 'Hello'), {}]) }.to raise_error ActiveData::AssociationTypeMismatch }
51
+
52
+ context 'scopes' do
53
+ specify { expect(scope.except_first).to be_instance_of model.scope_class }
54
+ specify { expect(scope.no_mars).to be_instance_of model.scope_class }
55
+ specify { expect(scope.except_first).to eq(model.scope([model.new(name: 'World'), model.new(name: 'Mars')])) }
56
+ specify { expect(scope.no_mars).to eq(model.scope([model.new(name: 'Hello'), model.new(name: 'World')])) }
57
+ specify { expect(scope.except_first.no_mars).to eq(model.scope([model.new(name: 'World')])) }
58
+ specify { expect(scope.no_mars.except_first).to eq(model.scope([model.new(name: 'World')])) }
59
+ specify { expect { scope.hidden_method }.to raise_error NoMethodError }
60
+
61
+ specify { expect(scope).to respond_to(:no_mars) }
62
+ specify { expect(scope).to respond_to(:except_first) }
63
+ specify { expect(scope).not_to respond_to(:hidden_method) }
64
+ end
65
+ end
66
+ end
67
+
68
+ context do
69
+ let!(:ancestor) do
70
+ stub_model do
71
+ include ActiveData::Model::Scopes
72
+ end
73
+ end
74
+
75
+ let!(:descendant1) do
76
+ Class.new ancestor
77
+ end
78
+
79
+ let!(:descendant2) do
80
+ Class.new ancestor
81
+ end
82
+
83
+ specify { expect(descendant1.scope_class).to be < Array }
84
+ specify { expect(descendant2.scope_class).to be < Array }
85
+ specify { expect(ancestor.scope_class).not_to eq(descendant1.scope_class) }
86
+ specify { expect(descendant1.scope_class).not_to eq(descendant2.scope_class) }
87
+ end
88
+ end
@@ -0,0 +1,192 @@
1
+ # encoding: UTF-8
2
+ require 'spec_helper'
3
+
4
+ describe ActiveData::Model::Attributes do
5
+ subject { model.new }
6
+
7
+ context 'object' do
8
+ before { stub_class(:descendant) }
9
+ let(:model) { stub_model { attribute :column, Object } }
10
+
11
+ specify { expect(model.new(column: 'hello').column).to eq('hello') }
12
+ specify { expect(model.new(column: []).column).to eq([]) }
13
+ specify { expect(model.new(column: Descendant.new).column).to be_a(Descendant) }
14
+ specify { expect(model.new(column: Object.new).column).to be_a(Object) }
15
+ specify { expect(model.new(column: nil).column).to be_nil }
16
+
17
+ context do
18
+ before { stub_class(:descendant2, Descendant) }
19
+ let(:model) { stub_model { attribute :column, Descendant } }
20
+
21
+ specify { expect(model.new(column: 'hello').column).to be_nil }
22
+ specify { expect(model.new(column: []).column).to be_nil }
23
+ specify { expect(model.new(column: Descendant.new).column).to be_a(Descendant) }
24
+ specify { expect(model.new(column: Descendant2.new).column).to be_a(Descendant2) }
25
+ specify { expect(model.new(column: Object.new).column).to be_nil }
26
+ specify { expect(model.new(column: nil).column).to be_nil }
27
+ end
28
+ end
29
+
30
+ context 'string' do
31
+ let(:model) { stub_model { attribute :column, String } }
32
+
33
+ specify { expect(model.new(column: 'hello').column).to eq('hello') }
34
+ specify { expect(model.new(column: 123).column).to eq('123') }
35
+ specify { expect(model.new(column: nil).column).to be_nil }
36
+ end
37
+
38
+ context 'integer' do
39
+ let(:model) { stub_model { attribute :column, Integer } }
40
+
41
+ specify { expect(model.new(column: 'hello').column).to be_nil }
42
+ specify { expect(model.new(column: '123hello').column).to be_nil }
43
+ specify { expect(model.new(column: '123').column).to eq(123) }
44
+ specify { expect(model.new(column: '123.5').column).to eq(123) }
45
+ specify { expect(model.new(column: 123).column).to eq(123) }
46
+ specify { expect(model.new(column: 123.5).column).to eq(123) }
47
+ specify { expect(model.new(column: nil).column).to be_nil }
48
+ specify { expect(model.new(column: [123]).column).to be_nil }
49
+ end
50
+
51
+ context 'float' do
52
+ let(:model) { stub_model { attribute :column, Float } }
53
+
54
+ specify { expect(model.new(column: 'hello').column).to be_nil }
55
+ specify { expect(model.new(column: '123hello').column).to be_nil }
56
+ specify { expect(model.new(column: '123').column).to eq(123.0) }
57
+ specify { expect(model.new(column: '123.').column).to be_nil }
58
+ specify { expect(model.new(column: '123.5').column).to eq(123.5) }
59
+ specify { expect(model.new(column: 123).column).to eq(123.0) }
60
+ specify { expect(model.new(column: 123.5).column).to eq(123.5) }
61
+ specify { expect(model.new(column: nil).column).to be_nil }
62
+ specify { expect(model.new(column: [123.5]).column).to be_nil }
63
+ end
64
+
65
+ context 'big_decimal' do
66
+ let(:model) { stub_model { attribute :column, BigDecimal } }
67
+
68
+ specify { expect(model.new(column: 'hello').column).to be_nil }
69
+ specify { expect(model.new(column: '123hello').column).to be_nil }
70
+ specify { expect(model.new(column: '123').column).to eq(BigDecimal.new('123.0')) }
71
+ specify { expect(model.new(column: '123.').column).to be_nil }
72
+ specify { expect(model.new(column: '123.5').column).to eq(BigDecimal.new('123.5')) }
73
+ specify { expect(model.new(column: 123).column).to eq(BigDecimal.new('123.0')) }
74
+ specify { expect(model.new(column: 123.5).column).to eq(BigDecimal.new('123.5')) }
75
+ specify { expect(model.new(column: nil).column).to be_nil }
76
+ specify { expect(model.new(column: [123.5]).column).to be_nil }
77
+ end
78
+
79
+ context 'boolean' do
80
+ let(:model) { stub_model { attribute :column, Boolean } }
81
+
82
+ specify { expect(model.new(column: 'hello').column).to be_nil }
83
+ specify { expect(model.new(column: 'true').column).to eq(true) }
84
+ specify { expect(model.new(column: 'false').column).to eq(false) }
85
+ specify { expect(model.new(column: '1').column).to eq(true) }
86
+ specify { expect(model.new(column: '0').column).to eq(false) }
87
+ specify { expect(model.new(column: true).column).to eq(true) }
88
+ specify { expect(model.new(column: false).column).to eq(false) }
89
+ specify { expect(model.new(column: 1).column).to eq(true) }
90
+ specify { expect(model.new(column: 0).column).to eq(false) }
91
+ specify { expect(model.new(column: nil).column).to be_nil }
92
+ specify { expect(model.new(column: [123]).column).to be_nil }
93
+ end
94
+
95
+ context 'array' do
96
+ let(:model) { stub_model { attribute :column, Array } }
97
+
98
+ specify { expect(model.new(column: [1, 2, 3]).column).to eq([1, 2, 3]) }
99
+ specify { expect(model.new(column: 'hello, world').column).to eq(['hello', 'world']) }
100
+ specify { expect(model.new(column: 10).column).to be_nil }
101
+ end
102
+
103
+ context 'date' do
104
+ let(:model) { stub_model { attribute :column, Date } }
105
+ let(:date) { Date.new(2013, 6, 13) }
106
+
107
+ specify { expect(model.new(column: nil).column).to be_nil }
108
+ specify { expect(model.new(column: '2013-06-13').column).to eq(date) }
109
+ specify { expect(model.new(column: '2013-55-55').column).to be_nil }
110
+ specify { expect(model.new(column: 'blablabla').column).to be_nil }
111
+ specify { expect(model.new(column: DateTime.new(2013, 6, 13, 23, 13)).column).to eq(date) }
112
+ specify { expect(model.new(column: Time.new(2013, 6, 13, 23, 13)).column).to eq(date) }
113
+ specify { expect(model.new(column: Date.new(2013, 6, 13)).column).to eq(date) }
114
+ end
115
+
116
+ context 'datetime' do
117
+ let(:model) { stub_model { attribute :column, DateTime } }
118
+ let(:datetime) { DateTime.new(2013, 6, 13, 23, 13) }
119
+
120
+ specify { expect(model.new(column: nil).column).to be_nil }
121
+ specify { expect(model.new(column: '2013-06-13 23:13').column).to eq(datetime) }
122
+ specify { expect(model.new(column: '2013-55-55 55:55').column).to be_nil }
123
+ specify { expect(model.new(column: 'blablabla').column).to be_nil }
124
+ specify { expect(model.new(column: Date.new(2013, 6, 13)).column).to eq(DateTime.new(2013, 6, 13, 0, 0)) }
125
+ specify { expect(model.new(column: Time.utc(2013, 6, 13, 23, 13).utc).column).to eq(DateTime.new(2013, 6, 13, 23, 13)) }
126
+ specify { expect(model.new(column: DateTime.new(2013, 6, 13, 23, 13)).column).to eq(DateTime.new(2013, 6, 13, 23, 13)) }
127
+ end
128
+
129
+ context 'time' do
130
+ let(:model) { stub_model { attribute :column, Time } }
131
+
132
+ specify { expect(model.new(column: nil).column).to be_nil }
133
+ specify { expect(model.new(column: '2013-06-13 23:13').column).to eq('2013-06-13 23:13'.to_time) }
134
+ specify { expect(model.new(column: '2013-55-55 55:55').column).to be_nil }
135
+ specify { expect(model.new(column: 'blablabla').column).to be_nil }
136
+ specify { expect(model.new(column: Date.new(2013, 6, 13)).column).to eq(Time.new(2013, 6, 13, 0, 0)) }
137
+ specify { expect(model.new(column: DateTime.new(2013, 6, 13, 19, 13)).column).to eq(DateTime.new(2013, 6, 13, 19, 13).to_time) }
138
+ specify { expect(model.new(column: Time.new(2013, 6, 13, 23, 13)).column).to eq(Time.new(2013, 6, 13, 23, 13)) }
139
+
140
+ context 'Time.zone set' do
141
+ around { |example| Time.use_zone('Bangkok', &example) }
142
+
143
+ specify { expect(model.new(column: nil).column).to be_nil }
144
+ specify { expect(model.new(column: '2013-06-13 23:13').column).to eq(Time.zone.parse('2013-06-13 23:13')) }
145
+ specify { expect(model.new(column: '2013-55-55 55:55').column).to be_nil }
146
+ specify { expect(model.new(column: 'blablabla').column).to be_nil }
147
+ specify { expect(model.new(column: Date.new(2013, 6, 13)).column).to eq(Time.new(2013, 6, 13, 0, 0)) }
148
+ specify { expect(model.new(column: DateTime.new(2013, 6, 13, 19, 13)).column).to eq(DateTime.new(2013, 6, 13, 19, 13).to_time) }
149
+ specify { expect(model.new(column: Time.new(2013, 6, 13, 23, 13)).column).to eq(Time.new(2013, 6, 13, 23, 13)) }
150
+ end
151
+ end
152
+
153
+ context 'time_zone' do
154
+ let(:model) { stub_model { attribute :column, ActiveSupport::TimeZone } }
155
+
156
+ specify { expect(model.new(column: nil).column).to be_nil }
157
+ specify { expect(model.new(column: Object.new).column).to be_nil }
158
+ specify { expect(model.new(column: Time.now).column).to be_nil }
159
+ specify { expect(model.new(column: 'blablabla').column).to be_nil }
160
+ specify { expect(model.new(column: TZInfo::Timezone.all.first).column).to be_a ActiveSupport::TimeZone }
161
+ specify { expect(model.new(column: 'Moscow').column).to be_a ActiveSupport::TimeZone }
162
+ specify { expect(model.new(column: '+4').column).to be_a ActiveSupport::TimeZone }
163
+ specify { expect(model.new(column: '-3').column).to be_a ActiveSupport::TimeZone }
164
+ specify { expect(model.new(column: '3600').column).to be_a ActiveSupport::TimeZone }
165
+ specify { expect(model.new(column: '-7200').column).to be_a ActiveSupport::TimeZone }
166
+ specify { expect(model.new(column: 4).column).to be_a ActiveSupport::TimeZone }
167
+ specify { expect(model.new(column: -3).column).to be_a ActiveSupport::TimeZone }
168
+ specify { expect(model.new(column: 3600).column).to be_a ActiveSupport::TimeZone }
169
+ specify { expect(model.new(column: -7200).column).to be_a ActiveSupport::TimeZone }
170
+ end
171
+
172
+ context 'uuid' do
173
+ let(:model) { stub_model { attribute :column, ActiveData::UUID } }
174
+ let(:uuid) { ActiveData::UUID.random_create }
175
+ let(:uuid_tools) { UUIDTools::UUID.random_create }
176
+
177
+ specify { expect(uuid.as_json).to eq(uuid.to_s) }
178
+ specify { expect(uuid.to_json).to eq("\"#{uuid.to_s}\"") }
179
+ specify { expect(uuid.to_param).to eq(uuid.to_s) }
180
+ specify { expect(uuid.to_query(:key)).to eq("key=#{uuid.to_s}") }
181
+
182
+ specify { expect(model.new(column: nil).column).to be_nil }
183
+ specify { expect(model.new(column: Object.new).column).to be_nil }
184
+ specify { expect(model.new(column: uuid_tools).column).to be_a ActiveData::UUID }
185
+ specify { expect(model.new(column: uuid_tools).column).to eq(uuid_tools) }
186
+ specify { expect(model.new(column: uuid).column).to eq(uuid) }
187
+ specify { expect(model.new(column: uuid.to_s).column).to eq(uuid) }
188
+ specify { expect(model.new(column: uuid.to_i).column).to eq(uuid) }
189
+ specify { expect(model.new(column: uuid.hexdigest).column).to eq(uuid) }
190
+ specify { expect(model.new(column: uuid.raw).column).to eq(uuid) }
191
+ end
192
+ end
@@ -0,0 +1,94 @@
1
+ require 'spec_helper'
2
+
3
+ describe ActiveData::Model::Validations::AssociatedValidator do
4
+ before do
5
+ stub_model(:validated_assoc) do
6
+ include ActiveData::Model
7
+ include ActiveData::Model::Lifecycle
8
+
9
+ attribute :name, String
10
+
11
+ validates_presence_of :name
12
+ end
13
+
14
+ stub_model(:unvalidated_assoc) do
15
+ include ActiveData::Model
16
+ include ActiveData::Model::Lifecycle
17
+
18
+ attribute :name, String
19
+ end
20
+
21
+ stub_model(:main) do
22
+ include ActiveData::Model::Persistence
23
+ include ActiveData::Model::Associations
24
+
25
+ attribute :name, String
26
+
27
+ validates_presence_of :name
28
+ validates_associated :validated_one, :unvalidated_one, :validated_many, :unvalidated_many
29
+
30
+ embeds_one :validated_one, validate: false, class_name: 'ValidatedAssoc'
31
+ embeds_one :unvalidated_one, class_name: 'UnvalidatedAssoc'
32
+ embeds_many :validated_many, class_name: 'ValidatedAssoc'
33
+ embeds_many :unvalidated_many, class_name: 'UnvalidatedAssoc'
34
+ end
35
+ end
36
+
37
+ context do
38
+ subject(:instance) { Main.instantiate name: 'hello', validated_one: { name: 'name' } }
39
+ it { is_expected.to be_valid }
40
+ end
41
+
42
+ context do
43
+ subject(:instance) { Main.instantiate name: 'hello', validated_one: { } }
44
+ it { is_expected.not_to be_valid }
45
+ specify { expect { instance.validate }.to change { instance.errors.messages }
46
+ .to(validated_one: ['is invalid']) }
47
+ end
48
+
49
+ context do
50
+ subject(:instance) { Main.instantiate name: 'hello', unvalidated_one: { name: 'name' } }
51
+ it { is_expected.to be_valid }
52
+ end
53
+
54
+ context do
55
+ subject(:instance) { Main.instantiate name: 'hello', unvalidated_one: { } }
56
+ it { is_expected.to be_valid }
57
+ end
58
+
59
+ context do
60
+ subject(:instance) { Main.instantiate name: 'hello', validated_many: [{ name: 'name' }] }
61
+ it { is_expected.to be_valid }
62
+ end
63
+
64
+ context do
65
+ subject(:instance) { Main.instantiate name: 'hello', validated_many: [{ }] }
66
+ it { is_expected.not_to be_valid }
67
+ specify { expect { instance.validate }.to change { instance.errors.messages }
68
+ .to(:'validated_many.0.name' => ["can't be blank"], validated_many: ['is invalid']) }
69
+ end
70
+
71
+ context do
72
+ subject(:instance) { Main.instantiate name: 'hello', unvalidated_many: [{ name: 'name' }] }
73
+ it { is_expected.to be_valid }
74
+ end
75
+
76
+ context do
77
+ subject(:instance) { Main.instantiate name: 'hello', unvalidated_many: [{ }] }
78
+ it { is_expected.to be_valid }
79
+ end
80
+
81
+ context do
82
+ subject(:instance) { Main.instantiate name: 'hello', validated_many: [{ name: 'name' }], validated_one: { } }
83
+ it { is_expected.not_to be_valid }
84
+ specify { expect { instance.validate }.to change { instance.errors.messages }
85
+ .to(validated_one: ['is invalid']) }
86
+ end
87
+
88
+ context do
89
+ subject(:instance) { Main.instantiate name: 'hello', validated_many: [{ }], validated_one: { name: 'name' } }
90
+ it { is_expected.not_to be_valid }
91
+ specify { expect { instance.validate }.to change { instance.errors.messages }
92
+ .to(:'validated_many.0.name' => ["can't be blank"], validated_many: ['is invalid']) }
93
+ end
94
+ end