govuk_content_models 13.0.0 → 13.1.0

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,8 @@
1
+ ## 13.1.0
2
+
3
+ * Perform link validation everytime an edition is saved,
4
+ irrespective of which fields changed.
5
+
1
6
  ## 13.0.0
2
7
 
3
8
  * Bundle metadata fields for `RenderedSpecialistDocument` into a single
@@ -1,10 +1,11 @@
1
1
  class LinkValidator < ActiveModel::Validator
2
2
  def validate(record)
3
- record.changes.each do |field_name, (_, new_value)|
4
- if govspeak_fields(record).include?(field_name.to_sym)
5
- messages = errors(new_value)
6
- record.errors[field_name] << messages if messages
7
- end
3
+ govspeak_field_names(record).each do |govspeak_field_name|
4
+ govspeak_field_value = record.read_attribute(govspeak_field_name)
5
+ next if govspeak_field_value.blank?
6
+
7
+ messages = errors(govspeak_field_value)
8
+ record.errors[govspeak_field_name] << messages if messages
8
9
  end
9
10
  end
10
11
 
@@ -38,7 +39,7 @@ class LinkValidator < ActiveModel::Validator
38
39
 
39
40
  protected
40
41
 
41
- def govspeak_fields(record)
42
+ def govspeak_field_names(record)
42
43
  if record.class.const_defined?(:GOVSPEAK_FIELDS)
43
44
  record.class.const_get(:GOVSPEAK_FIELDS)
44
45
  else
@@ -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 = "13.0.0"
3
+ VERSION = "13.1.0"
4
4
  end
@@ -5,12 +5,22 @@ class LinkValidatorTest < ActiveSupport::TestCase
5
5
  include Mongoid::Document
6
6
 
7
7
  field "body", type: String
8
+ field "assignee", type: String
8
9
  GOVSPEAK_FIELDS = [:body]
9
10
 
10
11
  validates_with LinkValidator
11
12
  end
12
13
 
13
14
  context "links" do
15
+ should "not be verified for blank govspeak fields" do
16
+ doc = Dummy.new(body: nil)
17
+
18
+ assert_nothing_raised do
19
+ doc.valid?
20
+ end
21
+ assert_empty doc.errors
22
+ end
23
+
14
24
  should "start with http[s]://, mailto: or /" do
15
25
  doc = Dummy.new(body: "abc [external](external.com)")
16
26
  assert doc.invalid?
@@ -22,25 +32,40 @@ class LinkValidatorTest < ActiveSupport::TestCase
22
32
  doc = Dummy.new(body: "abc [internal](/internal)")
23
33
  assert doc.valid?
24
34
  end
35
+
25
36
  should "not contain hover text" do
26
37
  doc = Dummy.new(body: 'abc [foobar](http://foobar.com "hover")')
27
38
  assert doc.invalid?
28
39
  assert_includes doc.errors.keys, :body
29
40
  end
41
+
30
42
  should "not set rel=external" do
31
43
  doc = Dummy.new(body: 'abc [foobar](http://foobar.com){:rel="external"}')
32
44
  assert doc.invalid?
33
45
  assert_includes doc.errors.keys, :body
34
46
  end
47
+
35
48
  should "show multiple errors" do
36
49
  doc = Dummy.new(body: 'abc [foobar](foobar.com "bar"){:rel="external"}')
37
50
  assert doc.invalid?
38
51
  assert_equal 3, doc.errors[:body].first.length
39
52
  end
53
+
40
54
  should "only show each error once" do
41
55
  doc = Dummy.new(body: 'abc [link1](foobar.com), ghi [link2](bazquux.com)')
42
56
  assert doc.invalid?
43
57
  assert_equal 1, doc.errors[:body].first.length
44
58
  end
59
+
60
+ should "be validated when any attribute of the document changes" do
61
+ # already published document having link validation errors
62
+ doc = Dummy.new(body: 'abc [link1](foobar.com), ghi [link2](bazquux.com)')
63
+ doc.save(validate: false)
64
+
65
+ doc.assignee = "4fdef0000000000000000001"
66
+ assert doc.invalid?
67
+
68
+ assert_equal 1, doc.errors[:body].first.length
69
+ end
45
70
  end
46
71
  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: 13.0.0
4
+ version: 13.1.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-06-27 00:00:00.000000000 Z
12
+ date: 2014-06-30 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bson_ext
@@ -468,7 +468,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
468
468
  version: '0'
469
469
  segments:
470
470
  - 0
471
- hash: -4378837483235683997
471
+ hash: 3968322493535116070
472
472
  required_rubygems_version: !ruby/object:Gem::Requirement
473
473
  none: false
474
474
  requirements:
@@ -477,7 +477,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
477
477
  version: '0'
478
478
  segments:
479
479
  - 0
480
- hash: -4378837483235683997
480
+ hash: 3968322493535116070
481
481
  requirements: []
482
482
  rubyforge_project:
483
483
  rubygems_version: 1.8.23