public_activity 1.6.4 → 2.0.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +26 -28
- data/Rakefile +4 -5
- data/lib/generators/public_activity/migration/migration_generator.rb +1 -1
- data/lib/generators/public_activity/migration/templates/migration.rb +10 -10
- data/lib/generators/public_activity/migration_upgrade/migration_upgrade_generator.rb +2 -1
- data/lib/generators/public_activity/migration_upgrade/templates/upgrade.rb +2 -2
- data/lib/public_activity/orm/active_record/activity.rb +14 -12
- data/lib/public_activity/renderable.rb +3 -2
- data/lib/public_activity/version.rb +1 -1
- data/test/migrations/001_create_activities.rb +1 -1
- data/test/migrations/002_create_articles.rb +1 -4
- data/test/migrations/003_create_users.rb +1 -3
- data/test/migrations/004_add_nonstandard_to_activities.rb +1 -3
- data/test/test_activist.rb +37 -36
- data/test/test_activity.rb +37 -38
- data/test/test_common.rb +58 -49
- data/test/test_controller_integration.rb +17 -11
- data/test/test_generators.rb +5 -17
- data/test/test_helper.rb +18 -24
- data/test/test_testing.rb +11 -10
- data/test/test_tracking.rb +169 -147
- data/test/test_view_helpers.rb +5 -5
- metadata +49 -40
- data/lib/generators/public_activity/activity/activity_generator.rb +0 -19
- data/lib/generators/public_activity/activity/templates/activity.rb +0 -5
- data/test/migrations_base.rb +0 -7
data/test/test_tracking.rb
CHANGED
@@ -6,76 +6,75 @@ describe PublicActivity::Tracked do
|
|
6
6
|
describe 'defining instance options' do
|
7
7
|
subject { article.new }
|
8
8
|
let :options do
|
9
|
-
{
|
10
|
-
:
|
11
|
-
:
|
12
|
-
:
|
9
|
+
{
|
10
|
+
key: 'key',
|
11
|
+
params: { a: 1 },
|
12
|
+
owner: User.create,
|
13
|
+
recipient: User.create
|
14
|
+
}
|
13
15
|
end
|
14
16
|
before(:each) { subject.activity(options) }
|
15
|
-
let(:activity){ subject.save; subject.activities.last }
|
17
|
+
let(:activity) { subject.save; subject.activities.last }
|
16
18
|
|
17
|
-
specify { subject.activity_key
|
18
|
-
specify { activity.key
|
19
|
+
specify { assert_same subject.activity_key, options[:key] }
|
20
|
+
specify { assert_equal activity.key, options[:key] }
|
19
21
|
|
20
|
-
specify { subject.activity_owner
|
21
|
-
specify { activity.owner
|
22
|
+
specify { assert_same subject.activity_owner, options[:owner] }
|
23
|
+
specify { assert_equal activity.owner, options[:owner] }
|
22
24
|
|
23
|
-
specify { subject.activity_params
|
24
|
-
specify { activity.parameters
|
25
|
+
specify { assert_same subject.activity_params, options[:params] }
|
26
|
+
specify { assert_equal activity.parameters, options[:params] }
|
25
27
|
|
26
|
-
specify { subject.activity_recipient
|
27
|
-
specify { activity.recipient
|
28
|
+
specify { assert_same subject.activity_recipient, options[:recipient] }
|
29
|
+
specify { assert_equal activity.recipient, options[:recipient] }
|
28
30
|
end
|
29
31
|
|
30
32
|
it 'can be tracked and be an activist at the same time' do
|
31
33
|
case PublicActivity.config.orm
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
40
|
-
|
41
|
-
belongs_to :user
|
42
|
-
end
|
43
|
-
|
44
|
-
field :name, type: String
|
45
|
-
field :published, type: Boolean
|
46
|
-
tracked
|
47
|
-
activist
|
48
|
-
end
|
49
|
-
when :mongo_mapper
|
50
|
-
class ActivistAndTrackedArticle
|
51
|
-
include MongoMapper::Document
|
52
|
-
include PublicActivity::Model
|
53
|
-
|
54
|
-
belongs_to :user
|
55
|
-
|
56
|
-
key :name, String
|
57
|
-
key :published, Boolean
|
58
|
-
tracked
|
59
|
-
activist
|
60
|
-
timestamps!
|
61
|
-
end
|
62
|
-
when :active_record
|
63
|
-
class ActivistAndTrackedArticle < ActiveRecord::Base
|
64
|
-
self.table_name = 'articles'
|
65
|
-
include PublicActivity::Model
|
66
|
-
tracked
|
67
|
-
activist
|
68
|
-
|
69
|
-
if ::ActiveRecord::VERSION::MAJOR < 4
|
70
|
-
attr_accessible :name, :published, :user
|
71
|
-
end
|
34
|
+
when :mongoid
|
35
|
+
class ActivistAndTrackedArticle
|
36
|
+
include Mongoid::Document
|
37
|
+
include Mongoid::Timestamps
|
38
|
+
include PublicActivity::Model
|
39
|
+
|
40
|
+
if ::Mongoid::VERSION.split('.')[0].to_i >= 7
|
41
|
+
belongs_to :user, optional: true
|
42
|
+
else
|
72
43
|
belongs_to :user
|
73
44
|
end
|
45
|
+
|
46
|
+
field :name, type: String
|
47
|
+
field :published, type: Boolean
|
48
|
+
tracked
|
49
|
+
activist
|
50
|
+
end
|
51
|
+
when :mongo_mapper
|
52
|
+
class ActivistAndTrackedArticle
|
53
|
+
include MongoMapper::Document
|
54
|
+
include PublicActivity::Model
|
55
|
+
|
56
|
+
belongs_to :user
|
57
|
+
|
58
|
+
key :name, String
|
59
|
+
key :published, Boolean
|
60
|
+
tracked
|
61
|
+
activist
|
62
|
+
timestamps!
|
63
|
+
end
|
64
|
+
when :active_record
|
65
|
+
class ActivistAndTrackedArticle < ActiveRecord::Base
|
66
|
+
self.table_name = 'articles'
|
67
|
+
include PublicActivity::Model
|
68
|
+
tracked
|
69
|
+
activist
|
70
|
+
|
71
|
+
belongs_to :user
|
72
|
+
end
|
74
73
|
end
|
75
74
|
|
76
75
|
art = ActivistAndTrackedArticle.new
|
77
76
|
art.save
|
78
|
-
art.activities.last.trackable_id
|
77
|
+
assert_equal art.activities.last.trackable_id, art.id
|
79
78
|
assert_nil art.activities.last.owner_id
|
80
79
|
end
|
81
80
|
|
@@ -84,13 +83,13 @@ describe PublicActivity::Tracked do
|
|
84
83
|
it 'should resolve symbols' do
|
85
84
|
a = article(nonstandard: :name).new(name: 'Symbol resolved')
|
86
85
|
a.save
|
87
|
-
a.activities.last.nonstandard
|
86
|
+
assert_equal a.activities.last.nonstandard, 'Symbol resolved'
|
88
87
|
end
|
89
88
|
|
90
89
|
it 'should resolve procs' do
|
91
|
-
a = article(nonstandard: proc {|_, model| model.name}).new(name: 'Proc resolved')
|
90
|
+
a = article(nonstandard: proc { |_, model| model.name }).new(name: 'Proc resolved')
|
92
91
|
a.save
|
93
|
-
a.activities.last.nonstandard
|
92
|
+
assert_equal a.activities.last.nonstandard, 'Proc resolved'
|
94
93
|
end
|
95
94
|
end
|
96
95
|
|
@@ -99,43 +98,43 @@ describe PublicActivity::Tracked do
|
|
99
98
|
a = article.new(name: 'Symbol resolved')
|
100
99
|
a.activity nonstandard: :name
|
101
100
|
a.save
|
102
|
-
a.activities.last.nonstandard
|
101
|
+
assert_equal a.activities.last.nonstandard, 'Symbol resolved'
|
103
102
|
end
|
104
103
|
|
105
104
|
it 'should resolve procs' do
|
106
105
|
a = article.new(name: 'Proc resolved')
|
107
|
-
a.activity nonstandard: proc {|_, model| model.name}
|
106
|
+
a.activity nonstandard: proc { |_, model| model.name }
|
108
107
|
a.save
|
109
|
-
a.activities.last.nonstandard
|
108
|
+
assert_equal a.activities.last.nonstandard, 'Proc resolved'
|
110
109
|
end
|
111
110
|
end
|
112
111
|
end
|
113
112
|
|
114
113
|
it 'should reset instance options on successful create_activity' do
|
115
114
|
a = article.new
|
116
|
-
a.activity key: 'test', params: {test: 1}
|
115
|
+
a.activity key: 'test', params: { test: 1 }
|
117
116
|
a.save
|
118
|
-
a.activities.count
|
119
|
-
|
120
|
-
a.activity_params
|
117
|
+
assert_equal a.activities.count, 1
|
118
|
+
assert_raises(PublicActivity::NoKeyProvided) { a.create_activity }
|
119
|
+
assert_empty a.activity_params
|
121
120
|
a.activity key: 'asd'
|
122
121
|
a.create_activity
|
123
|
-
|
122
|
+
assert_raises(PublicActivity::NoKeyProvided) { a.create_activity }
|
124
123
|
end
|
125
124
|
|
126
125
|
it 'should not accept global key option' do
|
127
126
|
# this example tests the lack of presence of sth that should not be here
|
128
127
|
a = article(key: 'asd').new
|
129
128
|
a.save
|
130
|
-
|
131
|
-
a.activities.count
|
129
|
+
assert_raises(PublicActivity::NoKeyProvided) { a.create_activity }
|
130
|
+
assert_equal a.activities.count, 1
|
132
131
|
end
|
133
132
|
|
134
133
|
it 'should not change global custom fields' do
|
135
134
|
a = article(nonstandard: 'global').new
|
136
135
|
a.activity nonstandard: 'instance'
|
137
136
|
a.save
|
138
|
-
a.class.activity_custom_fields_global
|
137
|
+
assert_equal a.class.activity_custom_fields_global, nonstandard: 'global'
|
139
138
|
end
|
140
139
|
|
141
140
|
describe 'disabling functionality' do
|
@@ -143,9 +142,9 @@ describe PublicActivity::Tracked do
|
|
143
142
|
PublicActivity.enabled = false
|
144
143
|
activity_count_before = PublicActivity::Activity.count
|
145
144
|
|
146
|
-
@article = article
|
145
|
+
@article = article.new
|
147
146
|
@article.save
|
148
|
-
PublicActivity::Activity.count
|
147
|
+
assert_equal PublicActivity::Activity.count, activity_count_before
|
149
148
|
|
150
149
|
PublicActivity.enabled = true
|
151
150
|
end
|
@@ -157,12 +156,12 @@ describe PublicActivity::Tracked do
|
|
157
156
|
klass.public_activity_off
|
158
157
|
@article = klass.new
|
159
158
|
@article.save
|
160
|
-
PublicActivity::Activity.count
|
159
|
+
assert_equal PublicActivity::Activity.count, activity_count_before
|
161
160
|
|
162
161
|
klass.public_activity_on
|
163
162
|
@article.name = 'Changed Article'
|
164
163
|
@article.save
|
165
|
-
PublicActivity::Activity.count
|
164
|
+
assert(PublicActivity::Activity.count > activity_count_before)
|
166
165
|
end
|
167
166
|
end
|
168
167
|
|
@@ -171,9 +170,10 @@ describe PublicActivity::Tracked do
|
|
171
170
|
let(:options) { {} }
|
172
171
|
|
173
172
|
it 'allows skipping the tracking on CRUD actions' do
|
174
|
-
|
173
|
+
art =
|
174
|
+
case PublicActivity.config.orm
|
175
175
|
when :mongoid
|
176
|
-
|
176
|
+
Class.new do
|
177
177
|
include Mongoid::Document
|
178
178
|
include Mongoid::Timestamps
|
179
179
|
include PublicActivity::Model
|
@@ -182,10 +182,10 @@ describe PublicActivity::Tracked do
|
|
182
182
|
|
183
183
|
field :name, type: String
|
184
184
|
field :published, type: Boolean
|
185
|
-
tracked :
|
185
|
+
tracked skip_defaults: true
|
186
186
|
end
|
187
187
|
when :mongo_mapper
|
188
|
-
|
188
|
+
Class.new do
|
189
189
|
include MongoMapper::Document
|
190
190
|
include PublicActivity::Model
|
191
191
|
|
@@ -193,44 +193,57 @@ describe PublicActivity::Tracked do
|
|
193
193
|
|
194
194
|
key :name, String
|
195
195
|
key :published, Boolean
|
196
|
-
tracked :
|
196
|
+
tracked skip_defaults: true
|
197
197
|
|
198
198
|
timestamps!
|
199
199
|
end
|
200
200
|
when :active_record
|
201
|
-
|
202
|
-
|
201
|
+
article(skip_defaults: true)
|
202
|
+
end
|
203
203
|
|
204
|
-
art
|
205
|
-
art
|
206
|
-
art
|
207
|
-
art
|
204
|
+
assert_includes art, PublicActivity::Common
|
205
|
+
refute_includes art, PublicActivity::Creation
|
206
|
+
refute_includes art, PublicActivity::Update
|
207
|
+
refute_includes art, PublicActivity::Destruction
|
208
208
|
end
|
209
209
|
|
210
210
|
describe 'default options' do
|
211
211
|
subject { article }
|
212
212
|
|
213
|
-
specify { subject
|
214
|
-
specify { subject
|
215
|
-
specify { subject
|
213
|
+
specify { assert_includes subject, PublicActivity::Creation }
|
214
|
+
specify { assert_includes subject, PublicActivity::Destruction }
|
215
|
+
specify { assert_includes subject, PublicActivity::Update }
|
216
216
|
|
217
|
-
specify
|
218
|
-
|
219
|
-
|
217
|
+
specify do
|
218
|
+
callbacks = subject._create_callbacks.select do |c|
|
219
|
+
c.kind.eql?(:after) && c.filter == :activity_on_create
|
220
|
+
end
|
221
|
+
|
222
|
+
refute_empty callbacks
|
223
|
+
end
|
224
|
+
|
225
|
+
specify do
|
226
|
+
callbacks = subject._update_callbacks.select do |c|
|
227
|
+
c.kind.eql?(:after) && c.filter == :activity_on_update
|
228
|
+
end
|
229
|
+
|
230
|
+
refute_empty callbacks
|
231
|
+
end
|
220
232
|
|
221
|
-
specify
|
222
|
-
|
223
|
-
|
233
|
+
specify do
|
234
|
+
callbacks = subject._destroy_callbacks.select do |c|
|
235
|
+
c.kind.eql?(:before) && c.filter == :activity_on_destroy
|
236
|
+
end
|
224
237
|
|
225
|
-
|
226
|
-
|
227
|
-
end.wont_be_empty }
|
238
|
+
refute_empty callbacks
|
239
|
+
end
|
228
240
|
end
|
229
241
|
|
230
242
|
it 'accepts :except option' do
|
231
|
-
|
243
|
+
art =
|
244
|
+
case PublicActivity.config.orm
|
232
245
|
when :mongoid
|
233
|
-
|
246
|
+
Class.new do
|
234
247
|
include Mongoid::Document
|
235
248
|
include Mongoid::Timestamps
|
236
249
|
include PublicActivity::Model
|
@@ -239,10 +252,10 @@ describe PublicActivity::Tracked do
|
|
239
252
|
|
240
253
|
field :name, type: String
|
241
254
|
field :published, type: Boolean
|
242
|
-
tracked :
|
255
|
+
tracked except: [:create]
|
243
256
|
end
|
244
257
|
when :mongo_mapper
|
245
|
-
|
258
|
+
Class.new do
|
246
259
|
include MongoMapper::Document
|
247
260
|
include PublicActivity::Model
|
248
261
|
|
@@ -250,23 +263,24 @@ describe PublicActivity::Tracked do
|
|
250
263
|
|
251
264
|
key :name, String
|
252
265
|
key :published, Boolean
|
253
|
-
tracked :
|
266
|
+
tracked except: [:create]
|
254
267
|
|
255
268
|
timestamps!
|
256
269
|
end
|
257
270
|
when :active_record
|
258
|
-
|
259
|
-
|
271
|
+
article(except: [:create])
|
272
|
+
end
|
260
273
|
|
261
|
-
art
|
262
|
-
art
|
263
|
-
art
|
274
|
+
refute_includes art, PublicActivity::Creation
|
275
|
+
assert_includes art, PublicActivity::Update
|
276
|
+
assert_includes art, PublicActivity::Destruction
|
264
277
|
end
|
265
278
|
|
266
279
|
it 'accepts :only option' do
|
267
|
-
|
280
|
+
art =
|
281
|
+
case PublicActivity.config.orm
|
268
282
|
when :mongoid
|
269
|
-
|
283
|
+
Class.new do
|
270
284
|
include Mongoid::Document
|
271
285
|
include Mongoid::Timestamps
|
272
286
|
include PublicActivity::Model
|
@@ -276,10 +290,10 @@ describe PublicActivity::Tracked do
|
|
276
290
|
field :name, type: String
|
277
291
|
field :published, type: Boolean
|
278
292
|
|
279
|
-
tracked :
|
293
|
+
tracked only: %i[create update]
|
280
294
|
end
|
281
295
|
when :mongo_mapper
|
282
|
-
|
296
|
+
Class.new do
|
283
297
|
include MongoMapper::Document
|
284
298
|
include PublicActivity::Model
|
285
299
|
|
@@ -288,61 +302,65 @@ describe PublicActivity::Tracked do
|
|
288
302
|
key :name, String
|
289
303
|
key :published, Boolean
|
290
304
|
|
291
|
-
tracked :
|
305
|
+
tracked only: %i[create update]
|
292
306
|
end
|
293
307
|
when :active_record
|
294
|
-
|
295
|
-
|
308
|
+
article(only: %I[create update])
|
309
|
+
end
|
296
310
|
|
297
|
-
art
|
298
|
-
art
|
299
|
-
art
|
311
|
+
assert_includes art, PublicActivity::Creation
|
312
|
+
refute_includes art, PublicActivity::Destruction
|
313
|
+
assert_includes art, PublicActivity::Update
|
300
314
|
end
|
301
315
|
|
302
316
|
it 'accepts :owner option' do
|
303
317
|
owner = mock('owner')
|
304
|
-
subject.tracked(:
|
305
|
-
subject.activity_owner_global
|
318
|
+
subject.tracked(owner: owner)
|
319
|
+
assert_equal subject.activity_owner_global, owner
|
306
320
|
end
|
307
321
|
|
308
322
|
it 'accepts :params option' do
|
309
|
-
params = {:
|
310
|
-
subject.tracked(:
|
311
|
-
subject.activity_params_global
|
323
|
+
params = { a: 1 }
|
324
|
+
subject.tracked(params: params)
|
325
|
+
assert_equal subject.activity_params_global, params
|
312
326
|
end
|
313
327
|
|
314
328
|
it 'accepts :on option' do
|
315
|
-
on = {:
|
316
|
-
subject.tracked(:
|
317
|
-
subject.activity_hooks
|
329
|
+
on = { a: -> {}, b: proc {} }
|
330
|
+
subject.tracked(on: on)
|
331
|
+
assert_equal subject.activity_hooks, on
|
318
332
|
end
|
319
333
|
|
320
334
|
it 'accepts :on option with string keys' do
|
321
|
-
on = {'a' =>
|
322
|
-
subject.tracked(:
|
323
|
-
subject.activity_hooks
|
335
|
+
on = { 'a' => -> {} }
|
336
|
+
subject.tracked(on: on)
|
337
|
+
assert_equal subject.activity_hooks, on.symbolize_keys
|
324
338
|
end
|
325
339
|
|
326
340
|
it 'accepts :on values that are procs' do
|
327
|
-
on = {:
|
328
|
-
subject.tracked(:
|
329
|
-
subject.activity_hooks
|
330
|
-
subject.activity_hooks
|
331
|
-
subject.activity_hooks
|
341
|
+
on = { unpassable: 1, proper: -> {}, proper_proc: proc {} }
|
342
|
+
subject.tracked(on: on)
|
343
|
+
assert_includes subject.activity_hooks, :proper
|
344
|
+
assert_includes subject.activity_hooks, :proper_proc
|
345
|
+
refute_includes subject.activity_hooks, :unpassable
|
332
346
|
end
|
333
347
|
|
334
348
|
describe 'global options' do
|
335
|
-
subject { article(recipient: :test, owner: :test2, params: {a: 'b'}) }
|
349
|
+
subject { article(recipient: :test, owner: :test2, params: { a: 'b' }) }
|
336
350
|
|
337
|
-
specify { subject.activity_recipient_global
|
338
|
-
specify { subject.activity_owner_global
|
339
|
-
specify { subject.activity_params_global
|
351
|
+
specify { assert_equal subject.activity_recipient_global, :test }
|
352
|
+
specify { assert_equal subject.activity_owner_global, :test2 }
|
353
|
+
specify { assert_equal subject.activity_params_global, a: 'b' }
|
340
354
|
end
|
341
355
|
end
|
342
356
|
|
343
357
|
describe 'activity hooks' do
|
344
|
-
subject
|
345
|
-
|
358
|
+
subject do
|
359
|
+
s = article
|
360
|
+
s.activity_hooks = { test: hook }
|
361
|
+
s
|
362
|
+
end
|
363
|
+
let(:hook) { -> {} }
|
346
364
|
|
347
365
|
it 'retrieves hooks' do
|
348
366
|
assert_same hook, subject.get_hook(:test)
|
@@ -353,33 +371,37 @@ describe PublicActivity::Tracked do
|
|
353
371
|
end
|
354
372
|
|
355
373
|
it 'returns nil when no matching hook is present' do
|
356
|
-
nil
|
374
|
+
assert_same nil, subject.get_hook(:nonexistent)
|
357
375
|
end
|
358
376
|
|
359
377
|
it 'allows hooks to decide if activity should be created' do
|
360
378
|
subject.tracked
|
361
|
-
@article = subject.new(:
|
379
|
+
@article = subject.new(name: 'Some Name')
|
362
380
|
PublicActivity.set_controller(mock('controller'))
|
363
381
|
pf = proc { |model, controller|
|
364
|
-
controller
|
365
|
-
model.name
|
382
|
+
assert_same controller, PublicActivity.get_controller
|
383
|
+
assert_equal model.name, 'Some Name'
|
366
384
|
false
|
367
385
|
}
|
368
386
|
pt = proc { |model, controller|
|
369
|
-
controller
|
370
|
-
model.name
|
387
|
+
assert_same controller, PublicActivity.get_controller
|
388
|
+
assert_equal model.name, 'Other Name'
|
371
389
|
true # this will save the activity with *.update key
|
372
390
|
}
|
373
|
-
@article.class.activity_hooks = {:
|
391
|
+
@article.class.activity_hooks = { create: pf, update: pt, destroy: pt }
|
374
392
|
|
375
|
-
@article.activities.to_a
|
393
|
+
assert_empty @article.activities.to_a
|
376
394
|
@article.save # create
|
377
395
|
@article.name = 'Other Name'
|
378
396
|
@article.save # update
|
379
397
|
@article.destroy # destroy
|
380
398
|
|
381
|
-
@article.activities.count
|
382
|
-
@article.activities.first.key
|
399
|
+
assert_equal @article.activities.count, 2
|
400
|
+
assert_equal @article.activities.first.key, 'article.update'
|
383
401
|
end
|
384
402
|
end
|
403
|
+
|
404
|
+
def teardown
|
405
|
+
PublicActivity.set_controller(nil)
|
406
|
+
end
|
385
407
|
end
|
data/test/test_view_helpers.rb
CHANGED
@@ -24,14 +24,14 @@ describe 'ViewHelpers Rendering' do
|
|
24
24
|
@view_flow.expects(:set).twice.with('name', ActiveSupport::SafeBuffer.new)
|
25
25
|
|
26
26
|
single_content_for('name', 'content')
|
27
|
-
@name
|
28
|
-
@content
|
27
|
+
assert_equal @name, 'name'
|
28
|
+
assert_equal @content, 'content'
|
29
29
|
single_content_for('name', 'content2')
|
30
|
-
@name
|
31
|
-
@content
|
30
|
+
assert_equal @name, 'name'
|
31
|
+
assert_equal @content, 'content2'
|
32
32
|
end
|
33
33
|
|
34
|
-
def content_for(name, content
|
34
|
+
def content_for(name, content)
|
35
35
|
@name = name
|
36
36
|
@content = content
|
37
37
|
end
|