Skip to content

Commit

Permalink
Refactor controller to improve readability
Browse files Browse the repository at this point in the history
  • Loading branch information
syed-ali-tw committed Oct 18, 2024
1 parent d20ad3e commit d3cea9f
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 22 deletions.
17 changes: 12 additions & 5 deletions app/controllers/editions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,17 @@ def confirm_unpublish
def process_unpublish
artefact = @resource.artefact

success = params["redirect_url"].strip.empty? ? UnpublishService.call(artefact, current_user) : UnpublishService.call(artefact, current_user, redirect_url)
success = unpublish_edition(artefact)
if success
notice = "Content unpublished"
notice << " and redirected" if redirect_url.present?
flash[:success] = notice
redirect_to root_path
else
@resource.errors.add(:unpublish, downstream_error_message)
render "secondary_nav_tabs/confirm_unpublish"
render_confirm_page_with_error
end
rescue StandardError
@resource.errors.add(:unpublish, downstream_error_message)
render "secondary_nav_tabs/confirm_unpublish"
render_confirm_page_with_error
end

protected
Expand All @@ -75,6 +73,15 @@ def setup_view_paths

private

def unpublish_edition(artefact)
params["redirect_url"].strip.empty? ? UnpublishService.call(artefact, current_user) : UnpublishService.call(artefact, current_user, redirect_url)
end

def render_confirm_page_with_error
@resource.errors.add(:unpublish, downstream_error_message)
render "secondary_nav_tabs/confirm_unpublish"
end

def downstream_error_message
"Due to a service problem, the edition couldn't be unpublished"
end
Expand Down
33 changes: 16 additions & 17 deletions app/views/editions/secondary_nav_tabs/confirm_unpublish.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,22 @@
<% end %>
<% end %>

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= render "govuk_publishing_components/components/inset_text", {
text: "If you unpublish a page from GOV.UK it cannot be undone.",
} %>
<div class="govuk-grid-column-two-thirds">
<%= render "govuk_publishing_components/components/inset_text", {
text: "If you unpublish a page from GOV.UK it cannot be undone.",
} %>

<%= form_for @edition, url: process_unpublish_edition_path(@edition), method: :post do %>
<%= hidden_field_tag :redirect_url, params[:redirect_url] %>
<p class="govuk-body govuk-!-margin-bottom-7">Are you sure you want to unpublish this document?</p>
<div class="govuk-button-group">
<%= render "govuk_publishing_components/components/button", {
text: "Unpublish document",
destructive: true,
} %>
<%= link_to("Cancel", unpublish_edition_path, class: "govuk-link govuk-link--no-visited-state") %>
</div>
<% end %>
</div>
<%= form_for @edition, url: process_unpublish_edition_path(@edition), method: :post do %>
<%= hidden_field_tag :redirect_url, params[:redirect_url] %>
<p class="govuk-body govuk-!-margin-bottom-7">Are you sure you want to unpublish this document?</p>
<div class="govuk-button-group">
<%= render "govuk_publishing_components/components/button", {
text: "Unpublish document",
destructive: true,
} %>
<%= link_to("Cancel", unpublish_edition_path, class: "govuk-link govuk-link--no-visited-state") %>
</div>
<% end %>
</div>
</div>
</div>

0 comments on commit d3cea9f

Please sign in to comment.