govuk_content_models 32.2.0 → 32.3.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +4 -0
- data/app/models/artefact.rb +23 -5
- data/app/models/artefact_action.rb +1 -0
- data/lib/govuk_content_models/version.rb +1 -1
- data/test/models/artefact_action_test.rb +52 -42
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 0ffd082b3277c164319de729908a7db69bf74b69
|
4
|
+
data.tar.gz: a9260435afd6fc9c3449213c771ac70d138d856d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: dc37f5693293e233a5e801a79c60e7e12b32fbcadaa177dd1b0a06e81df71129704687018c44966e895e3f1630f82fec7863a1176d4566fed8faf48691b8e27b
|
7
|
+
data.tar.gz: fd53211639556b8b28f7242ed6ce0f4073c21b607614854de91f24d8e6c468f782888b85cd78cb3eee7885aa0eafa127e86326b0924bee476b05928385efac97
|
data/CHANGELOG.md
CHANGED
data/app/models/artefact.rb
CHANGED
@@ -318,6 +318,17 @@ class Artefact
|
|
318
318
|
save(options)
|
319
319
|
end
|
320
320
|
|
321
|
+
# We should use this method when performing save actions from rake tasks,
|
322
|
+
# message queue consumer or any other performed tasks that have no user associated
|
323
|
+
# as we are still interested to know what triggered the action.
|
324
|
+
def save_as_task(task_name, options = {})
|
325
|
+
default_action = new_record? ? "create" : "update"
|
326
|
+
action_type = options.delete(:action_type) || default_action
|
327
|
+
|
328
|
+
record_action(action_type, task_name: task_name)
|
329
|
+
save(options)
|
330
|
+
end
|
331
|
+
|
321
332
|
def record_create_action
|
322
333
|
record_action "create"
|
323
334
|
end
|
@@ -328,14 +339,21 @@ class Artefact
|
|
328
339
|
|
329
340
|
def record_action(action_type, options={})
|
330
341
|
user = options[:user]
|
342
|
+
task_name = options[:task_name]
|
331
343
|
current_snapshot = snapshot
|
332
|
-
last_snapshot = actions.last
|
344
|
+
last_snapshot = actions.last.snapshot if actions.last
|
345
|
+
|
333
346
|
unless current_snapshot == last_snapshot
|
334
|
-
|
335
|
-
|
347
|
+
|
348
|
+
attributes = {
|
336
349
|
action_type: action_type,
|
337
|
-
snapshot: current_snapshot
|
338
|
-
|
350
|
+
snapshot: current_snapshot,
|
351
|
+
}
|
352
|
+
|
353
|
+
attributes.merge!(user: user) if user
|
354
|
+
attributes.merge!(task_performed_by: task_name) if task_name
|
355
|
+
|
356
|
+
new_action = actions.build(attributes)
|
339
357
|
# Mongoid will not fire creation callbacks on embedded documents, so we
|
340
358
|
# need to trigger this manually. There is a `cascade_callbacks` option on
|
341
359
|
# `embeds_many`, but it doesn't appear to trigger creation events on
|
@@ -39,86 +39,96 @@ class ArtefactActionTest < ActiveSupport::TestCase
|
|
39
39
|
}
|
40
40
|
end
|
41
41
|
|
42
|
+
def setup
|
43
|
+
@artefact = Artefact.create!(base_fields)
|
44
|
+
end
|
45
|
+
|
42
46
|
test "a new artefact should have a create action" do
|
43
|
-
|
44
|
-
a.reload
|
47
|
+
@artefact.reload
|
45
48
|
|
46
|
-
assert_equal 1,
|
47
|
-
action =
|
49
|
+
assert_equal 1, @artefact.actions.size
|
50
|
+
action = @artefact.actions.first
|
48
51
|
assert_equal "create", action[:action_type]
|
49
52
|
assert_equal merge_attributes(DEFAULTS, base_fields), action.snapshot
|
50
53
|
assert action.created_at, "Action has no creation timestamp"
|
51
54
|
end
|
52
55
|
|
53
56
|
test "an updated artefact should have two actions" do
|
54
|
-
|
55
|
-
|
56
|
-
|
57
|
-
a.reload
|
57
|
+
@artefact.description = "An artefact of shining wonderment."
|
58
|
+
@artefact.save!
|
59
|
+
@artefact.reload
|
58
60
|
|
59
|
-
assert_equal 2,
|
60
|
-
assert_equal ["create", "update"],
|
61
|
+
assert_equal 2, @artefact.actions.size
|
62
|
+
assert_equal ["create", "update"], @artefact.actions.map(&:action_type)
|
61
63
|
create_snapshot = merge_attributes(DEFAULTS, base_fields)
|
62
|
-
update_snapshot = create_snapshot.merge("description" =>
|
63
|
-
assert_equal create_snapshot,
|
64
|
-
assert_equal update_snapshot,
|
65
|
-
|
64
|
+
update_snapshot = create_snapshot.merge("description" => @artefact.description)
|
65
|
+
assert_equal create_snapshot, @artefact.actions[0].snapshot
|
66
|
+
assert_equal update_snapshot, @artefact.actions[1].snapshot
|
67
|
+
@artefact.actions.each do |action|
|
66
68
|
assert action.created_at, "Action has no creation timestamp"
|
67
69
|
end
|
68
70
|
end
|
69
71
|
|
70
72
|
test "saving with no tracked changes will not create a new snapshot" do
|
71
|
-
|
72
|
-
|
73
|
-
|
74
|
-
assert_equal 1, a.actions.size
|
73
|
+
@artefact.updated_at = Time.zone.now + 5.minutes
|
74
|
+
@artefact.save!
|
75
|
+
assert_equal 1, @artefact.actions.size
|
75
76
|
end
|
76
77
|
|
77
78
|
test "updating attributes as a user should record a user action" do
|
78
|
-
a = Artefact.create!(base_fields)
|
79
79
|
user = FactoryGirl.create :user
|
80
80
|
updates = {description: "Shiny shiny description"}
|
81
|
-
|
82
|
-
|
81
|
+
@artefact.update_attributes_as user, updates
|
82
|
+
@artefact.reload
|
83
83
|
|
84
|
-
assert_equal "Shiny shiny description",
|
85
|
-
assert_equal 2,
|
86
|
-
assert_equal ["create", "update"],
|
87
|
-
assert_equal user,
|
84
|
+
assert_equal "Shiny shiny description", @artefact.description
|
85
|
+
assert_equal 2, @artefact.actions.size
|
86
|
+
assert_equal ["create", "update"], @artefact.actions.map(&:action_type)
|
87
|
+
assert_equal user, @artefact.actions.last.user
|
88
88
|
assert_equal(
|
89
89
|
merge_attributes(DEFAULTS, base_fields, updates),
|
90
|
-
|
90
|
+
@artefact.actions.last.snapshot
|
91
91
|
)
|
92
92
|
end
|
93
93
|
|
94
|
+
test "saving a task should record the task action" do
|
95
|
+
@artefact.description = "Updated automatically"
|
96
|
+
@artefact.save_as_task('TaggingUpdater')
|
97
|
+
@artefact.reload
|
98
|
+
|
99
|
+
assert_equal 2, @artefact.actions.size
|
100
|
+
assert_equal ["create", "update"], @artefact.actions.map(&:action_type)
|
101
|
+
|
102
|
+
assert_equal 'TaggingUpdater', @artefact.actions.last.task_performed_by
|
103
|
+
assert_equal nil, @artefact.actions.last.user
|
104
|
+
end
|
105
|
+
|
94
106
|
test "saving as a user should record a user action" do
|
95
|
-
a = Artefact.create!(base_fields)
|
96
107
|
user = FactoryGirl.create :user
|
97
108
|
updates = {description: "Shiny shiny description"}
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
assert_equal "Shiny shiny description",
|
103
|
-
assert_equal 2,
|
104
|
-
assert_equal ["create", "update"],
|
105
|
-
assert_equal user,
|
109
|
+
@artefact.description = updates[:description]
|
110
|
+
@artefact.save_as user
|
111
|
+
@artefact.reload
|
112
|
+
|
113
|
+
assert_equal "Shiny shiny description", @artefact.description
|
114
|
+
assert_equal 2, @artefact.actions.size
|
115
|
+
assert_equal ["create", "update"], @artefact.actions.map(&:action_type)
|
116
|
+
assert_equal user, @artefact.actions.last.user
|
106
117
|
assert_equal(
|
107
118
|
merge_attributes(DEFAULTS, base_fields, updates),
|
108
|
-
|
119
|
+
@artefact.actions.last.snapshot
|
109
120
|
)
|
110
121
|
end
|
111
122
|
|
112
123
|
test "saving as a user with an action type" do
|
113
|
-
a = Artefact.create!(base_fields)
|
114
124
|
user = FactoryGirl.create :user
|
115
125
|
updates = {description: "Shiny shiny description"}
|
116
|
-
|
117
|
-
|
118
|
-
|
126
|
+
@artefact.description = updates[:description]
|
127
|
+
@artefact.save_as user, action_type: "awesome"
|
128
|
+
@artefact.reload
|
119
129
|
|
120
|
-
assert_equal user,
|
121
|
-
assert_equal "awesome",
|
130
|
+
assert_equal user, @artefact.actions.last.user
|
131
|
+
assert_equal "awesome", @artefact.actions.last.action_type
|
122
132
|
end
|
123
133
|
|
124
134
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: govuk_content_models
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 32.
|
4
|
+
version: 32.3.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Paul Battley
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-03-03 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bson_ext
|