activehistory 0.1 → 0.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/Gemfile.lock +5 -2
- data/activehistory.gemspec +2 -1
- data/lib/activehistory.rb +17 -4
- data/lib/activehistory/action.rb +5 -3
- data/lib/activehistory/adapters/active_record.rb +329 -105
- data/lib/activehistory/connection.rb +11 -23
- data/lib/activehistory/event.rb +79 -25
- data/lib/activehistory/version.rb +2 -2
- data/test/active_record_adapter/association_test/belongs_to_association_test.rb +46 -38
- data/test/active_record_adapter/association_test/has_and_belongs_to_many_test.rb +96 -63
- data/test/active_record_adapter/association_test/has_many_association_test.rb +166 -27
- data/test/active_record_adapter/association_test/has_one_association_test.rb +36 -40
- data/test/active_record_adapter/create_test.rb +10 -14
- data/test/active_record_adapter/destroy_test.rb +4 -6
- data/test/active_record_adapter/event_test.rb +33 -7
- data/test/active_record_adapter/save_test.rb +8 -12
- data/test/activehistory_test.rb +96 -0
- data/test/models.rb +1 -1
- data/test/test_helper.rb +12 -1
- metadata +17 -2
@@ -10,72 +10,211 @@ class HasManyAssociationTest < ActiveSupport::TestCase
|
|
10
10
|
test '::create with has_many association' do
|
11
11
|
@property = create(:property)
|
12
12
|
WebMock::RequestRegistry.instance.reset!
|
13
|
-
|
13
|
+
|
14
14
|
@photo = travel_to(@time) { create(:photo, property: @property) }
|
15
15
|
|
16
|
-
assert_posted("/events") do
|
17
|
-
|
18
|
-
assert_equal 2, req_data['actions'].size
|
19
|
-
|
20
|
-
assert_equal req_data['actions'][0], {
|
16
|
+
assert_posted("/events") do
|
17
|
+
assert_action_for @photo, {
|
21
18
|
diff: {
|
22
19
|
id: [nil, @photo.id],
|
23
20
|
property_id: [nil, @property.id],
|
24
21
|
format: [nil, @photo.format]
|
25
22
|
},
|
26
|
-
|
23
|
+
subject_type: "Photo",
|
24
|
+
subject_id: @photo.id,
|
27
25
|
timestamp: @time.iso8601(3),
|
28
26
|
type: 'create'
|
29
|
-
}
|
27
|
+
}
|
30
28
|
|
31
|
-
|
29
|
+
assert_action_for @property, {
|
32
30
|
timestamp: @time.iso8601(3),
|
33
31
|
type: 'update',
|
34
|
-
|
32
|
+
subject_type: "Property",
|
33
|
+
subject_id: @property.id,
|
35
34
|
diff: {
|
36
35
|
photo_ids: [[], [@photo.id]]
|
37
36
|
}
|
38
|
-
}
|
37
|
+
}
|
39
38
|
end
|
40
39
|
end
|
41
|
-
|
40
|
+
|
42
41
|
test '::update with has_many association' do
|
43
42
|
@property = create(:property)
|
44
43
|
@photo = create(:photo, property: @property)
|
45
44
|
WebMock::RequestRegistry.instance.reset!
|
46
|
-
|
45
|
+
|
47
46
|
travel_to(@time) { @photo.update(property: nil) }
|
48
|
-
|
49
|
-
assert_posted("/events") do
|
50
|
-
|
51
|
-
assert_equal 2, req_data['actions'].size
|
52
|
-
|
53
|
-
assert_equal req_data['actions'][0], {
|
47
|
+
|
48
|
+
assert_posted("/events") do
|
49
|
+
assert_action_for @photo, {
|
54
50
|
diff: {
|
55
51
|
property_id: [@property.id, nil]
|
56
52
|
},
|
57
|
-
|
53
|
+
subject_type: "Photo",
|
54
|
+
subject_id: @photo.id,
|
58
55
|
timestamp: @time.iso8601(3),
|
59
56
|
type: 'update'
|
60
|
-
}
|
57
|
+
}
|
61
58
|
|
62
|
-
|
59
|
+
assert_action_for @property, {
|
63
60
|
timestamp: @time.iso8601(3),
|
64
61
|
type: 'update',
|
65
|
-
|
62
|
+
subject_type: "Property",
|
63
|
+
subject_id: @property.id,
|
66
64
|
diff: {
|
67
65
|
photo_ids: [[@photo.id], []]
|
68
66
|
}
|
69
|
-
}
|
67
|
+
}
|
70
68
|
end
|
71
69
|
end
|
72
70
|
|
73
71
|
test 'has_many <<'
|
74
72
|
test 'has_many.delete'
|
75
73
|
test 'has_many.destroy'
|
76
|
-
|
77
|
-
test '
|
78
|
-
|
74
|
+
|
75
|
+
test 'has_many=' do
|
76
|
+
@property = create(:property)
|
77
|
+
@photo1 = create(:photo)
|
78
|
+
@photo2 = create(:photo)
|
79
|
+
WebMock::RequestRegistry.instance.reset!
|
80
|
+
|
81
|
+
travel_to(@time) { @property.photos = [@photo1] }
|
82
|
+
assert_posted("/events") do
|
83
|
+
assert_action_for @photo1, {
|
84
|
+
diff: { property_id: [nil, @property.id] },
|
85
|
+
subject_type: "Photo",
|
86
|
+
subject_id: @photo1.id,
|
87
|
+
timestamp: @time.iso8601(3),
|
88
|
+
type: 'update'
|
89
|
+
}
|
90
|
+
|
91
|
+
assert_action_for @property, {
|
92
|
+
diff: { photo_ids: [[], [@photo1.id]] },
|
93
|
+
subject_type: "Property",
|
94
|
+
subject_id: @property.id,
|
95
|
+
timestamp: @time.iso8601(3),
|
96
|
+
type: 'update'
|
97
|
+
}
|
98
|
+
end
|
99
|
+
|
100
|
+
WebMock::RequestRegistry.instance.reset!
|
101
|
+
travel_to(@time) { @property.photos = [@photo2] }
|
102
|
+
assert_posted("/events") do
|
103
|
+
assert_action_for @photo2, {
|
104
|
+
diff: { property_id: [nil, @property.id] },
|
105
|
+
subject_type: "Photo",
|
106
|
+
subject_id: @photo2.id,
|
107
|
+
timestamp: @time.iso8601(3),
|
108
|
+
type: 'update'
|
109
|
+
}
|
110
|
+
|
111
|
+
assert_action_for @property, {
|
112
|
+
diff: { photo_ids: [[@photo1.id], [@photo2.id]] },
|
113
|
+
subject_type: "Property",
|
114
|
+
subject_id: @property.id,
|
115
|
+
timestamp: @time.iso8601(3),
|
116
|
+
type: 'update'
|
117
|
+
}
|
118
|
+
|
119
|
+
assert_action_for @photo1, {
|
120
|
+
diff: { property_id: [@property.id, nil] },
|
121
|
+
subject_type: "Photo",
|
122
|
+
subject_id: @photo1.id,
|
123
|
+
timestamp: @time.iso8601(3),
|
124
|
+
type: 'update'
|
125
|
+
}
|
126
|
+
end
|
127
|
+
end
|
128
|
+
|
129
|
+
test 'has_many_ids=' do
|
130
|
+
@property = create(:property)
|
131
|
+
@photo1 = create(:photo)
|
132
|
+
@photo2 = create(:photo)
|
133
|
+
WebMock::RequestRegistry.instance.reset!
|
134
|
+
|
135
|
+
travel_to(@time) { @property.photo_ids = [@photo1].map(&:id) }
|
136
|
+
assert_posted("/events") do
|
137
|
+
assert_action_for @property, {
|
138
|
+
diff: { photo_ids: [[], [@photo1.id]] },
|
139
|
+
subject_type: "Property",
|
140
|
+
subject_id: @property.id,
|
141
|
+
timestamp: @time.iso8601(3),
|
142
|
+
type: 'update'
|
143
|
+
}
|
144
|
+
|
145
|
+
assert_action_for @photo1, {
|
146
|
+
diff: { property_id: [nil, @property.id] },
|
147
|
+
subject_type: "Photo",
|
148
|
+
subject_id: @photo1.id,
|
149
|
+
timestamp: @time.iso8601(3),
|
150
|
+
type: 'update'
|
151
|
+
}
|
152
|
+
end
|
153
|
+
|
154
|
+
WebMock::RequestRegistry.instance.reset!
|
155
|
+
travel_to(@time) { @property.photo_ids = [@photo2].map(&:id) }
|
156
|
+
assert_posted("/events") do
|
157
|
+
assert_action_for @photo2, {
|
158
|
+
diff: { property_id: [nil, @property.id] },
|
159
|
+
subject_type: "Photo",
|
160
|
+
subject_id: @photo2.id,
|
161
|
+
timestamp: @time.iso8601(3),
|
162
|
+
type: 'update'
|
163
|
+
}
|
164
|
+
|
165
|
+
assert_action_for @property, {
|
166
|
+
diff: { photo_ids: [[@photo1.id], [@photo2.id]] },
|
167
|
+
subject_type: "Property",
|
168
|
+
subject_id: @property.id,
|
169
|
+
timestamp: @time.iso8601(3),
|
170
|
+
type: 'update'
|
171
|
+
}
|
172
|
+
|
173
|
+
assert_action_for @photo1, {
|
174
|
+
diff: { property_id: [@property.id, nil] },
|
175
|
+
subject_type: "Photo",
|
176
|
+
subject_id: @photo1.id,
|
177
|
+
timestamp: @time.iso8601(3),
|
178
|
+
type: 'update'
|
179
|
+
}
|
180
|
+
end
|
181
|
+
end
|
182
|
+
|
183
|
+
test 'has_many.clear' do
|
184
|
+
@photo1 = create(:photo)
|
185
|
+
@photo2 = create(:photo)
|
186
|
+
@property = create(:property, photos: [@photo1, @photo2])
|
187
|
+
WebMock::RequestRegistry.instance.reset!
|
188
|
+
|
189
|
+
travel_to(@time) { @property.photos.clear }
|
190
|
+
assert_posted("/events") do |req|
|
191
|
+
|
192
|
+
assert_action_for @property, {
|
193
|
+
diff: { photo_ids: [[@photo1, @photo2].map(&:id), []] },
|
194
|
+
subject_type: "Property",
|
195
|
+
subject_id: @property.id,
|
196
|
+
timestamp: @time.iso8601(3),
|
197
|
+
type: 'update'
|
198
|
+
}
|
199
|
+
|
200
|
+
assert_action_for @photo1, {
|
201
|
+
diff: { property_id: [@property.id, nil] },
|
202
|
+
subject_type: "Photo",
|
203
|
+
subject_id: @photo1.id,
|
204
|
+
timestamp: @time.iso8601(3),
|
205
|
+
type: 'update'
|
206
|
+
}
|
207
|
+
|
208
|
+
assert_action_for @photo2, {
|
209
|
+
diff: { property_id: [@property.id, nil] },
|
210
|
+
subject_type: "Photo",
|
211
|
+
subject_id: @photo2.id,
|
212
|
+
timestamp: @time.iso8601(3),
|
213
|
+
type: 'update'
|
214
|
+
}
|
215
|
+
end
|
216
|
+
end
|
217
|
+
|
79
218
|
test 'has_many.create'
|
80
219
|
|
81
220
|
end
|
@@ -13,30 +13,29 @@ class HasOneAssociationTest < ActiveSupport::TestCase
|
|
13
13
|
|
14
14
|
@account = travel_to(@time) { create(:account, email_address: @email_address) }
|
15
15
|
|
16
|
-
assert_posted("/events") do
|
17
|
-
|
18
|
-
assert_equal 2, req_data['actions'].size
|
19
|
-
|
20
|
-
assert_equal req_data['actions'][0], {
|
16
|
+
assert_posted("/events") do
|
17
|
+
assert_action_for @account, {
|
21
18
|
diff: {
|
22
19
|
id: [nil, @account.id],
|
23
20
|
name: [nil, @account.name],
|
24
21
|
photos_count: [nil, 0],
|
25
22
|
email_address_id: [nil, @email_address.id]
|
26
23
|
},
|
27
|
-
|
24
|
+
subject_type: "Account",
|
25
|
+
subject_id: @account.id,
|
28
26
|
timestamp: @time.iso8601(3),
|
29
27
|
type: 'create'
|
30
|
-
}
|
28
|
+
}
|
31
29
|
|
32
|
-
|
30
|
+
assert_action_for @email_address, {
|
33
31
|
timestamp: @time.iso8601(3),
|
34
32
|
type: 'update',
|
35
|
-
|
33
|
+
subject_type: "EmailAddress",
|
34
|
+
subject_id: @email_address.id,
|
36
35
|
diff: {
|
37
36
|
account_id: [nil, @account.id]
|
38
37
|
}
|
39
|
-
}
|
38
|
+
}
|
40
39
|
end
|
41
40
|
end
|
42
41
|
|
@@ -46,61 +45,59 @@ class HasOneAssociationTest < ActiveSupport::TestCase
|
|
46
45
|
|
47
46
|
@email_address = travel_to(@time) { create(:email_address, account: @account) }
|
48
47
|
|
49
|
-
assert_posted("/events") do
|
50
|
-
|
51
|
-
assert_equal 2, req_data['actions'].size
|
52
|
-
|
53
|
-
assert_equal req_data['actions'][0], {
|
48
|
+
assert_posted("/events") do
|
49
|
+
assert_action_for @email_address, {
|
54
50
|
diff: {
|
55
51
|
id: [nil, @email_address.id],
|
56
52
|
address: [nil, @email_address.address],
|
57
53
|
account_id: [nil, @account.id]
|
58
54
|
},
|
59
|
-
|
55
|
+
subject_type: "EmailAddress",
|
56
|
+
subject_id: @email_address.id,
|
60
57
|
timestamp: @time.iso8601(3),
|
61
58
|
type: 'create'
|
62
|
-
}
|
59
|
+
}
|
63
60
|
|
64
|
-
|
61
|
+
assert_action_for @account, {
|
65
62
|
timestamp: @time.iso8601(3),
|
66
63
|
type: 'update',
|
67
|
-
|
64
|
+
subject_type: "Account",
|
65
|
+
subject_id: @account.id,
|
68
66
|
diff: {
|
69
67
|
email_address_id: [nil, @email_address.id]
|
70
68
|
}
|
71
|
-
}
|
69
|
+
}
|
72
70
|
end
|
73
71
|
end
|
74
72
|
|
75
73
|
test '::create with has_one association 2' do
|
76
74
|
@account = travel_to(@time) { create(:account, email_address: build(:email_address)) }
|
77
75
|
|
78
|
-
assert_posted("/events") do
|
79
|
-
|
80
|
-
assert_equal 2, req_data['actions'].size
|
81
|
-
|
82
|
-
assert_equal req_data['actions'][0], {
|
76
|
+
assert_posted("/events") do
|
77
|
+
assert_action_for @account, {
|
83
78
|
diff: {
|
84
79
|
id: [nil, @account.id],
|
85
80
|
name: [nil, @account.name],
|
86
81
|
photos_count: [nil, 0],
|
87
82
|
email_address_id: [nil, @account.email_address.id]
|
88
83
|
},
|
89
|
-
|
84
|
+
subject_type: "Account",
|
85
|
+
subject_id: @account.id,
|
90
86
|
timestamp: @time.iso8601(3),
|
91
87
|
type: 'create'
|
92
|
-
}
|
88
|
+
}
|
93
89
|
|
94
|
-
|
90
|
+
assert_action_for @account.email_address, {
|
95
91
|
timestamp: @time.iso8601(3),
|
96
92
|
type: 'create',
|
97
|
-
|
93
|
+
subject_type: "EmailAddress",
|
94
|
+
subject_id: @account.email_address.id,
|
98
95
|
diff: {
|
99
96
|
id: [nil, @account.email_address.id],
|
100
97
|
address: [nil, @account.email_address.address],
|
101
98
|
account_id: [nil, @account.id]
|
102
99
|
}
|
103
|
-
}
|
100
|
+
}
|
104
101
|
end
|
105
102
|
end
|
106
103
|
|
@@ -111,27 +108,26 @@ class HasOneAssociationTest < ActiveSupport::TestCase
|
|
111
108
|
@email_address = @account.email_address
|
112
109
|
travel_to(@time) { @account.update(email_address: nil) }
|
113
110
|
|
114
|
-
assert_posted("/events") do
|
115
|
-
|
116
|
-
assert_equal 2, req_data['actions'].size
|
117
|
-
|
118
|
-
assert_equal req_data['actions'][0], {
|
111
|
+
assert_posted("/events") do
|
112
|
+
assert_action_for @email_address, {
|
119
113
|
diff: {
|
120
114
|
account_id: [@account.id, nil]
|
121
115
|
},
|
122
|
-
|
116
|
+
subject_type: "EmailAddress",
|
117
|
+
subject_id: @email_address.id,
|
123
118
|
timestamp: @time.iso8601(3),
|
124
119
|
type: 'update'
|
125
|
-
}
|
120
|
+
}
|
126
121
|
|
127
|
-
|
122
|
+
assert_action_for @account, {
|
128
123
|
timestamp: @time.iso8601(3),
|
129
124
|
type: 'update',
|
130
|
-
|
125
|
+
subject_type: "Account",
|
126
|
+
subject_id: @account.id,
|
131
127
|
diff: {
|
132
128
|
email_address_id: [@email_address.id, nil]
|
133
129
|
}
|
134
|
-
}
|
130
|
+
}
|
135
131
|
end
|
136
132
|
end
|
137
133
|
|
@@ -10,11 +10,8 @@ class CreateTest < ActiveSupport::TestCase
|
|
10
10
|
test '::create creates an Action' do
|
11
11
|
@property = travel_to(@time) { create(:property) }
|
12
12
|
|
13
|
-
assert_posted("/events") do
|
14
|
-
|
15
|
-
assert_equal 1, req_data['actions'].size
|
16
|
-
|
17
|
-
assert_equal req_data['actions'][0], {
|
13
|
+
assert_posted("/events") do
|
14
|
+
assert_action_for @property, {
|
18
15
|
diff: {
|
19
16
|
id: [nil, @property.id],
|
20
17
|
name: [nil, @property.name],
|
@@ -25,30 +22,29 @@ class CreateTest < ActiveSupport::TestCase
|
|
25
22
|
active: [nil, @property.active],
|
26
23
|
created_at: [nil, @property.created_at]
|
27
24
|
},
|
28
|
-
|
25
|
+
subject_type: "Property",
|
26
|
+
subject_id: @property.id,
|
29
27
|
timestamp: @time.iso8601(3),
|
30
28
|
type: 'create'
|
31
|
-
}
|
29
|
+
}
|
32
30
|
end
|
33
31
|
end
|
34
32
|
|
35
33
|
test "::create creates an Action without :excluded attributes" do
|
36
34
|
@comment = travel_to(@time) { create(:comment) }
|
37
35
|
|
38
|
-
assert_posted("/events") do
|
39
|
-
|
40
|
-
assert_equal 1, req_data['actions'].size
|
41
|
-
|
42
|
-
assert_equal req_data['actions'][0], {
|
36
|
+
assert_posted("/events") do
|
37
|
+
assert_action_for @comment, {
|
43
38
|
diff: {
|
44
39
|
id: [nil, @comment.id],
|
45
40
|
# No Title
|
46
41
|
body: [nil, @comment.body]
|
47
42
|
},
|
48
|
-
|
43
|
+
subject_type: "Comment",
|
44
|
+
subject_id: @comment.id,
|
49
45
|
timestamp: @time.iso8601(3),
|
50
46
|
type: 'create'
|
51
|
-
}
|
47
|
+
}
|
52
48
|
end
|
53
49
|
end
|
54
50
|
|
@@ -13,14 +13,12 @@ class DestroyTest < ActiveSupport::TestCase
|
|
13
13
|
|
14
14
|
travel_to(@time) { @property.destroy }
|
15
15
|
|
16
|
-
assert_posted("/events") do
|
17
|
-
|
18
|
-
assert_equal 1, req_data['actions'].size
|
19
|
-
|
20
|
-
assert_equal req_data['actions'][0], {
|
16
|
+
assert_posted("/events") do
|
17
|
+
assert_action_for @property, {
|
21
18
|
timestamp: @time.iso8601(3),
|
22
19
|
type: 'destroy',
|
23
|
-
|
20
|
+
subject_type: "Property",
|
21
|
+
subject_id: @property.id,
|
24
22
|
diff: {
|
25
23
|
id: [@property.id, nil],
|
26
24
|
name: [@property.name, nil],
|