govuk_content_models 10.1.0 → 10.1.1

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG.md CHANGED
@@ -1,3 +1,7 @@
1
+ ## 10.1.1
2
+
3
+ * Allow an artefact to be associated with multiple needs
4
+
1
5
  ## 10.0.0
2
6
 
3
7
  * Add validation for tag IDs
@@ -33,7 +33,12 @@ class Artefact
33
33
  field "owning_app", type: String
34
34
  field "rendering_app", type: String
35
35
  field "active", type: Boolean, default: false
36
+
37
+ # will be removed once multiple need_ids
38
+ # gets deployed and tested.
36
39
  field "need_id", type: String
40
+
41
+ field "need_ids", type: Array, default: []
37
42
  field "fact_checkers", type: String
38
43
  field "publication_id", type: String
39
44
  field "description", type: String
@@ -142,6 +147,7 @@ class Artefact
142
147
  reject_if: proc { |attrs| attrs["title"].blank? && attrs["url"].blank? }
143
148
 
144
149
  before_validation :normalise, on: :create
150
+ before_validation :filter_out_empty_need_ids, if: :need_ids_changed?
145
151
  before_create :record_create_action
146
152
  before_update :record_update_action
147
153
  after_update :update_editions
@@ -154,6 +160,7 @@ class Artefact
154
160
  validates :language, inclusion: { in: ["en", "cy"] }
155
161
  validates_with CannotEditSlugIfEverPublished
156
162
  validate :validate_prefixes_and_paths
163
+ validate :format_of_new_need_ids, if: :need_ids_changed?
157
164
 
158
165
  def self.in_alphabetical_order
159
166
  order_by([[:name, :asc]])
@@ -347,6 +354,13 @@ class Artefact
347
354
  attributes.except "_id", "created_at", "updated_at", "actions"
348
355
  end
349
356
 
357
+ def need_id=(new_need_id)
358
+ super
359
+
360
+ need_ids << new_need_id if new_need_id.present? && ! need_ids.include?(new_need_id)
361
+ new_need_id
362
+ end
363
+
350
364
  private
351
365
 
352
366
  def validate_prefixes_and_paths
@@ -362,6 +376,16 @@ class Artefact
362
376
  end
363
377
  end
364
378
 
379
+ def filter_out_empty_need_ids
380
+ need_ids.reject!(&:blank?)
381
+ end
382
+
383
+ def format_of_new_need_ids
384
+ # http://api.rubyonrails.org/classes/ActiveModel/Dirty.html
385
+ new_need_ids = need_ids_was.blank? ? need_ids : need_ids - need_ids_was
386
+ errors.add(:need_ids, "must be six-digit integers") if new_need_ids.any? {|need_id| need_id !~ /^\d{6}$/ }
387
+ end
388
+
365
389
  def valid_url_path?(path)
366
390
  return false unless path.starts_with?("/")
367
391
  uri = URI.parse(path)
@@ -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 = "10.1.0"
3
+ VERSION = "10.1.1"
4
4
  end
@@ -22,6 +22,7 @@ class ArtefactActionTest < ActiveSupport::TestCase
22
22
  "active" => false,
23
23
  "tag_ids" => [],
24
24
  "tags" => [],
25
+ "need_ids" => [],
25
26
  "state" => "draft",
26
27
  "related_artefact_ids" => [],
27
28
  "paths" => [],
@@ -92,6 +92,76 @@ class ArtefactTest < ActiveSupport::TestCase
92
92
  end
93
93
  end
94
94
 
