govuk_content_models 28.2.0 → 28.3.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
+ ## 28.3.0
2
+
3
+ - Add error message when validating primary and additional topics
4
+ - Embed part errors within edition errors
5
+
1
6
  ## 28.2.0
2
7
 
3
8
  - Add an index for `created_at` on Edition.
data/app/models/parted.rb CHANGED
@@ -5,6 +5,7 @@ module Parted
5
5
  klass.embeds_many :parts
6
6
  klass.accepts_nested_attributes_for :parts, allow_destroy: true,
7
7
  reject_if: proc { |attrs| attrs["title"].blank? and attrs["body"].blank? }
8
+ klass.after_validation :merge_embedded_parts_errors
8
9
  end
9
10
 
10
11
  def build_clone(edition_class=nil)
@@ -29,4 +30,18 @@ module Parted
29
30
  def whole_body
30
31
  self.parts.map {|i| %Q{\# #{i.title}\n\n#{i.body}} }.join("\n\n")
31
32
  end
33
+
34
+ private
35
+
36
+ def merge_embedded_parts_errors
37
+ return if parts.empty?
38
+
39
+ if errors.delete(:parts) == ["is invalid"]
40
+ parts_errors = parts.inject({}) do |result, part|
41
+ result["#{part._id}:#{part.order}"] = part.errors.messages if part.errors.present?
42
+ result
43
+ end
44
+ errors.add(:parts, parts_errors)
45
+ end
46
+ end
32
47
  end
@@ -7,7 +7,7 @@ class LinkValidator < ActiveModel::Validator
7
7
  next if govspeak_field_value.blank?
8
8
 
9
9
  messages = errors(govspeak_field_value)
10
- record.errors[govspeak_field_name] << messages if messages
10
+ record.errors[govspeak_field_name] << messages unless messages.blank?
11
11
  end
12
12
  end
13
13
 
@@ -7,6 +7,7 @@ class TopicValidator < ActiveModel::Validator
7
7
 
8
8
  if additional_topics.include?(record.primary_topic)
9
9
  record.errors.add(:base, "You can't have the primary topic set as an additional topic")
10
+ record.errors.add(:additional_topics, "can't have the primary topic set as an additional topic")
10
11
  end
11
12
  end
12
13
  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 = "28.2.0"
3
+ VERSION = "28.3.0"
4
4
  end
@@ -0,0 +1,18 @@
1
+ require "test_helper"
2
+ # require "edition"
3
+ # require "parted"
4
+
5
+ class PartedTest < ActiveSupport::TestCase
6
+ test "should merge part validation errors with parent document's errors" do
7
+ edition = FactoryGirl.create(:guide_edition)
8
+ edition.parts.build(_id: '54c10d4d759b743528000010', order: '1', title: "", slug: "overview")
9
+ edition.parts.build(_id: '54c10d4d759b743528000011', order: '2', title: "Prepare for your appointment", slug: "")
10
+ edition.parts.build(_id: '54c10d4d759b743528000012', order: '3', title: "Valid", slug: "valid")
11
+
12
+ refute edition.valid?
13
+
14
+ assert_equal({title: ["can't be blank"]}, edition.errors[:parts][0]['54c10d4d759b743528000010:1'])
15
+ assert_equal({slug: ["can't be blank", "is invalid"]}, edition.errors[:parts][0]['54c10d4d759b743528000011:2'])
16
+ assert_equal 2, edition.errors[:parts][0].length
17
+ end
18
+ end
@@ -23,6 +23,13 @@ class LinkValidatorTest < ActiveSupport::TestCase
23
23
  assert_empty doc.errors
24
24
  end
25
25
 
26
+ should "not contain empty array for errors on fields" do
27
+ doc = Dummy.new(body: "Nothing is invalid")
28
+
29
+ assert doc.valid?
30
+ assert_empty doc.errors[:body]
31
+ end
32
+
26
33
  should "start with http[s]://, mailto: or /" do
27
34
  doc = Dummy.new(body: "abc [external](external.com)")
28
35
  assert doc.invalid?
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: 28.2.0
4
+ version: 28.3.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: 2015-01-20 00:00:00.000000000 Z
12
+ date: 2015-01-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: bson_ext
@@ -440,6 +440,7 @@ files:
440
440
  - test/models/local_transaction_edition_test.rb
441
441
  - test/models/manual_change_history_test.rb
442
442
  - test/models/overview_dashboard_test.rb
443
+ - test/models/parted_test.rb
443
444
  - test/models/prerendered_entity_tests.rb
444
445
  - test/models/rendered_manual_test.rb
445
446
  - test/models/rendered_specialist_document_test.rb
@@ -477,7 +478,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
477
478
  version: '0'
478
479
  segments:
479
480
  - 0
480
- hash: -3094189113344547894
481
+ hash: 2774210225727487391
481
482
  required_rubygems_version: !ruby/object:Gem::Requirement
482
483
  none: false
483
484
  requirements:
@@ -486,7 +487,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
486
487
  version: '0'
487
488
  segments:
488
489
  - 0
489
- hash: -3094189113344547894
490
+ hash: 2774210225727487391
490
491
  requirements: []
491
492
  rubyforge_project:
492
493
  rubygems_version: 1.8.23
@@ -522,6 +523,7 @@ test_files:
522
523
  - test/models/local_transaction_edition_test.rb
523
524
  - test/models/manual_change_history_test.rb
524
525
  - test/models/overview_dashboard_test.rb
526
+ - test/models/parted_test.rb
525
527
  - test/models/prerendered_entity_tests.rb
526
528
  - test/models/rendered_manual_test.rb
527
529
  - test/models/rendered_specialist_document_test.rb