govuk_content_models 6.0.4 → 6.0.5
Sign up to get free protection for your applications and to get access to all the features.
- data/app/models/travel_advice_edition.rb +1 -10
- data/app/validators/slug_validator.rb +1 -1
- data/lib/govuk_content_models.rb +2 -1
- data/lib/govuk_content_models/require_all.rb +1 -0
- data/lib/govuk_content_models/version.rb +1 -1
- data/lib/mongoid/monkey_patches.rb +17 -0
- data/test/models/artefact_test.rb +7 -0
- data/test/models/edition_test.rb +15 -0
- data/test/models/travel_advice_edition_test.rb +9 -0
- data/test/models/workflow_test.rb +18 -0
- metadata +5 -4
@@ -151,16 +151,7 @@ class TravelAdviceEdition
|
|
151
151
|
end
|
152
152
|
|
153
153
|
def anything_other_than_state_changed?(*additional_allowed_fields)
|
154
|
-
self.changed? and ((
|
155
|
-
end
|
156
|
-
|
157
|
-
def real_fields_changed
|
158
|
-
# There's an issue with dirty-tracking of Array fields. Merely accessing them will mark
|
159
|
-
# them as changed, but with no changes. This recifies that.
|
160
|
-
# this also allows changes when the change is something changing from nil to an empty array
|
161
|
-
self.changes.reject { |k, v|
|
162
|
-
v.nil? || v == [nil, []]
|
163
|
-
}.keys
|
154
|
+
self.changed? and ((changes.keys - ['state'] - additional_allowed_fields) != [] or self.parts.any?(&:changed?))
|
164
155
|
end
|
165
156
|
|
166
157
|
def alert_status_contains_valid_values
|
@@ -15,7 +15,7 @@ class SlugValidator < ActiveModel::EachValidator
|
|
15
15
|
elsif value.to_s =~ /\Agovernment\/(.+)/ and prefixed_inside_government_format_names.include?(record.kind)
|
16
16
|
parts = $1.split('/')
|
17
17
|
else
|
18
|
-
parts = [value]
|
18
|
+
parts = [value.clone]
|
19
19
|
end
|
20
20
|
|
21
21
|
if record.respond_to?(:kind)
|
data/lib/govuk_content_models.rb
CHANGED
@@ -0,0 +1,17 @@
|
|
1
|
+
# Mongoid 2.x does not track Array field changes accurately.
|
2
|
+
# Accessing an array field will mark it dirty which becomes problematic
|
3
|
+
# for workflow callbacks, e.g. when checking if a published edition is being edited.
|
4
|
+
# See https://github.com/mongoid/mongoid/issues/2311 for details of the mongoid issue.
|
5
|
+
#
|
6
|
+
module Mongoid
|
7
|
+
module Dirty
|
8
|
+
def changes
|
9
|
+
_changes = {}
|
10
|
+
changed.each do |attr|
|
11
|
+
change = attribute_change(attr)
|
12
|
+
_changes[attr] = change if change
|
13
|
+
end
|
14
|
+
_changes
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
@@ -371,6 +371,13 @@ class ArtefactTest < ActiveSupport::TestCase
|
|
371
371
|
end
|
372
372
|
end
|
373
373
|
|
374
|
+
should "not remove double dashes in a Detailed Guide slug" do
|
375
|
+
a = FactoryGirl.create(:artefact, slug: "duplicate-slug--1", kind: "detailed_guide")
|
376
|
+
a.reload
|
377
|
+
|
378
|
+
assert_equal "duplicate-slug--1", a.slug
|
379
|
+
end
|
380
|
+
|
374
381
|
context "artefact language" do
|
375
382
|
should "return english by default" do
|
376
383
|
a = FactoryGirl.create(:artefact)
|
data/test/models/edition_test.rb
CHANGED
@@ -303,6 +303,21 @@ class EditionTest < ActiveSupport::TestCase
|
|
303
303
|
assert_equal 7, new_edition.parts.size #there are 5 'default' parts plus an additional two created by the factory
|
304
304
|
end
|
305
305
|
|
306
|
+
# Mongoid 2.x marks array fields as dirty whenever they are accessed.
|
307
|
+
# See https://github.com/mongoid/mongoid/issues/2311
|
308
|
+
# This behaviour has been patched in lib/mongoid/monkey_patches.rb
|
309
|
+
# in order to prevent workflow validation failures for editions
|
310
|
+
# with array fields.
|
311
|
+
#
|
312
|
+
test "editions with array fields should accurately track changes" do
|
313
|
+
bs = FactoryGirl.create(:business_support_edition, sectors: [])
|
314
|
+
assert_empty bs.changes
|
315
|
+
bs.sectors
|
316
|
+
assert_empty bs.changes
|
317
|
+
bs.sectors << 'manufacturing'
|
318
|
+
assert_equal ['sectors'], bs.changes.keys
|
319
|
+
end
|
320
|
+
|
306
321
|
test "edition finder should return the published edition when given an empty edition parameter" do
|
307
322
|
dummy_publication = template_published_answer
|
308
323
|
second_publication = template_unpublished_answer(2)
|
@@ -136,6 +136,15 @@ class TravelAdviceEditionTest < ActiveSupport::TestCase
|
|
136
136
|
@ta.alert_status = [ ]
|
137
137
|
assert @ta.valid?
|
138
138
|
end
|
139
|
+
|
140
|
+
# Test that accessing an Array field doesn't mark it as dirty.
|
141
|
+
# mongoid/dirty#changes method is patched in lib/mongoid/monkey_patches.rb
|
142
|
+
# See https://github.com/mongoid/mongoid/issues/2311 for details.
|
143
|
+
should "track changes to alert status accurately" do
|
144
|
+
@ta.alert_status = [ ]
|
145
|
+
@ta.alert_status
|
146
|
+
assert @ta.valid?
|
147
|
+
end
|
139
148
|
end
|
140
149
|
|
141
150
|
context "on version_number" do
|
@@ -304,4 +304,22 @@ class WorkflowTest < ActiveSupport::TestCase
|
|
304
304
|
user.take_action!(edition, "archive")
|
305
305
|
assert_equal "archived", edition.state
|
306
306
|
end
|
307
|
+
|
308
|
+
# Mongoid 2.x marks array fields as dirty whenever they are accessed.
|
309
|
+
# See https://github.com/mongoid/mongoid/issues/2311
|
310
|
+
# This behaviour has been patched in lib/mongoid/monkey_patches.rb
|
311
|
+
# in order to prevent workflow validation failures for editions
|
312
|
+
# with array fields.
|
313
|
+
#
|
314
|
+
test "not_editing_published_item should not consider unchanged array fields as changes" do
|
315
|
+
bs = FactoryGirl.create(:business_support_edition, state: 'published', sectors: [])
|
316
|
+
assert_empty bs.errors
|
317
|
+
bs.sectors # Access the Array field
|
318
|
+
bs.valid?
|
319
|
+
assert_empty bs.errors
|
320
|
+
bs.sectors << 'education'
|
321
|
+
assert_equal ['sectors'], bs.changes.keys
|
322
|
+
bs.valid?
|
323
|
+
assert_equal "Published editions can't be edited", bs.errors[:base].first
|
324
|
+
end
|
307
325
|
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: 6.0.
|
4
|
+
version: 6.0.5
|
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-01-
|
12
|
+
date: 2014-01-13 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: bson_ext
|
@@ -397,6 +397,7 @@ files:
|
|
397
397
|
- lib/govuk_content_models/test_helpers/factories.rb
|
398
398
|
- lib/govuk_content_models/test_helpers/local_services.rb
|
399
399
|
- lib/govuk_content_models/version.rb
|
400
|
+
- lib/mongoid/monkey_patches.rb
|
400
401
|
- test/fixtures/contactotron_api_response.json
|
401
402
|
- test/fixtures/uploads/image.jpg
|
402
403
|
- test/models/artefact_action_test.rb
|
@@ -452,7 +453,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
452
453
|
version: '0'
|
453
454
|
segments:
|
454
455
|
- 0
|
455
|
-
hash:
|
456
|
+
hash: 888131122217194786
|
456
457
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
457
458
|
none: false
|
458
459
|
requirements:
|
@@ -461,7 +462,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
461
462
|
version: '0'
|
462
463
|
segments:
|
463
464
|
- 0
|
464
|
-
hash:
|
465
|
+
hash: 888131122217194786
|
465
466
|
requirements: []
|
466
467
|
rubyforge_project:
|
467
468
|
rubygems_version: 1.8.23
|