Skip to content

Commit

Permalink
Merge pull request #115 from alphagov/ensure-user-chooses-a-format
Browse files Browse the repository at this point in the history
Ensure user chooses supertype / document type
  • Loading branch information
benthorner committed Aug 10, 2018
2 parents d706527 + e103031 commit efd88fb
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 7 deletions.
20 changes: 16 additions & 4 deletions app/controllers/new_document_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,29 @@ def choose_supertype
end

def choose_document_type
supertype_schema = SupertypeSchema.find(params[:supertype])
unless params[:supertype]
redirect_to new_document_path, alert: "Please choose a supertype"
return
end

if supertype_schema.managed_elsewhere
redirect_to supertype_schema.managed_elsewhere_url
@supertype_schema = SupertypeSchema.find(params[:supertype])

if @supertype_schema.managed_elsewhere
redirect_to @supertype_schema.managed_elsewhere_url
return
end

@document_types = supertype_schema.document_types
@document_types = @supertype_schema.document_types
end

def create
unless params[:document_type]
redirect_to choose_document_type_path(supertype: params[:supertype]),
alert: "Please choose a document type"

return
end

document_type_schema = DocumentTypeSchema.find(params[:document_type])

if document_type_schema.managed_elsewhere
Expand Down
1 change: 1 addition & 0 deletions app/views/new_document/choose_document_type.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
}
} %>
<%= hidden_field(nil, :supertype, value: @supertype_schema.id) %>
<%= render "govuk_publishing_components/components/button", text: "Continue", margin_bottom: true %>
<% end %>
</div>
Expand Down
5 changes: 3 additions & 2 deletions app/views/new_document/choose_supertype.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

<div class="govuk-grid-row">
<div class="govuk-grid-column-two-thirds">
<%= form_tag do %>
<%= form_tag(choose_document_type_path, method: :get, enforce_utf8: false) do %>
<%= render "govuk_publishing_components/components/radio", {
name: "supertype",
items: @supertypes.map do |supertype|
Expand All @@ -15,7 +15,8 @@
}
end
} %>
<%= render "govuk_publishing_components/components/button", text: "Continue", margin_bottom: true %>
<% end %>
</div>
</div>
</div>
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

get '/documents/publishing-guidance' => 'new_document#guidance', as: :guidance
get '/documents/new' => 'new_document#choose_supertype', as: :new_document
post '/documents/new' => 'new_document#choose_document_type'
get '/documents/choose-document-type' => 'new_document#choose_document_type', as: :choose_document_type
post '/documents/create' => 'new_document#create', as: :create_document

get '/documents/:id/publish' => 'publish_document#confirmation', as: :publish_document
Expand Down
36 changes: 36 additions & 0 deletions spec/features/choose_a_format_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
# frozen_string_literal: true

require "spec_helper"

RSpec.feature "Choosing a format" do
scenario "User forgets to choose a format" do
when_i_dont_choose_a_supertype
then_i_see_a_supertype_error
when_i_choose_a_supertype
and_i_dont_choose_a_document_type
then_i_see_a_document_type_error
end

def when_i_dont_choose_a_supertype
visit "/"
click_on "New document"
click_on "Continue"
end

def then_i_see_a_supertype_error
expect(page).to have_content "Please choose a supertype"
end

def when_i_choose_a_supertype
choose SupertypeSchema.all.reject(&:managed_elsewhere).map(&:label).sample
click_on "Continue"
end

def and_i_dont_choose_a_document_type
click_on "Continue"
end

def then_i_see_a_document_type_error
expect(page).to have_content "Please choose a document type"
end
end

0 comments on commit efd88fb

Please sign in to comment.