Browse Source

Refactor case logs

pull/21/head
baarkerlounger 3 years ago
parent
commit
5d787cf85c
  1. 32
      app/controllers/case_logs_controller.rb
  2. 24
      app/controllers/form_controller.rb
  3. 5
      app/javascript/styles/_task-list.scss
  4. 2
      app/views/case_logs/_tasklist.html.erb
  5. 3
      app/views/case_logs/household/tenant_age.html.erb
  6. 3
      app/views/case_logs/household/tenant_code.html.erb
  7. 3
      app/views/case_logs/household/tenant_gender.html.erb
  8. 2
      app/views/case_logs/index.html.erb
  9. 8
      app/views/case_logs/new.html.erb
  10. 4
      config/routes.rb
  11. 15
      spec/controllers/case_logs_controller_spec.rb
  12. 39
      spec/features/case_log_spec.rb

32
app/controllers/case_logs_controller.rb

@ -4,6 +4,11 @@ class CaseLogsController < ApplicationController
@in_progress_case_logs = CaseLog.where(status: 0)
end
def create
@case_log = CaseLog.create!
redirect_to @case_log
end
# We don't have a dedicated non-editable show view
def show
@case_log = CaseLog.find(params[:id])
@ -14,9 +19,26 @@ class CaseLogsController < ApplicationController
@case_log = CaseLog.find(params[:id])
end
# def update
# @case_log = CaseLog.find(params[:id])
# @case_log.update!(tenant_code: params[:case_log][:tenant_code]) if params[:case_log]
# render_next_question(@case_log)
# end
FIRST_QUESTION_FOR_SUBSECTION = {
"Household characteristics" => "case_logs/household/tenant_code",
}.freeze
NEXT_QUESTION = {
"tenant_code" => "case_logs/household/tenant_age",
"tenant_age" => "case_logs/household/tenant_gender",
"tenant_gender" => "case_logs/household/tenant_ethnic_group",
"tenant_ethnic_group" => "case_logs/household/tenant_nationality",
}.freeze
def next_question
@subsection = params[:subsection]
@case_log_id = params[:case_log_id]
result = if @subsection
FIRST_QUESTION_FOR_SUBSECTION[@subsection]
else
@previous_question = params[:previous_question]
NEXT_QUESTION[@previous_question]
end
render result, locals: { case_log_id: @case_log_id }
end
end

24
app/controllers/form_controller.rb

@ -1,24 +0,0 @@
class FormController < ApplicationController
FIRST_QUESTION_FOR_SUBSECTION = {
"Household characteristics" => "case_logs/household/tenant_code",
}
NEXT_QUESTION = {
"tenant_code" => "case_logs/household/tenant_age",
"tenant_age" => "case_logs/household/tenant_gender",
"tenant_gender" => "case_logs/household/tenant_ethnic_group",
"tenant_ethnic_group" => "case_logs/household/tenant_nationality"
}
def next_question
subsection = params[:subsection]
result = if subsection
FIRST_QUESTION_FOR_SUBSECTION[subsection]
else
NEXT_QUESTION[params[:previous_question]]
end
render result
end
end

5
app/javascript/styles/_task-list.scss

@ -80,3 +80,8 @@
}
}
}
turbo-frame {
display: block;
border: 1px solid blue
}

2
app/views/case_logs/_tasklist.html.erb

@ -18,7 +18,7 @@
<ul class="app-task-list__items">
<% subsections.map do |subsection| %>
<li class="app-task-list__item">
<%= link_to subsection, form_path(subsection: subsection), class: "task-name" %>
<%= link_to subsection, form_path(subsection: subsection, case_log_id: @case_log.id), class: "task-name" %>
<strong class="govuk-tag govuk-tag--grey app-task-list__tag">Not started</strong>
</li>
<% end %>

3
app/views/case_logs/household/tenant_age.html.erb

@ -1,11 +1,12 @@
<%= turbo_frame_tag "case_log_form" do %>
<%= form_with action: '/form', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= f.govuk_number_field :tenant_age,
hint: { text: "More detail" },
label: { text: "What is the tenant's age?", size: "l"},
min: 0, max: 150, step: 1, width: 20
%>
<%= f.hidden_field :previous_question, value: :tenant_age %>
<%= f.hidden_field :case_log_id, value: case_log_id %>
<%= f.govuk_submit "Save and continue" %>
<% end %>
<% end %>

3
app/views/case_logs/household/tenant_code.html.erb