95
+ context "#need_ids" do
96
+ should "be empty by default" do
97
+ assert_empty FactoryGirl.build(:artefact).need_ids
98
+ end
99
+
100
+ should "filter out empty strings" do
101
+ artefact = FactoryGirl.create(:artefact, need_ids: ["", "100002"])
102
+ assert_equal ["100002"], artefact.reload.need_ids
103
+ end
104
+
105
+ should "store multiple needs related to an artefact" do
106
+ artefact = FactoryGirl.create(:artefact, need_ids: ["100001", "100002"])
107
+ assert_equal ["100001", "100002"], artefact.reload.need_ids
108
+ end
109
+
110
+ should "be six-digit integers" do
111
+ artefact = FactoryGirl.build(:artefact, need_ids: ["B1231"])
112
+
113
+ refute artefact.valid?
114
+ assert_includes artefact.errors[:need_ids], "must be six-digit integers"
115
+ end
116
+
117
+ should "not validate need ids that were migrated from the singular need_id field" do
118
+ artefact = FactoryGirl.create(:artefact)
119
+ # simulate what happened during migration
120
+ artefact.set(:need_ids, ['As an employer
121
+ I need to know which type of DBS check an employee needs
122
+ so that I can apply for the correct one'])
123
+
124
+ artefact.need_ids << "100045"
125
+
126
+ assert artefact.valid?
127
+ end
128
+
129
+ context "for backwards compatibility" do
130
+ setup do
131
+ @artefact = FactoryGirl.create(:artefact)
132
+ end
133
+
134
+ should "append to need_ids when need_id is assigned" do
135
+ @artefact.need_id = "100045"
136
+
137
+ assert_equal "100045", @artefact.need_id
138
+ assert_includes @artefact.need_ids, "100045"
139
+ end
140
+
141
+ should "append to existing need_ids when need_id is assigned" do
142
+ @artefact.set(:need_ids, ["100044"])
143
+ @artefact.set(:need_id, "100044")
144
+
145
+ @artefact.need_id = "100045"
146
+
147
+ assert_equal "100045", @artefact.need_id
148
+ assert_equal ["100044", "100045"], @artefact.need_ids
149
+ end
150
+
151
+ # this should only matter till the time we have both fields
152
+ # need_id and need_ids. can delete this test once we unset need_id.
153
+ should "keep need_ids unchanged when need_id is removed" do
154
+ @artefact.set(:need_ids, ["100044", "100045"])
155
+ @artefact.set(:need_id, "100044")
156
+
157
+ @artefact.need_id = nil
158
+
159
+ assert_equal nil, @artefact.need_id
160
+ assert_equal ["100044", "100045"], @artefact.need_ids
161
+ end
162
+ end
163
+ end
164
+
95
165
  context "validating paths and prefixes" do
96
166
  setup do
97
167
  @a = FactoryGirl.build(:artefact)
@@ -167,9 +237,9 @@ class ArtefactTest < ActiveSupport::TestCase
167
237
  end
168
238
 
169
239
  test "should store and return related artefacts in order" do
170
- a = Artefact.create!(slug: "a", name: "a", kind: "place", need_id: 1, owning_app: "x")
171
- b = Artefact.create!(slug: "b", name: "b", kind: "place", need_id: 2, owning_app: "x")
172
- c = Artefact.create!(slug: "c", name: "c", kind: "place", need_id: 3, owning_app: "x")
240
+ a = Artefact.create!(slug: "a", name: "a", kind: "place", need_ids: ["100001"], owning_app: "x")
241
+ b = Artefact.create!(slug: "b", name: "b", kind: "place", need_ids: ["100001"], owning_app: "x")
242
+ c = Artefact.create!(slug: "c", name: "c", kind: "place", need_ids: ["100001"], owning_app: "x")
173
243
 
174
244
  a.related_artefacts = [b, c]
175
245
  a.save!
@@ -179,9 +249,9 @@ class ArtefactTest < ActiveSupport::TestCase
179
249
  end
180
250
 
181
251
  test "should store and return related artefacts in order, even when not in natural order" do
182
- a = Artefact.create!(slug: "a", name: "a", kind: "place", need_id: 1, owning_app: "x")
183
- b = Artefact.create!(slug: "b", name: "b", kind: "place", need_id: 2, owning_app: "x")
184
- c = Artefact.create!(slug: "c", name: "c", kind: "place", need_id: 3, owning_app: "x")
252
+ a = Artefact.create!(slug: "a", name: "a", kind: "place", need_ids: ["100001"], owning_app: "x")
253
+ b = Artefact.create!(slug: "b", name: "b", kind: "place", need_ids: ["100001"], owning_app: "x")
254
+ c = Artefact.create!(slug: "c", name: "c", kind: "place", need_ids: ["100001"], owning_app: "x")
185
255
 
186
256
  a.related_artefacts = [c, b]
187
257
  a.save!
@@ -191,10 +261,10 @@ class ArtefactTest < ActiveSupport::TestCase
191
261
  end
192
262
 
193
263
  test "should store and return related artefacts in order, with a scope" do
194
- a = Artefact.create!(slug: "a", name: "a", kind: "place", need_id: 1, owning_app: "x")
195
- b = Artefact.create!(state: "live", slug: "b", name: "b", kind: "place", need_id: 2, owning_app: "x")
196
- c = Artefact.create!(slug: "c", name: "c", kind: "place", need_id: 3, owning_app: "x")
197
- d = Artefact.create!(state: "live", slug: "d", name: "d", kind: "place", need_id: 3, owning_app: "x")
264
+ a = Artefact.create!(slug: "a", name: "a", kind: "place", need_ids: ["100001"], owning_app: "x")
265
+ b = Artefact.create!(state: "live", slug: "b", name: "b", kind: "place", need_ids: ["100001"], owning_app: "x")
266
+ c = Artefact.create!(slug: "c", name: "c", kind: "place", need_ids: ["100001"], owning_app: "x")
267
+ d = Artefact.create!(state: "live", slug: "d", name: "d", kind: "place", need_ids: ["100001"], owning_app: "x")
198
268
 
199
269
  a.related_artefacts = [d, c, b]
200
270
  a.save!
@@ -509,7 +579,7 @@ class ArtefactTest < ActiveSupport::TestCase
509
579
  FactoryGirl.create(:tag, :tag_id => "fruit/aggregate", :tag_type => 'section', :title => "Aggregrate fruits", :parent_id => "fruit")
510
580
  FactoryGirl.create(:tag, :tag_id => "vegetables", :tag_type => 'section', :title => "Vegetables")
511
581
 
512
- @artefact = Artefact.create!(slug: "apple", name: "Apple", sections: [], kind: "guide", need_id: 1, owning_app: "x")
582
+ @artefact = Artefact.create!(slug: "apple", name: "Apple", sections: [], kind: "guide", need_ids: ["100001"], owning_app: "x")
513
583
  end
514
584
 
515
585
  context "when related items are present in all groups" do
@@ -517,9 +587,9 @@ class ArtefactTest < ActiveSupport::TestCase
517
587
  @artefact.sections = ["fruit/simple"]
518
588
 
519
589
  @artefact.related_artefacts = [
520
- Artefact.create!(slug: "pear", name: "Pear", kind: "guide", sections: ["fruit/simple"], need_id: 4, owning_app: "x"),
521
- Artefact.create!(slug: "pineapple", name: "Pineapple", kind: "guide", sections: ["fruit/aggregate"], need_id: 2, owning_app: "x"),
522
- Artefact.create!(slug: "broccoli", name: "Broccoli", kind: "guide", sections: ["vegetables"], need_id: 3, owning_app: "x")
590
+ Artefact.create!(slug: "pear", name: "Pear", kind: "guide", sections: ["fruit/simple"], need_ids: ["100001"], owning_app: "x"),
591
+ Artefact.create!(slug: "pineapple", name: "Pineapple", kind: "guide", sections: ["fruit/aggregate"], need_ids: ["100001"], owning_app: "x"),
592
+ Artefact.create!(slug: "broccoli", name: "Broccoli", kind: "guide", sections: ["vegetables"], need_ids: ["100001"], owning_app: "x")
523
593
  ]
524
594
  @artefact.save!
525
595
  @artefact.reload
@@ -541,9 +611,9 @@ class ArtefactTest < ActiveSupport::TestCase
541
611
  end
542
612
 
543
613
  should "return related artefacts in order, with a scope" do
544
- a = Artefact.create!(state: "live", slug: "a", name: "a", kind: "place", need_id: 1, owning_app: "x")
545
- b = Artefact.create!(slug: "b", name: "b", kind: "place", need_id: 2, owning_app: "x")
546
- c = Artefact.create!(state: "live", slug: "c", name: "c", kind: "place", need_id: 3, owning_app: "x")
614
+ a = Artefact.create!(state: "live", slug: "a", name: "a", kind: "place", need_ids: ["100001"], owning_app: "x")
615
+ b = Artefact.create!(slug: "b", name: "b", kind: "place", need_ids: ["100001"], owning_app: "x")
616
+ c = Artefact.create!(state: "live", slug: "c", name: "c", kind: "place", need_ids: ["100001"], owning_app: "x")
547
617
 
548
618
  @artefact.related_artefacts = [c,b,a]
549
619
  @artefact.save!
@@ -563,8 +633,8 @@ class ArtefactTest < ActiveSupport::TestCase
563
633
 
564
634
  should "return all related artefacts in 'other' when an artefact has no sections" do
565
635
  @artefact.related_artefacts = [
566
- Artefact.create!(slug: "pear", name: "Pear", kind: "guide", sections: ["fruit/simple"], need_id: 4, owning_app: "x"),
567
- Artefact.create!(slug: "banana", name: "Banana", kind: "guide", sections: ["fruit/simple"], need_id: 6, owning_app: "x")
636
+ Artefact.create!(slug: "pear", name: "Pear", kind: "guide", sections: ["fruit/simple"], need_ids: ["100001"], owning_app: "x"),
637
+ Artefact.create!(slug: "banana", name: "Banana", kind: "guide", sections: ["fruit/simple"], need_ids: ["100001"], owning_app: "x")
568
638
  ]
569
639
 
570
640
  assert_equal [], @artefact.related_artefacts_grouped_by_distance["subsection"]
@@ -577,8 +647,8 @@ class ArtefactTest < ActiveSupport::TestCase
577
647
 
578
648
  @artefact.primary_section = "fruit/multiple"
579
649
  @artefact.related_artefacts = [
580
- Artefact.create!(slug: "fig", name: "Fig", kind: "guide", sections: ["fruit/multiple"], need_id: 4, owning_app: "x"),
581
- Artefact.create!(slug: "strawberry", name: "Strawberry", kind: "guide", sections: ["fruit/simple"], need_id: 6, owning_app: "x")
650
+ Artefact.create!(slug: "fig", name: "Fig", kind: "guide", sections: ["fruit/multiple"], need_ids: ["100001"], owning_app: "x"),
651
+ Artefact.create!(slug: "strawberry", name: "Strawberry", kind: "guide", sections: ["fruit/simple"], need_ids: ["100001"], owning_app: "x")
582
652
  ]
583
653
  @artefact.save!
584
654
 
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.1.0
4
+ version: 10.1.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-07 00:00:00.000000000 Z
12
+ date: 2014-04-08 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: -4548551299969788770
468
+ hash: -3442913868239428672
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: -4548551299969788770
477
+ hash: -3442913868239428672
478
478
  requirements: []
479
479
  rubyforge_project:
480
480
  rubygems_version: 1.8.23