govuk_content_models 32.2.0 → 32.3.0

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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: aa9614ff18d6424badcb16a5a972bf66e13e121b
4
- data.tar.gz: 106c23fef7501232f6193eaf7c8d34c03b04e11b
3
+ metadata.gz: 0ffd082b3277c164319de729908a7db69bf74b69
4
+ data.tar.gz: a9260435afd6fc9c3449213c771ac70d138d856d
5
5
  SHA512:
6
- metadata.gz: f1137d9954ddf99cd1bb814638f23b77921520c06ed205dd83d0e2c7e666391bf0938433ea9f7c6a14e7c74dc4930656237782d690aab8097933c6d8323f6e8c
7
- data.tar.gz: 3c161e491c8b70c2e3f6e28cbdf41cf1809dd126ef246fcc246139904d985c6c495d0e0f98f51df7140605b78d8ec5e0abaa0527262ba7971ded35ea04107373
6
+ metadata.gz: dc37f5693293e233a5e801a79c60e7e12b32fbcadaa177dd1b0a06e81df71129704687018c44966e895e3f1630f82fec7863a1176d4566fed8faf48691b8e27b
7
+ data.tar.gz: fd53211639556b8b28f7242ed6ce0f4073c21b607614854de91f24d8e6c468f782888b85cd78cb3eee7885aa0eafa127e86326b0924bee476b05928385efac97
data/CHANGELOG.md CHANGED
@@ -1,5 +1,9 @@
1
1
  # CHANGELOG
2
2
 
3
+ ## 32.3.0
4
+
5
+ - Add `save_as_task` method to Artefact model [#360](https://github.com/alphagov/govuk_content_models/pull/360)
6
+
3
7
  ## 32.2.0
4
8
 
5
9
  - Add `homepage_url` attribute to `LocalAuthority` model
@@ -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 ? actions.last.snapshot : nil
344
+ last_snapshot = actions.last.snapshot if actions.last
345
+
333
346
  unless current_snapshot == last_snapshot
334
- new_action = actions.build(
335
- user: user,
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
@@ -6,6 +6,7 @@ class ArtefactAction
6
6
 
7
7
  field "action_type", type: String
8
8
  field "snapshot", type: Hash
9
+ field "task_performed_by", type: String
9
10
 
10
11
  embedded_in :artefact
11
12
 
@@ -1,4 +1,4 @@
1
1
  module GovukContentModels
2
2
  # Changing this causes Jenkins to tag and release the gem into the wild
3
- VERSION = "32.2.0"
3
+ VERSION = "32.3.0"
4
4
  end
@@ -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
- a = Artefact.create!(base_fields)
44
- a.reload
47
+ @artefact.reload
45
48
 
46
- assert_equal 1, a.actions.size
47
- action = a.actions.first
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
- a = Artefact.create!(base_fields)
55
- a.description = "An artefact of shining wonderment."
56
- a.save!
57
- a.reload
57
+ @artefact.description = "An artefact of shining wonderment."
58
+ @artefact.save!
59
+ @artefact.reload
58
60
 
59
- assert_equal 2, a.actions.size
60
- assert_equal ["create", "update"], a.actions.map(&:action_type)
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" => a.description)
63
- assert_equal create_snapshot, a.actions[0].snapshot
64
- assert_equal update_snapshot, a.actions[1].snapshot
65
- a.actions.each do |action|
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
- a = Artefact.create!(base_fields)
72
- a.updated_at = Time.zone.now + 5.minutes
73
- a.save!
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
- a.update_attributes_as user, updates
82
- a.reload
81
+ @artefact.update_attributes_as user, updates
82
+ @artefact.reload
83
83
 
84
- assert_equal "Shiny shiny description", a.description
85
- assert_equal 2, a.actions.size
86
- assert_equal ["create", "update"], a.actions.map(&:action_type)
87
- assert_equal user, a.actions.last.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
- a.actions.last.snapshot
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
- a.description = updates[:description]
99
- a.save_as user
100
- a.reload
101
-
102
- assert_equal "Shiny shiny description", a.description
103
- assert_equal 2, a.actions.size
104
- assert_equal ["create", "update"], a.actions.map(&:action_type)
105
- assert_equal user, a.actions.last.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
- a.actions.last.snapshot
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
- a.description = updates[:description]
117
- a.save_as user, action_type: "awesome"
118
- a.reload
126
+ @artefact.description = updates[:description]
127
+ @artefact.save_as user, action_type: "awesome"
128
+ @artefact.reload
119
129
 
120
- assert_equal user, a.actions.last.user
121
- assert_equal "awesome", a.actions.last.action_type
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.2.0
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-02-02 00:00:00.000000000 Z
11
+ date: 2016-03-03 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: bson_ext