secretary-rails 1.0.0.beta1 → 1.0.0.beta2
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.
- data/README.md +49 -17
- data/lib/generators/secretary/install_generator.rb +1 -1
- data/lib/secretary/dirty/collection_association.rb +74 -0
- data/lib/secretary/dirty/singular_association.rb +84 -0
- data/lib/secretary/dirty.rb +8 -0
- data/lib/secretary/errors.rb +22 -6
- data/lib/secretary/gem_version.rb +1 -1
- data/lib/secretary/has_secretary.rb +13 -3
- data/lib/secretary/tracks_association.rb +33 -75
- data/lib/secretary/versioned_attributes.rb +0 -3
- data/lib/secretary.rb +1 -0
- data/spec/factories.rb +11 -0
- data/spec/internal/app/models/animal.rb +1 -3
- data/spec/internal/app/models/car.rb +2 -0
- data/spec/internal/app/models/image.rb +6 -0
- data/spec/internal/app/models/person.rb +2 -6
- data/spec/internal/app/models/story.rb +4 -0
- data/spec/internal/db/combustion_test.sqlite +0 -0
- data/spec/internal/db/schema.rb +14 -0
- data/spec/internal/log/test.log +8329 -0
- data/spec/lib/secretary/dirty/collection_association_spec.rb +186 -0
- data/spec/lib/secretary/dirty/singular_association_spec.rb +248 -0
- data/spec/lib/secretary/has_secretary_spec.rb +9 -0
- data/spec/lib/secretary/tracks_association_spec.rb +6 -198
- data/spec/spec_helper.rb +1 -1
- metadata +29 -18
- /data/spec/tmp/db/migrate/{20131105082639_create_versions.rb → 20131106020537_create_versions.rb} +0 -0
@@ -0,0 +1,186 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Secretary::Dirty::CollectionAssociation do
|
4
|
+
let(:person) { create :person }
|
5
|
+
let(:animal) { create :animal, name: "Bob", color: "dog" }
|
6
|
+
|
7
|
+
it 'sets associations_were' do
|
8
|
+
person.animals << animal
|
9
|
+
person.animals_were.should eq []
|
10
|
+
end
|
11
|
+
|
12
|
+
it 'marks the association as changed when the association is changed' do
|
13
|
+
person.animals << animal
|
14
|
+
person.animals_changed?.should eq true
|
15
|
+
end
|
16
|
+
|
17
|
+
it 'clears out the dirty association after commit' do
|
18
|
+
person.animals << animal
|
19
|
+
person.animals_changed?.should eq true
|
20
|
+
person.save!
|
21
|
+
person.animals_changed?.should eq false
|
22
|
+
end
|
23
|
+
|
24
|
+
it "creates a new version when adding" do
|
25
|
+
person.animals = [animal]
|
26
|
+
person.save!
|
27
|
+
|
28
|
+
person.versions.count.should eq 2
|
29
|
+
version = person.versions.last
|
30
|
+
version.object_changes["animals"][0].should eq []
|
31
|
+
|
32
|
+
version.object_changes["animals"][1].should eq [
|
33
|
+
{"name" => "Bob", "color" => "dog"}
|
34
|
+
]
|
35
|
+
end
|
36
|
+
|
37
|
+
it 'makes a version when adding' do
|
38
|
+
person = create :person
|
39
|
+
animal = build :animal, name: "Bryan", color: 'lame'
|
40
|
+
|
41
|
+
person.animals << animal
|
42
|
+
person.save!
|
43
|
+
person.versions.count.should eq 2
|
44
|
+
|
45
|
+
versions = person.versions.order('version_number').to_a
|
46
|
+
versions.last.object_changes["animals"][0].should eq []
|
47
|
+
versions.last.object_changes["animals"][1].should eq [{
|
48
|
+
"name" => "Bryan",
|
49
|
+
"color" => "lame"
|
50
|
+
}]
|
51
|
+
end
|
52
|
+
|
53
|
+
it 'makes a version when removing' do
|
54
|
+
person = build :person
|
55
|
+
animal = build :animal, name: "Bryan", color: 'lame'
|
56
|
+
person.animals = [animal]
|
57
|
+
person.save!
|
58
|
+
person.versions.count.should eq 1
|
59
|
+
|
60
|
+
person.animals = []
|
61
|
+
person.save!
|
62
|
+
person.versions.count.should eq 2
|
63
|
+
|
64
|
+
versions = person.versions.order('version_number').to_a
|
65
|
+
versions.last.object_changes["animals"][0].should eq [{
|
66
|
+
"name" => "Bryan",
|
67
|
+
"color" => "lame"
|
68
|
+
}]
|
69
|
+
versions.last.object_changes["animals"][1].should eq []
|
70
|
+
end
|
71
|
+
|
72
|
+
|
73
|
+
context 'with accepts_nested_attributes_for' do
|
74
|
+
it 'adds a new version when adding to collection' do
|
75
|
+
animals_attributes = [
|
76
|
+
{
|
77
|
+
"name" => "George",
|
78
|
+
"color" => "yes"
|
79
|
+
}
|
80
|
+
]
|
81
|
+
|
82
|
+
person.animals_attributes = animals_attributes
|
83
|
+
person.animals_were.should eq []
|
84
|
+
person.save!
|
85
|
+
person.versions.count.should eq 2
|
86
|
+
|
87
|
+
version = person.versions.order('version_number').last
|
88
|
+
version.object_changes["animals"][0].should eq []
|
89
|
+
version.object_changes["animals"][1].should eq [{
|
90
|
+
"name" => "George",
|
91
|
+
"color" => "yes"
|
92
|
+
}]
|
93
|
+
end
|
94
|
+
|
95
|
+
it 'adds a new version when changing something in collection' do
|
96
|
+
animals_attributes = [
|
97
|
+
{
|
98
|
+
"id" => animal.id,
|
99
|
+
"name" => "Lemon"
|
100
|
+
}
|
101
|
+
]
|
102
|
+
|
103
|
+
person.animals << animal
|
104
|
+
person.save!
|
105
|
+
person.versions.count.should eq 2
|
106
|
+
person.animals_attributes = animals_attributes
|
107
|
+
person.save!
|
108
|
+
person.versions.count.should eq 3
|
109
|
+
|
110
|
+
version = person.versions.order('version_number').last
|
111
|
+
version.object_changes["animals"][0].should eq [{
|
112
|
+
"name" => "Bob",
|
113
|
+
"color" => "dog"
|
114
|
+
}]
|
115
|
+
version.object_changes["animals"][1].should eq [{
|
116
|
+
"name" => "Lemon",
|
117
|
+
"color" => "dog"
|
118
|
+
}]
|
119
|
+
end
|
120
|
+
|
121
|
+
it 'adds a new version when removing something from collection' do
|
122
|
+
animals_attributes = [
|
123
|
+
{
|
124
|
+
"id" => animal.id,
|
125
|
+
"_destroy" => "1"
|
126
|
+
}
|
127
|
+
]
|
128
|
+
|
129
|
+
person.animals << animal
|
130
|
+
person.save!
|
131
|
+
person.versions.count.should eq 2
|
132
|
+
person.animals_attributes = animals_attributes
|
133
|
+
person.save!
|
134
|
+
person.versions.count.should eq 3
|
135
|
+
|
136
|
+
version = person.versions.order('version_number').last
|
137
|
+
version.object_changes["animals"][0].should eq [{
|
138
|
+
"name" => "Bob",
|
139
|
+
"color" => "dog"
|
140
|
+
}]
|
141
|
+
version.object_changes["animals"][1].should eq []
|
142
|
+
end
|
143
|
+
|
144
|
+
it 'does not add a new version if nothing has changed' do
|
145
|
+
animals_attributes = [
|
146
|
+
{
|
147
|
+
"id" => animal.id,
|
148
|
+
"name" => "Bob"
|
149
|
+
}
|
150
|
+
]
|
151
|
+
|
152
|
+
person.animals << animal
|
153
|
+
person.save!
|
154
|
+
person.versions.count.should eq 2
|
155
|
+
# this doesn't call before_add/remove callbacks
|
156
|
+
person.animals_attributes = animals_attributes
|
157
|
+
person.save!
|
158
|
+
person.versions.count.should eq 2
|
159
|
+
end
|
160
|
+
end
|
161
|
+
|
162
|
+
describe '#association_changed?' do
|
163
|
+
it 'is true if the association has changed' do
|
164
|
+
person.animals_changed?.should eq false
|
165
|
+
person.animals << animal
|
166
|
+
person.animals_changed?.should eq true
|
167
|
+
end
|
168
|
+
|
169
|
+
it 'is false after the parent object has been saved' do
|
170
|
+
person.animals << animal
|
171
|
+
person.animals_changed?.should eq true
|
172
|
+
person.save!
|
173
|
+
person.animals_changed?.should eq false
|
174
|
+
end
|
175
|
+
|
176
|
+
it 'is false if the association has not changed' do
|
177
|
+
person.animals << animal
|
178
|
+
person.animals_changed?.should eq true
|
179
|
+
person.save!
|
180
|
+
person.animals_changed?.should eq false
|
181
|
+
|
182
|
+
person.animals = [animal]
|
183
|
+
person.animals_changed?.should eq false
|
184
|
+
end
|
185
|
+
end
|
186
|
+
end
|
@@ -0,0 +1,248 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe Secretary::Dirty::SingularAssociation do
|
4
|
+
describe 'has_one' do
|
5
|
+
let(:story) { create :story }
|
6
|
+
let(:image) { create :image, title: "Superman", url: "superman.jpg" }
|
7
|
+
|
8
|
+
it 'sets association_was' do
|
9
|
+
story.image = image
|
10
|
+
story.image_was.should eq nil
|
11
|
+
story.save!
|
12
|
+
story.image_was.should eq image
|
13
|
+
end
|
14
|
+
|
15
|
+
it 'marks the association as changed when the association is changed' do
|
16
|
+
story.image = image
|
17
|
+
story.image_changed?.should eq true
|
18
|
+
end
|
19
|
+
|
20
|
+
it 'clears out the dirty association after commit' do
|
21
|
+
story.image = image
|
22
|
+
story.image_changed?.should eq true
|
23
|
+
story.save!
|
24
|
+
story.image_changed?.should eq false
|
25
|
+
end
|
26
|
+
|
27
|
+
it "creates a new version when setting the association" do
|
28
|
+
story.image = image
|
29
|
+
story.save!
|
30
|
+
|
31
|
+
story.versions.count.should eq 2
|
32
|
+
version = story.versions.last
|
33
|
+
version.object_changes["image"][0].should eq Hash[]
|
34
|
+
|
35
|
+
version.object_changes["image"][1].should eq Hash[{
|
36
|
+
"title" => "Superman",
|
37
|
+
"url" => "superman.jpg"
|
38
|
+
}]
|
39
|
+
end
|
40
|
+
|
41
|
+
it 'makes a version when removing' do
|
42
|
+
story.image = image
|
43
|
+
story.save!
|
44
|
+
story.versions.count.should eq 2
|
45
|
+
|
46
|
+
story.image = nil
|
47
|
+
story.save!
|
48
|
+
story.versions.count.should eq 3
|
49
|
+
|
50
|
+
versions = story.versions.order('version_number').to_a
|
51
|
+
versions.last.object_changes["image"][0].should eq Hash[{
|
52
|
+
"title" => "Superman",
|
53
|
+
"url" => "superman.jpg"
|
54
|
+
}]
|
55
|
+
versions.last.object_changes["image"][1].should eq Hash[]
|
56
|
+
end
|
57
|
+
|
58
|
+
|
59
|
+
context 'with accepts_nested_attributes_for' do
|
60
|
+
it 'adds a new version when adding association' do
|
61
|
+
image_attributes = {
|
62
|
+
"title" => "Superman",
|
63
|
+
"url" => "super.jpg"
|
64
|
+
}
|
65
|
+
|
66
|
+
story.image_attributes = image_attributes
|
67
|
+
story.save!
|
68
|
+
story.versions.count.should eq 2
|
69
|
+
|
70
|
+
version = story.versions.order('version_number').last
|
71
|
+
version.object_changes["image"][0].should eq Hash[]
|
72
|
+
version.object_changes["image"][1].should eq Hash[{
|
73
|
+
"title" => "Superman",
|
74
|
+
"url" => "super.jpg"
|
75
|
+
}]
|
76
|
+
end
|
77
|
+
|
78
|
+
it 'adds a new version when changing the existing associated object' do
|
79
|
+
image_attributes = {
|
80
|
+
"id" => image.id,
|
81
|
+
"title" => "Lemon"
|
82
|
+
}
|
83
|
+
|
84
|
+
story.image = image
|
85
|
+
story.save!
|
86
|
+
story.versions.count.should eq 2
|
87
|
+
|
88
|
+
story.image_attributes = image_attributes
|
89
|
+
story.save!
|
90
|
+
story.versions.count.should eq 3
|
91
|
+
|
92
|
+
version = story.versions.order('version_number').last
|
93
|
+
version.object_changes["image"][0].should eq Hash[{
|
94
|
+
"title" => "Superman",
|
95
|
+
"url" => "superman.jpg"
|
96
|
+
}]
|
97
|
+
version.object_changes["image"][1].should eq Hash[{
|
98
|
+
"title" => "Lemon",
|
99
|
+
"url" => "superman.jpg"
|
100
|
+
}]
|
101
|
+
end
|
102
|
+
|
103
|
+
it 'adds a new version when removing the association' do
|
104
|
+
image_attributes = {
|
105
|
+
"id" => image.id,
|
106
|
+
"_destroy" => "1"
|
107
|
+
}
|
108
|
+
|
109
|
+
story.image = image
|
110
|
+
story.save!
|
111
|
+
story.versions.count.should eq 2
|
112
|
+
|
113
|
+
story.image_attributes = image_attributes
|
114
|
+
story.save!
|
115
|
+
story.versions.count.should eq 3
|
116
|
+
|
117
|
+
version = story.versions.order('version_number').last
|
118
|
+
version.object_changes["image"][0].should eq Hash[{
|
119
|
+
"title" => "Superman",
|
120
|
+
"url" => "superman.jpg"
|
121
|
+
}]
|
122
|
+
version.object_changes["image"][1].should eq Hash[]
|
123
|
+
end
|
124
|
+
|
125
|
+
it 'does not add a new version if nothing has changed' do
|
126
|
+
image_attributes = {
|
127
|
+
"id" => image.id,
|
128
|
+
"title" => "Superman"
|
129
|
+
}
|
130
|
+
|
131
|
+
story.image = image
|
132
|
+
story.save!
|
133
|
+
story.versions.count.should eq 2
|
134
|
+
|
135
|
+
story.image_attributes = image_attributes
|
136
|
+
story.save!
|
137
|
+
story.versions.count.should eq 2
|
138
|
+
end
|
139
|
+
end
|
140
|
+
|
141
|
+
describe '#association_changed?' do
|
142
|
+
it 'is true if the association has changed' do
|
143
|
+
story.image_changed?.should eq false
|
144
|
+
story.image = image
|
145
|
+
story.image_changed?.should eq true
|
146
|
+
end
|
147
|
+
|
148
|
+
it 'is false after the parent object has been saved' do
|
149
|
+
story.image = image
|
150
|
+
story.image_changed?.should eq true
|
151
|
+
story.save!
|
152
|
+
story.image_changed?.should eq false
|
153
|
+
end
|
154
|
+
|
155
|
+
it 'is false if the association has not changed' do
|
156
|
+
story.image = image
|
157
|
+
story.image_changed?.should eq true
|
158
|
+
story.save!
|
159
|
+
story.image_changed?.should eq false
|
160
|
+
|
161
|
+
story.image = image
|
162
|
+
story.image_changed?.should eq false
|
163
|
+
end
|
164
|
+
end
|
165
|
+
end
|
166
|
+
|
167
|
+
|
168
|
+
|
169
|
+
describe 'belongs_to' do
|
170
|
+
let(:story) { create :story, headline: "Headline", body: "Body" }
|
171
|
+
let(:image) { create :image, title: "Superman", url: "superman.jpg" }
|
172
|
+
|
173
|
+
it 'sets association_was' do
|
174
|
+
image.story = story
|
175
|
+
image.story_was.should eq nil
|
176
|
+
image.save!
|
177
|
+
image.story_was.should eq story
|
178
|
+
end
|
179
|
+
|
180
|
+
it 'marks the association as changed when the association is changed' do
|
181
|
+
image.story = story
|
182
|
+
image.story_changed?.should eq true
|
183
|
+
end
|
184
|
+
|
185
|
+
it 'clears out the dirty association after commit' do
|
186
|
+
image.story = story
|
187
|
+
image.story_changed?.should eq true
|
188
|
+
image.save!
|
189
|
+
image.story_changed?.should eq false
|
190
|
+
end
|
191
|
+
|
192
|
+
it "creates a new version when setting the association" do
|
193
|
+
image.story = story
|
194
|
+
image.save!
|
195
|
+
|
196
|
+
image.versions.count.should eq 2
|
197
|
+
version = image.versions.order('version_number').last
|
198
|
+
version.object_changes["story"][0].should eq Hash[]
|
199
|
+
|
200
|
+
version.object_changes["story"][1].should eq Hash[{
|
201
|
+
"headline" => "Headline",
|
202
|
+
"body" => "Body"
|
203
|
+
}]
|
204
|
+
end
|
205
|
+
|
206
|
+
it 'makes a version when removing' do
|
207
|
+
image.story = story
|
208
|
+
image.save!
|
209
|
+
image.versions.count.should eq 2
|
210
|
+
|
211
|
+
image.story = nil
|
212
|
+
image.save!
|
213
|
+
image.versions.count.should eq 3
|
214
|
+
|
215
|
+
versions = image.versions.order('version_number').to_a
|
216
|
+
versions.last.object_changes["story"][0].should eq Hash[{
|
217
|
+
"headline" => "Headline",
|
218
|
+
"body" => "Body"
|
219
|
+
}]
|
220
|
+
versions.last.object_changes["story"][1].should eq Hash[]
|
221
|
+
end
|
222
|
+
|
223
|
+
describe '#association_changed?' do
|
224
|
+
it 'is true if the association has changed' do
|
225
|
+
image.story_changed?.should eq false
|
226
|
+
image.story = story
|
227
|
+
image.story_changed?.should eq true
|
228
|
+
end
|
229
|
+
|
230
|
+
it 'is false after the parent object has been saved' do
|
231
|
+
image.story = story
|
232
|
+
image.story_changed?.should eq true
|
233
|
+
image.save!
|
234
|
+
image.story_changed?.should eq false
|
235
|
+
end
|
236
|
+
|
237
|
+
it 'is false if the association has not changed' do
|
238
|
+
image.story = story
|
239
|
+
image.story_changed?.should eq true
|
240
|
+
image.save!
|
241
|
+
image.story_changed?.should eq false
|
242
|
+
|
243
|
+
image.story = story
|
244
|
+
image.story_changed?.should eq false
|
245
|
+
end
|
246
|
+
end
|
247
|
+
end
|
248
|
+
end
|
@@ -39,6 +39,15 @@ describe Secretary::HasSecretary do
|
|
39
39
|
Secretary.versioned_models.should include "Story"
|
40
40
|
end
|
41
41
|
|
42
|
+
it 'sets versioned attributes if option is specified' do
|
43
|
+
Animal.versioned_attributes.should eq ["name", "color"]
|
44
|
+
end
|
45
|
+
|
46
|
+
it 'sets excluded attributes if option is specified' do
|
47
|
+
Person.versioned_attributes.should_not include "name"
|
48
|
+
Person.versioned_attributes.should_not include "ethnicity"
|
49
|
+
end
|
50
|
+
|
42
51
|
it "adds the has_many association for versions" do
|
43
52
|
new_story.versions.to_a.should eq Array.new
|
44
53
|
end
|
@@ -8,207 +8,15 @@ describe Secretary::TracksAssociation do
|
|
8
8
|
}.should raise_error Secretary::NotVersionedError
|
9
9
|
end
|
10
10
|
|
11
|
+
it "raises an error if there is no association with the given name" do
|
12
|
+
-> {
|
13
|
+
Person.tracks_association :giraffes
|
14
|
+
}.should raise_error Secretary::NoAssociationError
|
15
|
+
end
|
16
|
+
|
11
17
|
it 'adds the associations to the versioned attributes' do
|
12
18
|
Person.versioned_attributes.should include "animals"
|
13
19
|
Person.versioned_attributes.should include "hobbies"
|
14
20
|
end
|
15
21
|
end
|
16
|
-
|
17
|
-
|
18
|
-
describe 'dirty association' do
|
19
|
-
let(:person) { create :person }
|
20
|
-
let(:animal) { create :animal }
|
21
|
-
|
22
|
-
it 'sets associations_were' do
|
23
|
-
person.animals << animal
|
24
|
-
person.animals_were.should eq []
|
25
|
-
end
|
26
|
-
|
27
|
-
it 'marks the association as changed when the association is changed' do
|
28
|
-
person.animals << animal
|
29
|
-
person.animals_changed?.should eq true
|
30
|
-
end
|
31
|
-
|
32
|
-
it 'clears out the dirty association after commit' do
|
33
|
-
person.animals << animal
|
34
|
-
person.animals_changed?.should eq true
|
35
|
-
person.save!
|
36
|
-
person.animals_changed?.should eq false
|
37
|
-
end
|
38
|
-
end
|
39
|
-
|
40
|
-
|
41
|
-
describe "adding to association collections" do
|
42
|
-
let(:person) { create :person }
|
43
|
-
let(:animal) { build :animal, name: "Bob", color: "dog" }
|
44
|
-
|
45
|
-
it "creates a new version when adding" do
|
46
|
-
person.animals = [animal]
|
47
|
-
person.save!
|
48
|
-
|
49
|
-
person.versions.count.should eq 2
|
50
|
-
version = person.versions.last
|
51
|
-
version.object_changes["animals"][0].should eq []
|
52
|
-
|
53
|
-
version.object_changes["animals"][1].should eq [
|
54
|
-
{"name" => "Bob", "color" => "dog"}
|
55
|
-
]
|
56
|
-
end
|
57
|
-
|
58
|
-
it 'makes a version when adding' do
|
59
|
-
person = create :person
|
60
|
-
animal = build :animal, name: "Bryan", color: 'lame'
|
61
|
-
|
62
|
-
person.animals << animal
|
63
|
-
person.save!
|
64
|
-
person.versions.count.should eq 2
|
65
|
-
|
66
|
-
versions = person.versions.order('version_number').to_a
|
67
|
-
versions.last.object_changes["animals"][0].should eq []
|
68
|
-
versions.last.object_changes["animals"][1].should eq [{
|
69
|
-
"name" => "Bryan",
|
70
|
-
"color" => "lame"
|
71
|
-
}]
|
72
|
-
end
|
73
|
-
|
74
|
-
it 'makes a version when removing' do
|
75
|
-
person = build :person
|
76
|
-
animal = build :animal, name: "Bryan", color: 'lame'
|
77
|
-
person.animals = [animal]
|
78
|
-
person.save!
|
79
|
-
person.versions.count.should eq 1
|
80
|
-
|
81
|
-
person.animals = []
|
82
|
-
person.save!
|
83
|
-
person.versions.count.should eq 2
|
84
|
-
|
85
|
-
versions = person.versions.order('version_number').to_a
|
86
|
-
versions.last.object_changes["animals"][0].should eq [{
|
87
|
-
"name" => "Bryan",
|
88
|
-
"color" => "lame"
|
89
|
-
}]
|
90
|
-
versions.last.object_changes["animals"][1].should eq []
|
91
|
-
end
|
92
|
-
end
|
93
|
-
|
94
|
-
|
95
|
-
describe 'with accepts_nested_attributes_for' do
|
96
|
-
let(:animal) { create :animal, name: "Henry", color: "blind" }
|
97
|
-
let(:person) { create :person }
|
98
|
-
|
99
|
-
it 'adds a new version when adding to collection' do
|
100
|
-
animals_attributes = [
|
101
|
-
{
|
102
|
-
"name" => "George",
|
103
|
-
"color" => "yes"
|
104
|
-
}
|
105
|
-
]
|
106
|
-
|
107
|
-
person.animals_attributes = animals_attributes
|
108
|
-
person.save!
|
109
|
-
person.versions.count.should eq 2
|
110
|
-
|
111
|
-
version = person.versions.order('version_number').last
|
112
|
-
version.object_changes["animals"][0].should eq []
|
113
|
-
version.object_changes["animals"][1].should eq [{
|
114
|
-
"name" => "George",
|
115
|
-
"color" => "yes"
|
116
|
-
}]
|
117
|
-
end
|
118
|
-
|
119
|
-
it 'adds a new version when changing something in collection' do
|
120
|
-
animals_attributes = [
|
121
|
-
{
|
122
|
-
"id" => animal.id,
|
123
|
-
"name" => "Lemon"
|
124
|
-
}
|
125
|
-
]
|
126
|
-
|
127
|
-
person.animals << animal
|
128
|
-
person.save!
|
129
|
-
person.versions.count.should eq 2
|
130
|
-
person.animals_attributes = animals_attributes
|
131
|
-
person.save!
|
132
|
-
person.versions.count.should eq 3
|
133
|
-
|
134
|
-
version = person.versions.order('version_number').last
|
135
|
-
version.object_changes["animals"][0].should eq [{
|
136
|
-
"name" => "Henry",
|
137
|
-
"color" => "blind"
|
138
|
-
}]
|
139
|
-
version.object_changes["animals"][1].should eq [{
|
140
|
-
"name" => "Lemon",
|
141
|
-
"color" => "blind"
|
142
|
-
}]
|
143
|
-
end
|
144
|
-
|
145
|
-
it 'adds a new version when removing something from collection' do
|
146
|
-
animals_attributes = [
|
147
|
-
{
|
148
|
-
"id" => animal.id,
|
149
|
-
"_destroy" => "1"
|
150
|
-
}
|
151
|
-
]
|
152
|
-
|
153
|
-
person.animals << animal
|
154
|
-
person.save!
|
155
|
-
person.versions.count.should eq 2
|
156
|
-
person.animals_attributes = animals_attributes
|
157
|
-
person.save!
|
158
|
-
person.versions.count.should eq 3
|
159
|
-
|
160
|
-
version = person.versions.order('version_number').last
|
161
|
-
version.object_changes["animals"][0].should eq [{
|
162
|
-
"name" => "Henry",
|
163
|
-
"color" => "blind"
|
164
|
-
}]
|
165
|
-
version.object_changes["animals"][1].should eq []
|
166
|
-
end
|
167
|
-
|
168
|
-
it 'does not add a new version if nothing has changed' do
|
169
|
-
animals_attributes = [
|
170
|
-
{
|
171
|
-
"id" => animal.id,
|
172
|
-
"name" => "Henry"
|
173
|
-
}
|
174
|
-
]
|
175
|
-
|
176
|
-
person.animals << animal
|
177
|
-
person.save!
|
178
|
-
person.versions.count.should eq 2
|
179
|
-
# this doesn't call before_add/remove callbacks
|
180
|
-
person.animals_attributes = animals_attributes
|
181
|
-
person.save!
|
182
|
-
person.versions.count.should eq 2
|
183
|
-
end
|
184
|
-
end
|
185
|
-
|
186
|
-
|
187
|
-
describe '#association_changed?' do
|
188
|
-
let(:person) { create :person }
|
189
|
-
let(:animal) { create :animal }
|
190
|
-
|
191
|
-
it 'is true if the association has changed' do
|
192
|
-
person.animals_changed?.should eq false
|
193
|
-
person.animals << animal
|
194
|
-
person.animals_changed?.should eq true
|
195
|
-
end
|
196
|
-
|
197
|
-
it 'is false after the parent object has been saved' do
|
198
|
-
person.animals << animal
|
199
|
-
person.animals_changed?.should eq true
|
200
|
-
person.save!
|
201
|
-
person.animals_changed?.should eq false
|
202
|
-
end
|
203
|
-
|
204
|
-
it 'is false if the association has not changed' do
|
205
|
-
person.animals << animal
|
206
|
-
person.animals_changed?.should eq true
|
207
|
-
person.save!
|
208
|
-
person.animals_changed?.should eq false
|
209
|
-
|
210
|
-
person.animals = [animal]
|
211
|
-
person.animals_changed?.should eq false
|
212
|
-
end
|
213
|
-
end
|
214
22
|
end
|