govuk_content_models 8.6.0 → 8.7.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.
@@ -9,6 +9,8 @@ class Attachment
9
9
  field :filename
10
10
  attaches :file
11
11
 
12
+ embedded_in :specialist_document_edition
13
+
12
14
  validates_with SafeHtml
13
15
 
14
16
  def url
@@ -5,7 +5,6 @@ class SlugValidator < ActiveModel::EachValidator
5
5
  DonePageValidator,
6
6
  ForeignTravelAdvicePageValidator,
7
7
  HelpPageValidator,
8
- DetailedGuidePageValidator,
9
8
  GovernmentPageValidator,
10
9
  SpecialistDocumentPageValidator,
11
10
  DefaultValidator
@@ -39,7 +38,10 @@ protected
39
38
  end
40
39
 
41
40
  def valid_slug?(url_part)
42
- ActiveSupport::Inflector.parameterize(url_part.to_s) == url_part.to_s
41
+ # Regex taken from ActiveSupport::Inflector.parameterize
42
+ # We don't want to use this method because it also does a number of cosmetic tidy-ups
43
+ # which lead to false-positives (eg merging consecutive '-'s)
44
+ ! url_part.to_s.match(/[^a-z0-9\-_]/)
43
45
  end
44
46
  end
45
47
 
@@ -74,45 +76,21 @@ protected
74
76
  end
75
77
  end
76
78
 
77
- class WhitehallFormatValidator < InstanceValidator
79
+ class GovernmentPageValidator < InstanceValidator
78
80
  def url_parts
79
- normalize_last_part_for_friendly_id(super)
81
+ # Some inside govt slugs have a . in them (eg news articles with no english translation)
82
+ super.map {|part| part.gsub(/\./, '') }
80
83
  end
81
84
 
82
- def validate!
83
- unless url_parts.all? { |url_part| valid_slug?(url_part) }
84
- record.errors[attribute] << "must be usable in a URL"
85
- end
86
- end
87
-
88
- protected
89
-
90
- def normalize_last_part_for_friendly_id(url_parts)
91
- url_parts[0...-1] + url_parts[-1..-1].map do |url_part|
92
- normalize_for_friendly_id(url_part)
93
- end
94
- end
95
-
96
- def normalize_for_friendly_id(url_part)
97
- url_part.sub('--', '-')
98
- end
99
-
100
- end
101
-
102
- class DetailedGuidePageValidator < WhitehallFormatValidator
103
- def applicable?
104
- of_kind?('detailed_guide')
105
- end
106
- end
107
-
108
- class GovernmentPageValidator < WhitehallFormatValidator
109
85
  def applicable?
110
86
  record.respond_to?(:kind) && prefixed_whitehall_format_names.include?(record.kind)
111
87
  end
112
88
 
113
89
  def validate!
114
90
  record.errors[attribute] << "Inside Government slugs must have a government/ prefix" unless starts_with?('government/')
115
- super
91
+ unless url_parts.all? { |url_part| valid_slug?(url_part) }
92
+ record.errors[attribute] << "must be usable in a URL"
93
+ end
116
94
  end
117
95
 
118
96
  protected
@@ -121,7 +99,7 @@ protected
121
99
  end
122
100
  end
123
101
 
124
- class SpecialistDocumentPageValidator < WhitehallFormatValidator
102
+ class SpecialistDocumentPageValidator < InstanceValidator
125
103
  def applicable?
126
104
  of_kind?('specialist-document')
127
105
  end
@@ -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 = "8.6.0"
3
+ VERSION = "8.7.0"
4
4
  end
@@ -8,7 +8,9 @@ class SpecialistDocumentEditionTest < ActiveSupport::TestCase
8
8
 
9
9
  setup do
10
10
  @original_asset_api_client = Attachable.asset_api_client
11
+ @success_response = stub("asset manager response", id: "/test-id")
11
12
  Attachable.asset_api_client = stub("asset_api_client")
13
+ Attachable.asset_api_client.stubs(:create_asset).returns(@success_response)
12
14
  end
13
15
 
14
16
  teardown do
@@ -60,12 +62,24 @@ class SpecialistDocumentEditionTest < ActiveSupport::TestCase
60
62
  edition = SpecialistDocumentEdition.new(basic_specialist_document_fields)
