active_data 1.0.0 → 1.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +5 -5
- data/.codeclimate.yml +13 -0
- data/.rubocop.yml +56 -0
- data/.rubocop_todo.yml +53 -0
- data/.rvmrc +1 -1
- data/.travis.yml +15 -2
- data/Appraisals +1 -1
- data/CHANGELOG.md +31 -0
- data/Guardfile +8 -8
- data/README.md +256 -0
- data/Rakefile +2 -4
- data/active_data.gemspec +8 -7
- data/gemfiles/rails.4.0.gemfile +1 -1
- data/gemfiles/rails.4.1.gemfile +1 -1
- data/gemfiles/rails.4.2.gemfile +1 -1
- data/gemfiles/rails.5.0.gemfile +1 -1
- data/gemfiles/rails.5.1.gemfile +14 -0
- data/lib/active_data/active_record/associations.rb +18 -13
- data/lib/active_data/active_record/nested_attributes.rb +8 -14
- data/lib/active_data/base.rb +13 -0
- data/lib/active_data/config.rb +4 -4
- data/lib/active_data/errors.rb +29 -13
- data/lib/active_data/extensions.rb +22 -21
- data/lib/active_data/model/associations/base.rb +22 -6
- data/lib/active_data/model/associations/embeds_any.rb +17 -0
- data/lib/active_data/model/associations/embeds_many.rb +29 -19
- data/lib/active_data/model/associations/embeds_one.rb +30 -26
- data/lib/active_data/model/associations/nested_attributes.rb +82 -50
- data/lib/active_data/model/associations/persistence_adapters/active_record/referenced_proxy.rb +31 -0
- data/lib/active_data/model/associations/persistence_adapters/active_record.rb +66 -0
- data/lib/active_data/model/associations/persistence_adapters/base.rb +53 -0
- data/lib/active_data/model/associations/references_any.rb +41 -0
- data/lib/active_data/model/associations/references_many.rb +51 -37
- data/lib/active_data/model/associations/references_one.rb +43 -41
- data/lib/active_data/model/associations/reflections/base.rb +19 -29
- data/lib/active_data/model/associations/reflections/embeds_any.rb +43 -0
- data/lib/active_data/model/associations/reflections/embeds_many.rb +3 -13
- data/lib/active_data/model/associations/reflections/embeds_one.rb +5 -37
- data/lib/active_data/model/associations/reflections/references_any.rb +62 -0
- data/lib/active_data/model/associations/reflections/references_many.rb +7 -7
- data/lib/active_data/model/associations/reflections/references_one.rb +9 -7
- data/lib/active_data/model/associations/reflections/singular.rb +35 -0
- data/lib/active_data/model/associations/validations.rb +2 -27
- data/lib/active_data/model/associations.rb +12 -10
- data/lib/active_data/model/attributes/attribute.rb +10 -10
- data/lib/active_data/model/attributes/base.rb +8 -7
- data/lib/active_data/model/attributes/localized.rb +4 -4
- data/lib/active_data/model/attributes/reference_many.rb +6 -8
- data/lib/active_data/model/attributes/reference_one.rb +17 -9
- data/lib/active_data/model/attributes/reflections/attribute.rb +2 -2
- data/lib/active_data/model/attributes/reflections/base.rb +8 -11
- data/lib/active_data/model/attributes/reflections/localized.rb +2 -2
- data/lib/active_data/model/attributes/reflections/reference_one.rb +11 -22
- data/lib/active_data/model/attributes/reflections/represents.rb +5 -6
- data/lib/active_data/model/attributes/represents.rb +6 -5
- data/lib/active_data/model/attributes.rb +33 -87
- data/lib/active_data/model/callbacks.rb +6 -7
- data/lib/active_data/model/conventions.rb +2 -0
- data/lib/active_data/model/dirty.rb +4 -4
- data/lib/active_data/model/lifecycle.rb +18 -20
- data/lib/active_data/model/localization.rb +5 -2
- data/lib/active_data/model/persistence.rb +2 -2
- data/lib/active_data/model/primary.rb +19 -14
- data/lib/active_data/model/representation.rb +81 -0
- data/lib/active_data/model/scopes.rb +22 -12
- data/lib/active_data/model/validations/associated.rb +3 -2
- data/lib/active_data/model/validations/nested.rb +6 -1
- data/lib/active_data/model/validations.rb +3 -3
- data/lib/active_data/model.rb +2 -1
- data/lib/active_data/undefined_class.rb +9 -0
- data/lib/active_data/version.rb +1 -1
- data/lib/active_data.rb +40 -17
- data/spec/lib/active_data/active_record/associations_spec.rb +107 -45
- data/spec/lib/active_data/active_record/nested_attributes_spec.rb +1 -2
- data/spec/lib/active_data/config_spec.rb +37 -15
- data/spec/lib/active_data/model/associations/embeds_many_spec.rb +475 -172
- data/spec/lib/active_data/model/associations/embeds_one_spec.rb +353 -96
- data/spec/lib/active_data/model/associations/nested_attributes_spec.rb +108 -12
- data/spec/lib/active_data/model/associations/persistence_adapters/active_record_spec.rb +58 -0
- data/spec/lib/active_data/model/associations/references_many_spec.rb +440 -64
- data/spec/lib/active_data/model/associations/references_one_spec.rb +347 -36
- data/spec/lib/active_data/model/associations/reflections/embeds_many_spec.rb +8 -7
- data/spec/lib/active_data/model/associations/reflections/embeds_one_spec.rb +7 -6
- data/spec/lib/active_data/model/associations/reflections/references_many_spec.rb +81 -33
- data/spec/lib/active_data/model/associations/reflections/references_one_spec.rb +116 -37
- data/spec/lib/active_data/model/associations/validations_spec.rb +27 -43
- data/spec/lib/active_data/model/associations_spec.rb +34 -25
- data/spec/lib/active_data/model/attributes/attribute_spec.rb +26 -23
- data/spec/lib/active_data/model/attributes/base_spec.rb +5 -6
- data/spec/lib/active_data/model/attributes/collection_spec.rb +7 -8
- data/spec/lib/active_data/model/attributes/dictionary_spec.rb +40 -33
- data/spec/lib/active_data/model/attributes/localized_spec.rb +27 -28
- data/spec/lib/active_data/model/attributes/reflections/attribute_spec.rb +6 -6
- data/spec/lib/active_data/model/attributes/represents_spec.rb +10 -78
- data/spec/lib/active_data/model/attributes_spec.rb +150 -45
- data/spec/lib/active_data/model/callbacks_spec.rb +69 -70
- data/spec/lib/active_data/model/conventions_spec.rb +0 -1
- data/spec/lib/active_data/model/dirty_spec.rb +22 -13
- data/spec/lib/active_data/model/lifecycle_spec.rb +49 -23
- data/spec/lib/active_data/model/persistence_spec.rb +5 -6
- data/spec/lib/active_data/model/representation_spec.rb +126 -0
- data/spec/lib/active_data/model/scopes_spec.rb +1 -3
- data/spec/lib/active_data/model/typecasting_spec.rb +6 -5
- data/spec/lib/active_data/model/validations/associated_spec.rb +26 -18
- data/spec/lib/active_data/model/validations/nested_spec.rb +89 -18
- data/spec/lib/active_data/model_spec.rb +1 -2
- data/spec/lib/active_data_spec.rb +0 -1
- data/spec/shared/nested_attribute_examples.rb +332 -0
- data/spec/spec_helper.rb +3 -0
- data/spec/support/model_helpers.rb +2 -2
- data/spec/support/muffle_helper.rb +7 -0
- metadata +52 -18
- data/lib/active_data/model/associations/collection/referenced.rb +0 -26
- data/lib/active_data/model/associations/reflections/reference_reflection.rb +0 -45
- data/spec/lib/active_data/model/nested_attributes.rb +0 -202
@@ -22,29 +22,50 @@ describe ActiveData::ActiveRecord::Associations do
|
|
22
22
|
|
23
23
|
attribute :first_name, String
|
24
24
|
attribute :last_name, String
|
25
|
+
attribute :admin, Boolean
|
25
26
|
end
|
26
27
|
|
27
28
|
stub_class(:user, ActiveRecord::Base) do
|
28
29
|
embeds_many :projects
|
29
30
|
embeds_one :profile
|
30
31
|
|
32
|
+
# Simulating JSON attribute
|
33
|
+
serialize :projects, JSON
|
34
|
+
|
35
|
+
def read_attribute(name)
|
36
|
+
if name.to_s == 'projects'
|
37
|
+
value = super
|
38
|
+
JSON.parse(value) if value
|
39
|
+
else
|
40
|
+
super
|
41
|
+
end
|
42
|
+
end
|
43
|
+
|
31
44
|
validates :projects, associated: true
|
32
45
|
end
|
33
46
|
end
|
34
47
|
|
35
|
-
|
36
|
-
|
48
|
+
let(:user) { User.new }
|
49
|
+
|
50
|
+
specify { expect(user.projects).to eq([]) }
|
51
|
+
specify { expect(user.profile).to be_nil }
|
37
52
|
|
38
53
|
context 'new owner' do
|
39
|
-
|
54
|
+
let(:user) { User.new }
|
40
55
|
|
41
56
|
describe '#projects' do
|
42
|
-
specify
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
specify
|
47
|
-
|
57
|
+
specify do
|
58
|
+
expect { user.projects << Project.new }
|
59
|
+
.not_to change { user.read_attribute(:projects) }
|
60
|
+
end
|
61
|
+
specify do
|
62
|
+
expect { user.projects << Project.new(title: 'First') }
|
63
|
+
.not_to change { user.read_attribute(:projects) }
|
64
|
+
end
|
65
|
+
specify do
|
66
|
+
expect { user.projects << Project.new(title: 'First') }
|
67
|
+
.not_to change { user.projects.reload.count }
|
68
|
+
end
|
48
69
|
specify do
|
49
70
|
user.projects << Project.new(title: 'First')
|
50
71
|
user.save
|
@@ -53,73 +74,111 @@ describe ActiveData::ActiveRecord::Associations do
|
|
53
74
|
end
|
54
75
|
|
55
76
|
describe '#profile' do
|
56
|
-
specify
|
57
|
-
|
58
|
-
|
59
|
-
|
77
|
+
specify do
|
78
|
+
expect { user.profile = Profile.new(first_name: 'google.com') }
|
79
|
+
.not_to change { user.read_attribute(:profile) }
|
80
|
+
end
|
81
|
+
specify do
|
82
|
+
expect { user.profile = Profile.new(first_name: 'google.com') }
|
83
|
+
.to change { user.profile }.from(nil).to(an_instance_of(Profile))
|
84
|
+
end
|
60
85
|
specify do
|
61
86
|
user.profile = Profile.new(first_name: 'google.com')
|
62
87
|
user.save
|
63
88
|
expect(user.reload.profile.first_name).to eq('google.com')
|
64
89
|
end
|
90
|
+
|
91
|
+
context 'with profile already set' do
|
92
|
+
before { user.create_profile(admin: true) }
|
93
|
+
|
94
|
+
specify do
|
95
|
+
user.profile.admin = false
|
96
|
+
user.save
|
97
|
+
expect(user.reload.profile.admin).to eq(false)
|
98
|
+
end
|
99
|
+
end
|
65
100
|
end
|
66
101
|
end
|
67
102
|
|
68
103
|
context 'persisted owner' do
|
69
|
-
|
104
|
+
let(:user) { User.create }
|
70
105
|
|
71
106
|
describe '#projects' do
|
72
|
-
specify
|
73
|
-
|
74
|
-
|
75
|
-
|
76
|
-
specify
|
107
|
+
specify do
|
108
|
+
expect { user.projects << Project.new }
|
109
|
+
.not_to change { user.read_attribute(:projects) }
|
110
|
+
end
|
111
|
+
specify do
|
112
|
+
expect { user.projects << Project.new(title: 'First') }
|
113
|
+
.to change { user.projects.reload.count }.from(0).to(1)
|
114
|
+
end
|
115
|
+
specify do
|
77
116
|
user.projects << Project.new(title: 'First')
|
78
117
|
user.save
|
79
|
-
expect(user.reload.projects.first.title).to eq('First')
|
118
|
+
expect(user.reload.projects.first.title).to eq('First')
|
119
|
+
end
|
80
120
|
|
81
121
|
context do
|
82
122
|
let(:project) { Project.new(title: 'First') }
|
83
123
|
before { project.build_author(name: 'Author') }
|
84
124
|
|
85
|
-
specify
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
125
|
+
specify do
|
126
|
+
expect { user.projects << project }
|
127
|
+
.to change { user.read_attribute(:projects) }.from(nil)
|
128
|
+
.to([{'title' => 'First', 'author' => {'name' => 'Author'}}])
|
129
|
+
end
|
130
|
+
specify do
|
131
|
+
expect do
|
132
|
+
user.projects << project
|
133
|
+
user.save
|
134
|
+
end
|
135
|
+
.to change { user.reload.read_attribute(:projects) }.from(nil)
|
136
|
+
.to([{'title' => 'First', 'author' => {'name' => 'Author'}}])
|
137
|
+
end
|
91
138
|
end
|
92
139
|
end
|
93
140
|
|
94
141
|
describe '#profile' do
|
95
|
-
specify
|
96
|
-
|
97
|
-
|
142
|
+
specify do
|
143
|
+
expect { user.profile = Profile.new(first_name: 'google.com') }
|
144
|
+
.to change { user.profile }.from(nil).to(an_instance_of(Profile))
|
145
|
+
end
|
146
|
+
specify do
|
98
147
|
user.profile = Profile.new(first_name: 'google.com')
|
99
148
|
user.save
|
100
|
-
expect(user.reload.profile.first_name).to eq('google.com')
|
101
|
-
|
102
|
-
|
103
|
-
.
|
104
|
-
|
105
|
-
|
106
|
-
|
149
|
+
expect(user.reload.profile.first_name).to eq('google.com')
|
150
|
+
end
|
151
|
+
specify do
|
152
|
+
expect { user.profile = Profile.new(first_name: 'google.com') }
|
153
|
+
.to change { user.read_attribute(:profile) }.from(nil)
|
154
|
+
.to({first_name: 'google.com', last_name: nil, admin: nil}.to_json)
|
155
|
+
end
|
156
|
+
specify do
|
157
|
+
expect do
|
158
|
+
user.profile = Profile.new(first_name: 'google.com')
|
159
|
+
user.save
|
160
|
+
end
|
161
|
+
.to change { user.reload.read_attribute(:profile) }.from(nil)
|
162
|
+
.to({first_name: 'google.com', last_name: nil, admin: nil}.to_json)
|
163
|
+
end
|
107
164
|
end
|
108
165
|
end
|
109
166
|
|
110
167
|
context 'class determine errors' do
|
111
168
|
specify do
|
112
|
-
expect
|
113
|
-
|
114
|
-
|
169
|
+
expect do
|
170
|
+
stub_class(:book, ActiveRecord::Base) do
|
171
|
+
embeds_one :author, class_name: 'Borogoves'
|
172
|
+
end.reflect_on_association(:author).klass end.to raise_error NameError
|
115
173
|
end
|
116
174
|
|
117
175
|
specify do
|
118
|
-
expect
|
119
|
-
|
120
|
-
|
121
|
-
|
122
|
-
|
176
|
+
expect do
|
177
|
+
stub_class(:user, ActiveRecord::Base) do
|
178
|
+
embeds_many :projects, class_name: 'Borogoves' do
|
179
|
+
attribute :title
|
180
|
+
end
|
181
|
+
end.reflect_on_association(:projects).klass end.to raise_error NameError
|
123
182
|
end
|
124
183
|
end
|
125
184
|
|
@@ -144,6 +203,9 @@ describe ActiveData::ActiveRecord::Associations do
|
|
144
203
|
specify { expect(User.reflect_on_association(:profile).klass).to be < Profile }
|
145
204
|
specify { expect(User.new.profile).to be_nil }
|
146
205
|
specify { expect(User.new.tap { |u| u.create_profile(first_name: 'Profile') }.profile).to be_a(User::Profile) }
|
147
|
-
specify
|
206
|
+
specify do
|
207
|
+
expect(User.new.tap { |u| u.create_profile(first_name: 'Profile') }.read_attribute(:profile))
|
208
|
+
.to eq({first_name: 'Profile', last_name: nil, admin: nil, age: nil}.to_json)
|
209
|
+
end
|
148
210
|
end
|
149
211
|
end
|
@@ -1,4 +1,3 @@
|
|
1
|
-
# encoding: UTF-8
|
2
1
|
require 'spec_helper'
|
3
2
|
|
4
3
|
describe ActiveData::Config do
|
@@ -6,14 +5,18 @@ describe ActiveData::Config do
|
|
6
5
|
|
7
6
|
describe '#include_root_in_json' do
|
8
7
|
its(:include_root_in_json) { should == false }
|
9
|
-
specify
|
10
|
-
|
8
|
+
specify do
|
9
|
+
expect { subject.include_root_in_json = true }
|
10
|
+
.to change { subject.include_root_in_json }.from(false).to(true)
|
11
|
+
end
|
11
12
|
end
|
12
13
|
|
13
14
|
describe '#i18n_scope' do
|
14
15
|
its(:i18n_scope) { should == :active_data }
|
15
|
-
specify
|
16
|
-
|
16
|
+
specify do
|
17
|
+
expect { subject.i18n_scope = :data_model }
|
18
|
+
.to change { subject.i18n_scope }.from(:active_data).to(:data_model)
|
19
|
+
end
|
17
20
|
end
|
18
21
|
|
19
22
|
describe '#logger' do
|
@@ -22,23 +25,42 @@ describe ActiveData::Config do
|
|
22
25
|
|
23
26
|
describe '#primary_attribute' do
|
24
27
|
its(:primary_attribute) { should == :id }
|
25
|
-
specify
|
26
|
-
|
28
|
+
specify do
|
29
|
+
expect { subject.primary_attribute = :identified }
|
30
|
+
.to change { subject.primary_attribute }.from(:id).to(:identified)
|
31
|
+
end
|
27
32
|
end
|
28
33
|
|
29
34
|
describe '#normalizer' do
|
30
|
-
specify
|
31
|
-
|
35
|
+
specify do
|
36
|
+
expect { subject.normalizer(:name) {} }
|
37
|
+
.to change {
|
38
|
+
begin
|
39
|
+
subject.normalizer(:name)
|
40
|
+
rescue ActiveData::NormalizerMissing
|
41
|
+
nil
|
42
|
+
end
|
43
|
+
}.from(nil).to(an_instance_of(Proc))
|
44
|
+
end
|
32
45
|
specify { expect { subject.normalizer(:wrong) }.to raise_error ActiveData::NormalizerMissing }
|
33
46
|
end
|
34
47
|
|
35
48
|
describe '#typecaster' do
|
36
|
-
specify
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
|
49
|
+
specify do
|
50
|
+
expect { subject.typecaster('Object') {} }
|
51
|
+
.to change { muffle(ActiveData::TypecasterMissing) { subject.typecaster(Time, Object) } }
|
52
|
+
.from(nil).to(an_instance_of(Proc))
|
53
|
+
end
|
54
|
+
specify do
|
55
|
+
expect { subject.typecaster('Object') {} }
|
56
|
+
.to change { muffle(ActiveData::TypecasterMissing) { subject.typecaster('time', 'object') } }
|
57
|
+
.from(nil).to(an_instance_of(Proc))
|
58
|
+
end
|
59
|
+
specify do
|
60
|
+
expect { subject.typecaster('Object') {} }
|
61
|
+
.to change { muffle(ActiveData::TypecasterMissing) { subject.typecaster(Object) } }
|
62
|
+
.from(nil).to(an_instance_of(Proc))
|
63
|
+
end
|
42
64
|
specify { expect { subject.typecaster(Object) }.to raise_error ActiveData::TypecasterMissing }
|
43
65
|
end
|
44
66
|
end
|