govuk_content_models 15.0.0 → 15.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,12 @@
1
+ ## 15.1.0
2
+ * Add draft tag functionality to `Tag.by_tag_id/s`.
3
+ * Permit tag type to be provided via an options
4
+ hash, while retaining backwards compatibility.
5
+
6
+ ## 15.0.0
7
+ * Refactor tags.
8
+ * Provide draft functionality in the `taggable` trait.
9
+
1
10
  ## 14.1.1
2
11
  * Use another way to transition Editions without
3
12
  validation
data/app/models/tag.rb CHANGED
@@ -65,12 +65,24 @@ class Tag
65
65
  title
66
66
  end
67
67
 
68
- def self.by_tag_id(tag_id, tag_type = nil)
69
- by_tag_ids([tag_id], tag_type).first
68
+ def self.by_tag_id(tag_id, tag_type_or_options = nil)
69
+ by_tag_ids([tag_id], tag_type_or_options).first
70
70
  end
71
71
 
72
- def self.by_tag_ids(tag_id_list, tag_type = nil)
73
- tag_scope = tag_type ? Tag.where(tag_type: tag_type) : Tag
72
+ def self.by_tag_ids(tag_id_list, tag_type_or_options = nil)
73
+ if tag_type_or_options.is_a?(String)
74
+ # Providing the type as a string argument is deprecated in favour of providing the type as an option
75
+ options = {type: tag_type_or_options}
76
+ else
77
+ options = tag_type_or_options || {}
78
+ end
79
+
80
+ tag_scope = options[:type] ? Tag.where(tag_type: options[:type]) : Tag
81
+
82
+ unless options[:draft]
83
+ tag_scope = tag_scope.where(:state.ne => 'draft')
84
+ end
85
+
74
86
  tag_scope = tag_scope.any_in(tag_id: tag_id_list)
75
87
 
76
88
  # Sort by id list because MongoID 2.x doesn't preserve order
@@ -85,9 +97,10 @@ class Tag
85
97
  any_of(list)
86
98
  end
87
99
 
88
- # Retrieve a list of tags by tag ID. Any missing tags raise an exception.
100
+ # Validate a list of tags by tag ID. Any missing tags raise an exception.
101
+ # Draft tags are considered present for internal validation.
89
102
  def self.validate_tag_ids(tag_id_list, tag_type = nil)
90
- found_tags = by_tag_ids(tag_id_list, tag_type)
103
+ found_tags = by_tag_ids(tag_id_list, type: tag_type, draft: true)
91
104
  missing_tag_ids = tag_id_list - found_tags.map(&:tag_id)
92
105
  raise MissingTags.new(missing_tag_ids) if missing_tag_ids.any?
93
106
  end
@@ -95,12 +95,6 @@ module Taggable
95
95
  end
96
96
 
97
97
  def tags(include_draft = false)
98
- all_tags = Tag.by_tag_ids(tag_ids)
99
-
100
- if include_draft
101
- all_tags
102
- else
103
- all_tags.reject {|tag| tag.state == 'draft' }
104
- end
98
+ Tag.by_tag_ids(tag_ids, draft: include_draft)
105
99
  end
106
100
  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 = "15.0.0"
3
+ VERSION = "15.1.0"
4
4
  end
@@ -37,9 +37,14 @@ class TagTest < ActiveSupport::TestCase
37
37
  end
38
38
 
39
39
  test "should load by tag ID and type" do
40
+ # This form is deprecated in favour of providing the type as an option
40
41
  assert_equal "Crime", Tag.by_tag_id("crime", "section").title
41
42
  end
42
43
 
44
+ test "accepts the tag type as an option" do
45
+ assert_equal "Crime", Tag.by_tag_id("crime", type: "section").title
46
+ end
47
+
43
48
  test "should not load an incorrectly-typed tag" do
44
49
  assert_nil Tag.by_tag_id("crime", "keyword")
45
50
  end
@@ -62,6 +67,23 @@ class TagTest < ActiveSupport::TestCase
62
67
  assert_equal %w(crime business housing), tags.map(&:tag_id)
63
68
  end
64
69
 
70
+ test "should not return draft tags unless requested" do
71
+ draft_tag = FactoryGirl.create(:tag,
72
+ tag_id: "draft-tag",
73
+ tag_type: "section",
74
+ title: "A draft tag",
75
+ state: "draft"
76
+ )
77
+
78
+ tag_ids = %w(crime business draft-tag housing)
79
+
80
+ assert_equal %w(crime business housing).to_set, Tag.by_tag_ids(tag_ids).map(&:tag_id).to_set
81
+ assert_equal %w(crime business draft-tag housing).to_set, Tag.by_tag_ids(tag_ids, draft: true).map(&:tag_id).to_set
82
+
83
+ assert_nil Tag.by_tag_id('draft-tag')
84
+ assert_equal draft_tag, Tag.by_tag_id('draft-tag', draft: true)
85
+ end
86
+
65
87
  test "should return nil for tags of the wrong type" do
66
88
  tag_ids = %w(crime business pie batman)
67
89
  tags = Tag.by_tag_ids(tag_ids, "section")
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: 15.0.0
4
+ version: 15.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-07-11 00:00:00.000000000 Z
12
+ date: 2014-07-14 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bson_ext
@@ -466,7 +466,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
466
466
  version: '0'
467
467
  segments:
468
468
  - 0
469
- hash: -116450993641469610
469
+ hash: -630092204464727193
470
470
  required_rubygems_version: !ruby/object:Gem::Requirement
471
471
  none: false
472
472
  requirements:
@@ -475,7 +475,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
475
475
  version: '0'
476
476
  segments:
477
477
  - 0
478
- hash: -116450993641469610
478
+ hash: -630092204464727193
479
479
  requirements: []
480
480
  rubyforge_project:
481
481
  rubygems_version: 1.8.23