@ -1,11 +1,12 @@
<%= turbo_frame_tag "case_log_form" do %>
<%= form_with action: '/form', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= f.govuk_text_field :tenant_code,
hint: { text: "More detail" },
label: { text: "What is the tenant code?", size: "l"},
width: 20
%>
<%= f.hidden_field :previous_question, value: :tenant_code %>
<%= f.hidden_field :case_log_id, value: case_log_id %>
<%= f.govuk_submit "Save and continue" %>
<% end %>
<% end %>

3
app/views/case_logs/household/tenant_gender.html.erb

@ -6,9 +6,10 @@
] %>
<%= turbo_frame_tag "case_log_form" do %>
<%= form_with action: '/form', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= f.govuk_collection_radio_buttons :tenant_gender, genders, :id, :name, legend: { text: "Which of these best describes the tenant's gender identity?", size: "l" } %>
<%= f.hidden_field :previous_question, value: :tenant_gender %>
<%= f.hidden_field :case_log_id, value: case_log_id %>
<%= f.govuk_submit "Save and continue" %>
<% end %>
<% end %>

2
app/views/case_logs/index.html.erb

@ -4,7 +4,7 @@
</div>
<div class="govuk-grid-column-two-thirds">
<a href="tasklist" class="govuk-button">Create new Log</a>
<%= link_to "Create new log", case_logs_path, method: :post, class: "govuk-button" %>
<%= render partial: "log_list", locals: { case_logs: @in_progress_case_logs, title: "Logs you need to complete", date_title: "Last Changed" } %>

8
app/views/case_logs/new.html.erb

@ -1,8 +0,0 @@
<%= form_with model: @case_log, builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= f.govuk_number_field :age,
hint: { text: "More detail" },
label: { text: "What is the tenant's age?", size: "l"},
min: 0, max: 150, step: 1, width: 20
%>
<%= f.govuk_submit %>
<% end %>

4
config/routes.rb

@ -2,8 +2,8 @@ Rails.application.routes.draw do
# For details on the DSL available within this file, see https://guides.rubyonrails.org/routing.html
get "about", to: "about#index"
get "/", to: "test#index"
get "form", to: "form#next_question"
post "form", to: "form#next_question"
get "form", to: "case_logs#next_question"
post "form", to: "case_logs#next_question"
resources :case_logs
end

15
spec/controllers/case_logs_controller_spec.rb

@ -11,10 +11,17 @@ RSpec.describe CaseLogsController, type: :controller do
end
end
describe "GET #new" do
it "returns a success response" do
get :new, params: {}, session: valid_session
expect(response).to be_successful
describe "Post #create" do
it "creates a new case log record" do
expect {
post :create, params: {}, session: valid_session
}.to change(CaseLog, :count).by(1)
end
it 'redirects to that case log' do
post :create, params: {}, session: valid_session
expect(response.status).to eq(302)
end
end
end

39
spec/features/case_log_spec.rb

@ -4,25 +4,30 @@ RSpec.describe "Test Features" do
let(:id) { case_log.id }
let(:status) { case_log.status }
it "displays a tasklist header" do
visit("/case_logs/342351")
expect(page).to have_content("Tasklist for log #{id}")
expect(page).to have_content("This submission is #{status}")
describe "Create new log" do
it "redirects to the task list for the new log" do
visit("/case_logs")
click_link("Create new log")
id = CaseLog.first.id
expect(page).to have_content("Tasklist for log #{id}")
end
end
it "has an edit form" do
visit("/case_logs/new")
expect(page).to have_field("age")
expect(page).to have_button("Continue")
end
describe "Viewing a log" do
it "displays a tasklist header" do
visit("/case_logs/342351")
expect(page).to have_content("Tasklist for log #{id}")
expect(page).to have_content("This submission is #{status}")
end
it "displays the household questions when you click into that section" do
visit("/case_logs/#{id}")
click_link("Household characteristics")
expect(page).to have_field("tenant-code-field")
click_button("Save and continue")
expect(page).to have_field("tenant-age-field")
click_button("Save and continue")
expect(page).to have_field("tenant-gender-0-field")
it "displays the household questions when you click into that section" do
visit("/case_logs/#{id}")
click_link("Household characteristics")
expect(page).to have_field("tenant-code-field")
click_button("Save and continue")
expect(page).to have_field("tenant-age-field")
click_button("Save and continue")
expect(page).to have_field("tenant-gender-0-field")
end
end
end

Loading…
Cancel
Save