61
63
  file = OpenStruct.new(original_filename: "document.pdf")
62
64
 
63
- success_response = stub("asset manager response", id: "/test-id")
64
- Attachable.asset_api_client.expects(:create_asset).with(file: file).returns(success_response)
65
+ Attachable.asset_api_client.expects(:create_asset).with(file: file).returns(@success_response)
65
66
 
66
67
  edition.build_attachment(title: "baz", file: file)
67
68
  edition.save!
68
69
  end
70
+
71
+ should "be able to remove an attachment from the list of attachments" do
72
+ attributes = basic_specialist_document_fields.merge(
73
+ attachments: [Attachment.new(title: "example", filename: "example.pdf")]
74
+ )
75
+ edition = SpecialistDocumentEdition.create!(attributes)
76
+ assert_equal 1, edition.attachments.count
77
+
78
+ edition.attachments = []
79
+ edition.save!
80
+
81
+ edition.reload
82
+ assert_equal [], edition.attachments
83
+ end
69
84
  end
70
85
  end
71
-
@@ -28,6 +28,25 @@ class SlugTest < ActiveSupport::TestCase
28
28
  should "allow a normal slug" do
29
29
  assert document_with_slug("normal-slug").valid?
30
30
  end
31
+
32
+ should "allow consecutive dashes in a slug" do
33
+ # Gems like friendly_id use -- to de-dup slug collisions
34
+ assert document_with_slug("normal-slug--1").valid?
35
+ end
36
+
37
+ should "allow a done page slug" do
38
+ assert document_with_slug("done/normal-slug").valid?
39
+ end
40
+ end
41
+
42
+ context "Foreign travel advice pages" do
43
+ should "allow a travel-advice page to start with 'foreign-travel-advice/'" do
44
+ assert document_with_slug("foreign-travel-advice/aruba", kind: "travel-advice").valid?
45
+ end
46
+
47
+ should "not allow other types to start with 'foreign-travel-advice/'" do
48
+ refute document_with_slug("foreign-travel-advice/aruba", kind: "answer").valid?
49
+ end
31
50
  end
32
51
 
33
52
  context "Help pages" do
@@ -35,6 +54,10 @@ class SlugTest < ActiveSupport::TestCase
35
54
  refute document_with_slug("test", kind: "help_page").valid?
36
55
  assert document_with_slug("help/test", kind: "help_page").valid?
37
56
  end
57
+
58
+ should "not allow non-help pages to start with help/" do
59
+ refute document_with_slug("help/test", kind: "answer").valid?
60
+ end
38
61
  end
39
62
 
40
63
  context "Inside government slugs" do
@@ -43,8 +66,13 @@ class SlugTest < ActiveSupport::TestCase
43
66
  assert document_with_slug("government/test", kind: "policy").valid?
44
67
  end
45
68
 
46
- should "allow friendly_id suffixes to pass" do
47
- assert document_with_slug("government/policy/test--3", kind: "policy").valid?
69
+ should "allow abritrarily deep slugs" do
70
+ assert document_with_slug("government/test/foo", kind: "policy").valid?
71
+ assert document_with_slug("government/test/foo/bar", kind: "policy").valid?
72
+ end
73
+
74
+ should "allow . in slugs" do
75
+ assert document_with_slug("government/world-location-news/221033.pt", kind: "news_story").valid?
48
76
  end
49
77
  end
50
78
 
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.6.0
4
+ version: 8.7.0
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -9,7 +9,7 @@ authors:
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2014-03-13 00:00:00.000000000 Z
12
+ date: 2014-03-18 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bson_ext
@@ -463,7 +463,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
463
463
  version: '0'
464
464
  segments:
465
465
  - 0
466
- hash: 4252404017043972541
466
+ hash: 515922098811798624
467
467
  required_rubygems_version: !ruby/object:Gem::Requirement
468
468
  none: false
469
469
  requirements:
@@ -472,7 +472,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
472
472
  version: '0'
473
473
  segments:
474
474
  - 0
475
- hash: 4252404017043972541
475
+ hash: 515922098811798624
476
476
  requirements: []
477
477
  rubyforge_project:
478
478
  rubygems_version: 1.8.23