govuk_content_models 10.4.0 → 10.4.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG.md +4 -0
- data/app/validators/tag_id_validator.rb +26 -6
- data/lib/govuk_content_models/test_helpers/factories.rb +1 -1
- data/lib/govuk_content_models/version.rb +1 -1
- data/test/models/artefact_tag_test.rb +4 -6
- data/test/models/artefact_test.rb +1 -16
- data/test/traits/taggable_test.rb +4 -5
- data/test/validators/tag_id_validator_test.rb +10 -4
- metadata +4 -4
data/CHANGELOG.md
CHANGED
@@ -1,17 +1,37 @@
|
|
1
1
|
class TagIdValidator < ActiveModel::Validator
|
2
2
|
|
3
3
|
def validate(record)
|
4
|
-
unless valid_tag_id?(record
|
4
|
+
unless valid_tag_id?(record)
|
5
5
|
record.errors[:tag_id] << "ID must be valid in a URL and have no more than one slash"
|
6
6
|
end
|
7
7
|
end
|
8
8
|
|
9
|
-
|
9
|
+
private
|
10
10
|
|
11
|
-
def valid_tag_id?(
|
12
|
-
tag_id
|
13
|
-
|
14
|
-
|
11
|
+
def valid_tag_id?(tag)
|
12
|
+
tag_contains_correct_characters(tag.tag_id) &&
|
13
|
+
tag_doesnt_end_with_slash(tag.tag_id) &&
|
14
|
+
tag_contains_correct_number_of_slashes(tag.tag_id, child?: tag_is_child_tag?(tag))
|
15
|
+
end
|
16
|
+
|
17
|
+
def tag_contains_correct_characters(tag_id)
|
18
|
+
tag_id =~ %r{^[a-z0-9/-]+$}
|
19
|
+
end
|
20
|
+
|
21
|
+
def tag_doesnt_end_with_slash(tag_id)
|
22
|
+
!tag_id.end_with?('/')
|
23
|
+
end
|
24
|
+
|
25
|
+
def tag_contains_correct_number_of_slashes(tag_id, options = {})
|
26
|
+
if options[:child?]
|
27
|
+
tag_id.count('/') <= 1
|
28
|
+
else
|
29
|
+
tag_id.count('/') == 0
|
30
|
+
end
|
31
|
+
end
|
32
|
+
|
33
|
+
def tag_is_child_tag?(tag)
|
34
|
+
tag.respond_to?(:parent_id) && tag.parent_id.present?
|
15
35
|
end
|
16
36
|
|
17
37
|
end
|
@@ -2,18 +2,16 @@ require "test_helper"
|
|
2
2
|
|
3
3
|
class ArtefactTagTest < ActiveSupport::TestCase
|
4
4
|
|
5
|
-
TEST_SECTIONS = [
|
6
|
-
['crime', 'Crime'], ['crime/the-police', 'The Police'], ['crime/batman', 'Batman']
|
7
|
-
]
|
8
5
|
TEST_KEYWORDS = [['cheese', 'Cheese'], ['bacon', 'Bacon']]
|
9
6
|
TEST_LEGACY_SOURCES = [
|
10
7
|
['businesslink', 'Business Link'], ['directgov', 'Directgov'], ['dvla', 'DVLA']
|
11
8
|
]
|
12
9
|
|
13
10
|
setup do
|
14
|
-
|
15
|
-
|
16
|
-
|
11
|
+
parent_section = FactoryGirl.create(:tag, :tag_id => 'crime', :tag_type => 'section', :title => 'Crime')
|
12
|
+
FactoryGirl.create(:tag, :tag_id => 'crime/the-police', :tag_type => 'section', :title => 'The Police', :parent_id => parent_section.id)
|
13
|
+
FactoryGirl.create(:tag, :tag_id => 'crime/batman', :tag_type => 'section', :title => 'Batman', :parent_id => parent_section.id)
|
14
|
+
|
17
15
|
TEST_KEYWORDS.each do |tag_id, title|
|
18
16
|
FactoryGirl.create(:tag, :tag_id => tag_id, :tag_type => 'keyword', :title => title)
|
19
17
|
end
|
@@ -61,7 +61,7 @@ class ArtefactTest < ActiveSupport::TestCase
|
|
61
61
|
a = FactoryGirl.build(:artefact, slug: "government/SomeThing/some.where/somehow/slug", kind: "case_study")
|
62
62
|
refute a.valid?
|
63
63
|
end
|
64
|
-
|
64
|
+
|
65
65
|
should "require a government prefix for Inside Government artefacts" do
|
66
66
|
a = FactoryGirl.build(:artefact, slug: "slug", kind: "case_study")
|
67
67
|
refute a.valid?
|
@@ -648,20 +648,5 @@ class ArtefactTest < ActiveSupport::TestCase
|
|
648
648
|
assert_equal [], @artefact.related_artefacts_grouped_by_distance["section"]
|
649
649
|
assert_equal ["pear", "banana"], @artefact.related_artefacts_grouped_by_distance["other"].map(&:slug)
|
650
650
|
end
|
651
|
-
|
652
|
-
should "return no section level related artefacts if the primary section has no parent_id" do
|
653
|
-
FactoryGirl.create(:tag, :tag_id => "fruit/multiple", :tag_type => 'section', :title => "Multiple fruits", :parent_id => nil)
|
654
|
-
|
655
|
-
@artefact.primary_section = "fruit/multiple"
|
656
|
-
@artefact.related_artefacts = [
|
657
|
-
Artefact.create!(slug: "fig", name: "Fig", kind: "guide", sections: ["fruit/multiple"], need_ids: ["100001"], owning_app: "x"),
|
658
|
-
Artefact.create!(slug: "strawberry", name: "Strawberry", kind: "guide", sections: ["fruit/simple"], need_ids: ["100001"], owning_app: "x")
|
659
|
-
]
|
660
|
-
@artefact.save!
|
661
|
-
|
662
|
-
assert_equal ["fig"], @artefact.related_artefacts_grouped_by_distance["subsection"].map(&:slug)
|
663
|
-
assert_equal [], @artefact.related_artefacts_grouped_by_distance["section"]
|
664
|
-
assert_equal ["strawberry"], @artefact.related_artefacts_grouped_by_distance["other"].map(&:slug)
|
665
|
-
end
|
666
651
|
end
|
667
652
|
end
|
@@ -3,14 +3,13 @@ require 'test_helper'
|
|
3
3
|
# This test relies on the fact that artefact uses the taggable module
|
4
4
|
class TaggableTest < ActiveSupport::TestCase
|
5
5
|
|
6
|
-
TEST_SECTIONS = [['crime', 'Crime'], ['crime/the-police', 'The Police'],
|
7
|
-
['crime/batman', 'Batman']]
|
8
6
|
TEST_KEYWORDS = [['cheese', 'Cheese'], ['bacon', 'Bacon']]
|
9
7
|
|
10
8
|
setup do
|
11
|
-
|
12
|
-
|
13
|
-
|
9
|
+
parent_section = FactoryGirl.create(:tag, :tag_id => 'crime', :tag_type => 'section', :title => 'Crime')
|
10
|
+
FactoryGirl.create(:tag, :tag_id => 'crime/the-police', :tag_type => 'section', :title => 'The Police', :parent_id => parent_section.id)
|
11
|
+
FactoryGirl.create(:tag, :tag_id => 'crime/batman', :tag_type => 'section', :title => 'Batman', :parent_id => parent_section.id)
|
12
|
+
|
14
13
|
TEST_KEYWORDS.each do |tag_id, title|
|
15
14
|
FactoryGirl.create(:tag, :tag_id => tag_id, :tag_type => 'keyword', :title => title)
|
16
15
|
end
|
@@ -40,13 +40,19 @@ class TagIdValidatorTest < ActiveSupport::TestCase
|
|
40
40
|
assert dummy.errors.has_key?(:tag_id)
|
41
41
|
end
|
42
42
|
|
43
|
-
should "permit a tag id containing a slash" do
|
44
|
-
dummy = Dummy.new(tag_id: "parent-tag-id/child-tag-id")
|
43
|
+
should "permit a child tag id containing a slash" do
|
44
|
+
dummy = Dummy.new(tag_id: "parent-tag-id/child-tag-id", parent_id: 1)
|
45
45
|
assert dummy.valid?
|
46
46
|
end
|
47
47
|
|
48
|
-
should "not permit
|
49
|
-
dummy = Dummy.new(tag_id: "parent-tag-id
|
48
|
+
should "not permit a parent tag id containing a slash" do
|
49
|
+
dummy = Dummy.new(tag_id: "an-invalid/parent-tag-id", parent_id: nil)
|
50
|
+
refute dummy.valid?
|
51
|
+
assert dummy.errors.has_key?(:tag_id)
|
52
|
+
end
|
53
|
+
|
54
|
+
should "not permit a child tag id with more than one slash" do
|
55
|
+
dummy = Dummy.new(tag_id: "parent-tag-id/two/slashes", parent_id: 1)
|
50
56
|
refute dummy.valid?
|
51
57
|
assert dummy.errors.has_key?(:tag_id)
|
52
58
|
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: 10.4.
|
4
|
+
version: 10.4.1
|
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-04-
|
12
|
+
date: 2014-04-24 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bson_ext
|
@@ -465,7 +465,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
465
465
|
version: '0'
|
466
466
|
segments:
|
467
467
|
- 0
|
468
|
-
hash:
|
468
|
+
hash: 1806516958570881698
|
469
469
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
470
470
|
none: false
|
471
471
|
requirements:
|
@@ -474,7 +474,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
474
474
|
version: '0'
|
475
475
|
segments:
|
476
476
|
- 0
|
477
|
-
hash:
|
477
|
+
hash: 1806516958570881698
|
478
478
|
requirements: []
|
479
479
|
rubyforge_project:
|
480
480
|
rubygems_version: 1.8.23
|