Skip to content

Commit a396a17

Browse files
ChrisBAshtonpatrickpatrickpatrick
authored andcommitted
Capture same base path error and display to user
Instead of presenting a server error to the user if they try and create an edition that has the base path of another published edition, the error is now presented to the user.
1 parent d8a61e3 commit a396a17

11 files changed

Lines changed: 60 additions & 32 deletions

File tree

app/controllers/admin/editions_controller.rb

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,9 @@ def create
9393
build_edition_dependencies
9494
render :new
9595
end
96+
rescue Whitehall::UnpublishableInstanceError => _e
97+
construct_similar_slug_warning_error
98+
render :new
9699
end
97100

98101
def edit

app/models/call_for_evidence.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ def process_associations_after_save(edition)
4848
schedule_republishing_workers
4949
end
5050

51+
def self.base_path
52+
"/government/calls-for-evidence/"
53+
end
54+
5155
def schedule_republishing_workers
5256
if opening_at.try(:future?)
5357
PublishingApiDocumentRepublishingWorker
@@ -131,10 +135,6 @@ def string_for_slug
131135
title
132136
end
133137

134-
def base_path
135-
"/government/calls-for-evidence/#{slug}"
136-
end
137-
138138
def publishing_api_presenter
139139
PublishingApi::CallForEvidencePresenter
140140
end

app/models/case_study.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,10 @@ class CaseStudy < Edition
1010
include Edition::WorldwideOrganisations
1111
include Edition::LeadImage
1212

13+
def self.base_path
14+
"/government/case-studies/"
15+
end
16+
1317
def rendering_app
1418
Whitehall::RenderingApp::FRONTEND
1519
end
@@ -18,10 +22,6 @@ def translatable?
1822
!non_english_edition?
1923
end
2024

21-
def base_path
22-
"/government/case-studies/#{slug}"
23-
end
24-
2525
def publishing_api_presenter
2626
PublishingApi::CaseStudyPresenter
2727
end

app/models/consultation.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ def process_associations_after_save(edition)
5858
schedule_republishing_workers
5959
end
6060

61+
def self.base_path
62+
"/government/consultations/"
63+
end
64+
6165
def schedule_republishing_workers
6266
if opening_at.try(:future?)
6367
PublishingApiDocumentRepublishingWorker
@@ -145,10 +149,6 @@ def string_for_slug
145149
title
146150
end
147151

148-
def base_path
149-
"/government/consultations/#{slug}"
150-
end
151-
152152
def publishing_api_presenter
153153
PublishingApi::ConsultationPresenter
154154
end

app/models/detailed_guide.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,10 @@ class DetailedGuide < Edition
1111
include Edition::RelatedDocuments
1212
include Edition::TopicalEvents
1313

14+
def self.base_path
15+
"/guidance/"
16+
end
17+
1418
has_many :related_mainstreams, foreign_key: "edition_id", dependent: :destroy
1519

1620
validate :related_mainstream_found, if: :related_mainstream_requested?
@@ -104,10 +108,6 @@ def all_nation_applicability_selected?
104108
newly_created ? false : all_nation_applicability
105109
end
106110

107-
def base_path
108-
"/guidance/#{slug}"
109-
end
110-
111111
def publishing_api_presenter
112112
PublishingApi::DetailedGuidePresenter
113113
end

app/models/document.rb

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -85,12 +85,9 @@ def first_edition_id
8585

8686
def similar_slug_exists?
8787
scope = Document.where(document_type:)
88-
sequence_separator = friendly_id_config.sequence_separator
8988

90-
# slug is a nullable column, so we can't assume that it exists
91-
return false if slug.nil?
92-
93-
slug_without_sequence = slug.split(sequence_separator).first
89+
# return false if slug.nil?
90+
return false if slug_without_sequence.blank?
9491

9592
scope.where(
9693
"slug IN (?) OR slug LIKE ?",
@@ -99,6 +96,17 @@ def similar_slug_exists?
9996
).count > 1
10097
end
10198

99+
def slug_without_sequence
100+
# slug is a nullable column, so we can't assume that it exists
101+
return if slug.nil?
102+
103+
slug.split(sequence_separator).first
104+
end
105+
106+
def sequence_separator
107+
friendly_id_config.sequence_separator
108+
end
109+
102110
def should_generate_new_friendly_id?
103111
sluggable_string.present?
104112
end

app/models/document_collection.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,10 @@ def process_associations_before_save(new_edition)
2222

2323
add_trait ClonesGroupsTrait
2424

25+
def self.base_path
26+
"/government/collections/"
27+
end
28+
2529
def rendering_app
2630
Whitehall::RenderingApp::FRONTEND
2731
end
@@ -34,10 +38,6 @@ def locale_can_be_changed?
3438
true
3539
end
3640

37-
def base_path
38-
"/government/collections/#{slug}"
39-
end
40-
4141
def publishing_api_presenter
4242
PublishingApi::DocumentCollectionPresenter
4343
end

app/models/edition.rb

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,6 +101,10 @@ def self.scheduled_for_publication_as(slug)
101101
document&.scheduled_edition
102102
end
103103

104+
def self.base_path
105+
"/government/generic-editions/"
106+
end
107+
104108
def skip_main_validation?
105109
FROZEN_STATES.include?(state)
106110
end
@@ -396,10 +400,11 @@ def attribute_names
396400
end
397401

398402
def base_path
399-
url_slug = slug || id.to_param
400-
"/government/generic-editions/#{url_slug}"
403+
"#{self.class.base_path}#{url_slug}"
401404
end
402405

406+
delegate :slug_without_sequence, to: :document
407+
403408
def public_path(options = {})
404409
return if base_path.nil?
405410

@@ -442,6 +447,10 @@ def deleted_associated_documents
442447

443448
private
444449

450+
def url_slug
451+
slug || id.to_param
452+
end
453+
445454
def date_for_government
446455
published_edition_date = first_public_at.try(:to_date)
447456
draft_edition_date = updated_at.try(:to_date)

app/models/fatality_notice.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ def process_associations_after_save(new_edition)
2525

2626
add_trait CasualtiesTrait
2727

28+
def self.base_path
29+
"/government/fatalities/"
30+
end
31+
2832
def has_operational_field?
2933
true
3034
end
@@ -33,10 +37,6 @@ def rendering_app
3337
Whitehall::RenderingApp::FRONTEND
3438
end
3539

36-
def base_path
37-
"/government/fatalities/#{slug}"
38-
end
39-
4040
def publishing_api_presenter
4141
PublishingApi::FatalityNoticePresenter
4242
end

app/services/draft_edition_updater.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@ def perform!
88
end
99

1010
def failure_reason
11+
return @failure_reason if @failure_reason.present?
12+
1113
if !edition.pre_publication?
1214
"A #{edition.state} edition may not be updated."
1315
elsif should_check_current_user_will_retain_access? && access_limit_excludes_current_user?

0 commit comments

Comments
 (0)