govuk_content_models 8.3.1 → 8.4.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.
- data/CHANGELOG.md +4 -0
- data/app/models/attachment.rb +22 -0
- data/app/models/specialist_document_edition.rb +9 -0
- data/app/validators/safe_html.rb +10 -1
- data/lib/govuk_content_models/version.rb +1 -1
- data/test/models/attachment_test.rb +43 -0
- data/test/models/specialist_document_edition_test.rb +63 -17
- data/test/validators/safe_html_validator_test.rb +10 -8
- metadata +6 -3
data/CHANGELOG.md
CHANGED
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
require 'attachable'
|
|
2
|
+
|
|
3
|
+
class Attachment
|
|
4
|
+
include Mongoid::Document
|
|
5
|
+
include Mongoid::Timestamps
|
|
6
|
+
include Attachable
|
|
7
|
+
|
|
8
|
+
field :title
|
|
9
|
+
field :filename
|
|
10
|
+
attaches :file
|
|
11
|
+
|
|
12
|
+
validates_with SafeHtml
|
|
13
|
+
|
|
14
|
+
def url
|
|
15
|
+
file.file_url
|
|
16
|
+
end
|
|
17
|
+
|
|
18
|
+
# TODO: Move this to a domain object in specialist publisher
|
|
19
|
+
def snippet
|
|
20
|
+
"[InlineAttachment:#{filename}]"
|
|
21
|
+
end
|
|
22
|
+
end
|
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
require "fact_check_address"
|
|
2
|
+
require "state_machine"
|
|
2
3
|
|
|
3
4
|
class SpecialistDocumentEdition
|
|
4
5
|
include Mongoid::Document
|
|
@@ -22,6 +23,8 @@ class SpecialistDocumentEdition
|
|
|
22
23
|
|
|
23
24
|
field :state, type: String
|
|
24
25
|
|
|
26
|
+
embeds_many :attachments, cascade_callbacks: true
|
|
27
|
+
|
|
25
28
|
state_machine initial: :draft do
|
|
26
29
|
event :publish do
|
|
27
30
|
transition draft: :published
|
|
@@ -75,4 +78,10 @@ class SpecialistDocumentEdition
|
|
|
75
78
|
def latest_edition?
|
|
76
79
|
subsequent_siblings.empty?
|
|
77
80
|
end
|
|
81
|
+
|
|
82
|
+
def build_attachment(attributes)
|
|
83
|
+
attachments.build(attributes.merge(
|
|
84
|
+
filename: attributes.fetch(:file).original_filename
|
|
85
|
+
))
|
|
86
|
+
end
|
|
78
87
|
end
|
data/app/validators/safe_html.rb
CHANGED
|
@@ -18,7 +18,7 @@ class SafeHtml < ActiveModel::Validator
|
|
|
18
18
|
end
|
|
19
19
|
|
|
20
20
|
def check_string(record, field_name, string)
|
|
21
|
-
if record.
|
|
21
|
+
if govspeak_fields(record).include?(field_name)
|
|
22
22
|
unless Govspeak::Document.new(string).valid?
|
|
23
23
|
error = "cannot include invalid Govspeak or JavaScript"
|
|
24
24
|
record.errors.add(field_name, error)
|
|
@@ -30,4 +30,13 @@ class SafeHtml < ActiveModel::Validator
|
|
|
30
30
|
end
|
|
31
31
|
end
|
|
32
32
|
end
|
|
33
|
+
|
|
34
|
+
private
|
|
35
|
+
def govspeak_fields(record)
|
|
36
|
+
if record.class.const_defined?(:GOVSPEAK_FIELDS)
|
|
37
|
+
record.class.const_get(:GOVSPEAK_FIELDS)
|
|
38
|
+
else
|
|
39
|
+
[]
|
|
40
|
+
end
|
|
41
|
+
end
|
|
33
42
|
end
|
|
@@ -0,0 +1,43 @@
|
|
|
1
|
+
require "test_helper"
|
|
2
|
+
require "ostruct"
|
|
3
|
+
require 'gds_api/test_helpers/asset_manager'
|
|
4
|
+
|
|
5
|
+
class AttachmentTest < ActiveSupport::TestCase
|
|
6
|
+
include GdsApi::TestHelpers::AssetManager
|
|
7
|
+
|
|
8
|
+
setup do
|
|
9
|
+
@original_asset_api_client = Attachable.asset_api_client
|
|
10
|
+
Attachable.asset_api_client = stub("asset_api_client")
|
|
11
|
+
end
|
|
12
|
+
|
|
13
|
+
teardown do
|
|
14
|
+
Attachable.asset_api_client = @original_asset_api_client
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
should "generate a snippet" do
|
|
18
|
+
attachment = Attachment.new(
|
|
19
|
+
title: "Supporting attachment",
|
|
20
|
+
filename: "document.pdf"
|
|
21
|
+
)
|
|
22
|
+
expected_snippet = "[InlineAttachment:document.pdf]"
|
|
23
|
+
|
|
24
|
+
assert_equal expected_snippet, attachment.snippet
|
|
25
|
+
end
|
|
26
|
+
|
|
27
|
+
should "return the url via #url" do
|
|
28
|
+
attachment = Attachment.new(
|
|
29
|
+
title: "Photo of me",
|
|
30
|
+
filename: "photo.jpg",
|
|
31
|
+
file_id: "test-id"
|
|
32
|
+
)
|
|
33
|
+
|
|
34
|
+
asset_url = stub("asset url")
|
|
35
|
+
asset_response = stub("asset response", file_url: asset_url)
|
|
36
|
+
Attachable.asset_api_client
|
|
37
|
+
.stubs(:asset)
|
|
38
|
+
.with(attachment.file_id)
|
|
39
|
+
.returns(asset_response)
|
|
40
|
+
|
|
41
|
+
assert_equal asset_url, attachment.url
|
|
42
|
+
end
|
|
43
|
+
end
|
|
@@ -3,35 +3,81 @@
|
|
|
3
3
|
require "test_helper"
|
|
4
4
|
|
|
5
5
|
class SpecialistDocumentEditionTest < ActiveSupport::TestCase
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
def basic_edition_fields
|
|
7
|
+
{
|
|
8
8
|
slug: 'cma-cases/merger-investigation-2014',
|
|
9
9
|
title: "Merger Investigation 2014",
|
|
10
10
|
summary: "This is the summary of stuff going on in the Merger Investigation 2014",
|
|
11
|
-
state: "published"
|
|
11
|
+
state: "published",
|
|
12
|
+
body: "A body",
|
|
13
|
+
opened_date: '2012-04-21',
|
|
14
|
+
document_id: 'a-document-id',
|
|
15
|
+
market_sector: 'oil-and-gas',
|
|
16
|
+
case_type: 'some-case-type',
|
|
17
|
+
case_state: 'open'
|
|
12
18
|
}
|
|
19
|
+
end
|
|
20
|
+
|
|
21
|
+
setup do
|
|
22
|
+
@original_asset_api_client = Attachable.asset_api_client
|
|
23
|
+
Attachable.asset_api_client = stub("asset_api_client")
|
|
24
|
+
end
|
|
25
|
+
|
|
26
|
+
teardown do
|
|
27
|
+
Attachable.asset_api_client = @original_asset_api_client
|
|
28
|
+
end
|
|
13
29
|
|
|
14
|
-
|
|
30
|
+
should "have correct fields" do
|
|
31
|
+
edition = SpecialistDocumentEdition.new(basic_edition_fields)
|
|
15
32
|
|
|
16
|
-
assert_equal
|
|
33
|
+
assert_equal basic_edition_fields[:title], edition.title
|
|
17
34
|
end
|
|
18
35
|
|
|
19
36
|
should "be persistable" do
|
|
20
|
-
edition = SpecialistDocumentEdition.create!(
|
|
21
|
-
slug: 'cma-cases/merger-investigation-2014',
|
|
22
|
-
title: "Merger Investigation 2014",
|
|
23
|
-
summary: "This is the summary of stuff going on in the Merger Investigation 2014",
|
|
24
|
-
body: "A body",
|
|
25
|
-
opened_date: '2012-04-21',
|
|
26
|
-
market_sector: 'oil-and-gas',
|
|
27
|
-
case_type: 'some-case-type',
|
|
28
|
-
case_state: 'open',
|
|
29
|
-
state: "published",
|
|
30
|
-
document_id: 'a-document-id'
|
|
31
|
-
)
|
|
37
|
+
edition = SpecialistDocumentEdition.create!(basic_edition_fields)
|
|
32
38
|
|
|
33
39
|
found = SpecialistDocumentEdition.where(slug: edition.slug).first
|
|
34
40
|
assert_equal found.attributes, edition.attributes
|
|
35
41
|
end
|
|
42
|
+
|
|
43
|
+
context "building attachments" do
|
|
44
|
+
should "build an attachment" do
|
|
45
|
+
edition = SpecialistDocumentEdition.new
|
|
46
|
+
file = OpenStruct.new(original_filename: "document.pdf")
|
|
47
|
+
|
|
48
|
+
edition.build_attachment(title: "baz", file: file)
|
|
49
|
+
|
|
50
|
+
attachment = edition.attachments.first
|
|
51
|
+
assert_equal "baz", attachment.title
|
|
52
|
+
assert_equal "document.pdf", attachment.filename
|
|
53
|
+
assert_equal file, attachment.instance_variable_get(:@file_file)
|
|
54
|
+
end
|
|
55
|
+
|
|
56
|
+
should "persist attachment record when document saved" do
|
|
57
|
+
Attachable.asset_api_client.stubs(:create_asset)
|
|
58
|
+
|
|
59
|
+
edition = SpecialistDocumentEdition.new(basic_edition_fields)
|
|
60
|
+
file = OpenStruct.new(original_filename: "document.pdf")
|
|
61
|
+
|
|
62
|
+
edition.build_attachment(title: "baz", file: file)
|
|
63
|
+
edition.save!
|
|
64
|
+
|
|
65
|
+
found = SpecialistDocumentEdition.where(slug: edition.slug).first
|
|
66
|
+
|
|
67
|
+
assert_equal 1, found.attachments.count
|
|
68
|
+
assert_equal "baz", found.attachments.first.title
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
should "transmit attached file to asset manager when document saved" do
|
|
72
|
+
edition = SpecialistDocumentEdition.new(basic_edition_fields)
|
|
73
|
+
file = OpenStruct.new(original_filename: "document.pdf")
|
|
74
|
+
|
|
75
|
+
success_response = stub("asset manager response", id: "/test-id")
|
|
76
|
+
Attachable.asset_api_client.expects(:create_asset).with(file: file).returns(success_response)
|
|
77
|
+
|
|
78
|
+
edition.build_attachment(title: "baz", file: file)
|
|
79
|
+
edition.save!
|
|
80
|
+
end
|
|
81
|
+
end
|
|
36
82
|
end
|
|
37
83
|
|
|
@@ -73,14 +73,16 @@ class SafeHtmlTest < ActiveSupport::TestCase
|
|
|
73
73
|
should "all models should use this validator" do
|
|
74
74
|
models_dir = File.expand_path("../../app/models/*", File.dirname(__FILE__))
|
|
75
75
|
|
|
76
|
-
classes = Dir[models_dir]
|
|
77
|
-
|
|
78
|
-
|
|
79
|
-
|
|
80
|
-
|
|
81
|
-
|
|
82
|
-
|
|
83
|
-
|
|
76
|
+
classes = Dir[models_dir]
|
|
77
|
+
.map { |file|
|
|
78
|
+
File.basename(file, ".rb").camelize.constantize
|
|
79
|
+
}
|
|
80
|
+
.select { |klass|
|
|
81
|
+
klass.included_modules.include?(Mongoid::Document)
|
|
82
|
+
}
|
|
83
|
+
.each { |klass|
|
|
84
|
+
assert_includes klass.validators.map(&:class), SafeHtml, "#{klass} must be validated with SafeHtml"
|
|
85
|
+
}
|
|
84
86
|
end
|
|
85
87
|
end
|
|
86
88
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: govuk_content_models
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 8.
|
|
4
|
+
version: 8.4.0
|
|
5
5
|
prerelease:
|
|
6
6
|
platform: ruby
|
|
7
7
|
authors:
|
|
@@ -348,6 +348,7 @@ files:
|
|
|
348
348
|
- app/models/artefact.rb
|
|
349
349
|
- app/models/artefact_action.rb
|
|
350
350
|
- app/models/artefact_external_link.rb
|
|
351
|
+
- app/models/attachment.rb
|
|
351
352
|
- app/models/business_support/business_size.rb
|
|
352
353
|
- app/models/business_support/business_type.rb
|
|
353
354
|
- app/models/business_support/location.rb
|
|
@@ -407,6 +408,7 @@ files:
|
|
|
407
408
|
- test/models/artefact_external_link_test.rb
|
|
408
409
|
- test/models/artefact_tag_test.rb
|
|
409
410
|
- test/models/artefact_test.rb
|
|
411
|
+
- test/models/attachment_test.rb
|
|
410
412
|
- test/models/business_support/business_size_test.rb
|
|
411
413
|
- test/models/business_support/business_type_test.rb
|
|
412
414
|
- test/models/business_support/location_test.rb
|
|
@@ -458,7 +460,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
|
458
460
|
version: '0'
|
|
459
461
|
segments:
|
|
460
462
|
- 0
|
|
461
|
-
hash:
|
|
463
|
+
hash: 4061637163716751307
|
|
462
464
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
|
463
465
|
none: false
|
|
464
466
|
requirements:
|
|
@@ -467,7 +469,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
467
469
|
version: '0'
|
|
468
470
|
segments:
|
|
469
471
|
- 0
|
|
470
|
-
hash:
|
|
472
|
+
hash: 4061637163716751307
|
|
471
473
|
requirements: []
|
|
472
474
|
rubyforge_project:
|
|
473
475
|
rubygems_version: 1.8.23
|
|
@@ -481,6 +483,7 @@ test_files:
|
|
|
481
483
|
- test/models/artefact_external_link_test.rb
|
|
482
484
|
- test/models/artefact_tag_test.rb
|
|
483
485
|
- test/models/artefact_test.rb
|
|
486
|
+
- test/models/attachment_test.rb
|
|
484
487
|
- test/models/business_support/business_size_test.rb
|
|
485
488
|
- test/models/business_support/business_type_test.rb
|
|
486
489
|
- test/models/business_support/location_test.rb
|