Matthew Phelan 3 years ago
parent
commit
6e9c88701b
  1. 35
      app/controllers/case_logs_controller.rb
  2. 25
      app/controllers/form_controller.rb
  3. 15
      app/javascript/controllers/hello_controller.js
  4. 17
      app/javascript/controllers/index.js
  5. 2
      app/javascript/packs/application.js
  6. 5
      app/javascript/styles/_task-list.scss
  7. 2
      app/views/case_logs/_tasklist.html.erb
  8. 7
      app/views/case_logs/household_situation/previous_housing_situation.erb
  9. 2
      app/views/case_logs/index.html.erb
  10. 8
      app/views/case_logs/new.html.erb
  11. 3
      app/views/form/questions/household/tenant_age.html.erb
  12. 3
      app/views/form/questions/household/tenant_code.html.erb
  13. 3
      app/views/form/questions/household/tenant_gender.html.erb
  14. 4
      config/routes.rb
  15. 2
      package.json
  16. 14
      spec/controllers/case_logs_controller_spec.rb
  17. 17
      spec/features/case_log_spec.rb
  18. 44
      yarn.lock

35
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,29 @@ 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" => "form/questions/household/tenant_code",
"Household situation" => "case_logs/household_situation/previous_housing_situation"
}.freeze
NEXT_QUESTION = {
"tenant_code" => "form/questions/household/tenant_age",
"tenant_age" => "form/questions/household/tenant_gender",
"tenant_gender" => "form/questions/household/tenant_ethnic_group",
"tenant_ethnic_group" => "form/questions/household/tenant_nationality",
}.freeze
def next_question
subsection = params[:subsection]
@case_log = CaseLog.find(params[:case_log_id])
result = if subsection
FIRST_QUESTION_FOR_SUBSECTION[subsection]
else
previous_question = params[:previous_question]
answer = params[previous_question]
@case_log.update!(previous_question => answer)
NEXT_QUESTION[previous_question]
end
render result, locals: { case_log_id: @case_log.id }
end
end

25
app/controllers/form_controller.rb

@ -1,25 +0,0 @@
class FormController < ApplicationController
FIRST_QUESTION_FOR_SUBSECTION = {
"Household characteristics" => "case_logs/household/tenant_code",
"Household situation" => "case_logs/household_situation/previous_housing_situation"
}
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

15
app/javascript/controllers/hello_controller.js

@ -1,7 +1,18 @@
import { Controller } from "@hotwired/stimulus"
// Visit The Stimulus Handbook for more details
// https://stimulusjs.org/handbook/introduction
//
// This example controller works with specially annotated HTML like:
//
// <div data-controller="hello">
// <h1 data-target="hello.output"></h1>
// </div>
import { Controller } from "stimulus"
export default class extends Controller {
static targets = [ "output" ]
connect() {
this.element.textContent = "Hello World!"
this.outputTarget.textContent = 'Hello, Stimulus!'
}
}

17
app/javascript/controllers/index.js

@ -1,14 +1,9 @@
import { Application } from "@hotwired/stimulus"
// Load all the controllers within this directory and all subdirectories.
// Controller files must be named *_controller.js.
const application = Application.start()
// Configure Stimulus development experience
application.warnings = true
application.debug = false
window.Stimulus = application
import { Application } from "stimulus"
import { definitionsFromContext } from "stimulus/webpack-helpers"
// Import and register all your controllers within this directory and all subdirectories
// Controller files must be named *_controller.js or *_controller.ts
import { definitionsFromContext } from "@hotwired/stimulus"
const context = require.context("controllers", true, /_controller\.(js|ts)$/)
const application = Application.start()
const context = require.context("controllers", true, /_controller\.js$/)
application.load(definitionsFromContext(context))

2
app/javascript/packs/application.js

@ -15,3 +15,5 @@ import "@hotwired/turbo-rails"
Rails.start()
ActiveStorage.start()
initAll()
import "controllers"

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 %>

7
app/views/case_logs/household_situation/previous_housing_situation.erb

@ -1,4 +1,4 @@
<% genders = [
<% situations = [
OpenStruct.new(id: 0, name: "Owner occupation (private)"),
OpenStruct.new(id: 1, name: "Owner occupation (low cost home ownership)"),
OpenStruct.new(id: 2, name: "Private sector tenancy"),
@ -25,9 +25,10 @@
] %>
<%= turbo_frame_tag "case_log_form" do %>
<%= form_with action: '/form', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= f.govuk_collection_radio_buttons :previous_housing_situation, genders, :id, :name, legend: { text: "What was the tenant’s housing situation immediately before this letting?", size: "l" } %>
<%= form_with action: '/case_logs', method: "next_question", builder: GOVUKDesignSystemFormBuilder::FormBuilder do |f| %>
<%= f.govuk_collection_radio_buttons :previous_housing_situation, situations, :id, :name, legend: { text: "What was the tenant’s housing situation immediately before this letting?", size: "l" } %>
<%= f.hidden_field :previous_question, value: :previous_housing_situation %>
<%= 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 %>

3
app/views/case_logs/household/tenant_age.html.erb → app/views/form/questions/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 → app/views/form/questions/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 → app/views/form/questions/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 %>

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

2
package.json

@ -10,6 +10,8 @@
"@rails/ujs": "^6.0.0",
"@rails/webpacker": "5.4.0",
"govuk-frontend": "^3.13.0",
"stimulus": "^2.0.0",
"stimulus-use": "^0.26.0",
"webpack": "^4.46.0",
"webpack-cli": "^3.3.12"
},

14
spec/controllers/case_logs_controller_spec.rb

@ -11,10 +11,16 @@ 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

17
spec/features/case_log_spec.rb

@ -4,18 +4,22 @@ RSpec.describe "Test Features" do
let(:id) { case_log.id }
let(:status) { case_log.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
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 "has an edit form" do
visit("/case_logs/new")
expect(page).to have_field("age")
expect(page).to have_button("Continue")
end
it "displays the household questions when you click into that section" do
visit("/case_logs/#{id}")
click_link("Household characteristics")
@ -26,3 +30,4 @@ RSpec.describe "Test Features" do
expect(page).to have_field("tenant-gender-0-field")
end
end
end

44
yarn.lock

@ -972,6 +972,30 @@
webpack-cli "^3.3.12"
webpack-sources "^1.4.3"
"@stimulus/core@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@stimulus/core/-/core-2.0.0.tgz#140c85318d6a8a8210c0faf182223b8459348877"
integrity sha512-ff70GafKtzc8zQ1/cG+UvL06GcifPWovf2wBEdjLMh9xO2GOYURO3y2RYgzIGYUIBefQwyfX2CLfJdZFJrEPTw==
dependencies:
"@stimulus/mutation-observers" "^2.0.0"
"@stimulus/multimap@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@stimulus/multimap/-/multimap-2.0.0.tgz#420cfa096ed6538df4a91dbd2b2842c1779952b2"
integrity sha512-pMBCewkZCFVB3e5mEMoyO9+9aKzHDITmf3OnPun51YWxlcPdHcwbjqm1ylK63fsoduIE+RowBpFwFqd3poEz4w==
"@stimulus/mutation-observers@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@stimulus/mutation-observers/-/mutation-observers-2.0.0.tgz#3dbe37453bda47a6c795a90204ee8d77a799fb87"
integrity sha512-kx4VAJdPhIGBQKGIoUDC2tupEKorG3A+ckc2b1UiwInKTMAC1axOHU8ebcwhaJIxRqIrs8//4SJo9YAAOx6FEg==
dependencies:
"@stimulus/multimap" "^2.0.0"
"@stimulus/webpack-helpers@^2.0.0":
version "2.0.0"
resolved "https://registry.yarnpkg.com/@stimulus/webpack-helpers/-/webpack-helpers-2.0.0.tgz#54296d2a2dffd4f962d2e802d99a3fdd84b8845f"
integrity sha512-D6tJWsAC024MwGEIKlUVYU8Ln87mlrmiwHvYAjipg+s8H4eLxUMQ3PZkWyPevfipH+oR3leuHsjYsK1gN5ViQA==
"@types/glob@^7.1.1":
version "7.1.4"
resolved "https://registry.npmjs.org/@types/glob/-/glob-7.1.4.tgz"
@ -3219,6 +3243,11 @@ homedir-polyfill@^1.0.1:
dependencies:
parse-passwd "^1.0.0"
hotkeys-js@>=3:
version "3.8.7"
resolved "https://registry.yarnpkg.com/hotkeys-js/-/hotkeys-js-3.8.7.tgz#c16cab978b53d7242f860ca3932e976b92399981"
integrity sha512-ckAx3EkUr5XjDwjEHDorHxRO2Kb7z6Z2Sxul4MbBkN8Nho7XDslQsgMJT+CiJ5Z4TgRxxvKHEpuLE3imzqy4Lg==
hpack.js@^2.1.6:
version "2.1.6"
resolved "https://registry.npmjs.org/hpack.js/-/hpack.js-2.1.6.tgz"
@ -6121,6 +6150,21 @@ static-extend@^0.1.1:
resolved "https://registry.npmjs.org/statuses/-/statuses-1.5.0.tgz"
integrity sha1-Fhx9rBd2Wf2YEfQ3cfqZOBR4Yow=
stimulus-use@^0.26.0:
version "0.26.0"
resolved "https://registry.yarnpkg.com/stimulus-use/-/stimulus-use-0.26.0.tgz#eb3f874bf2ec1ff213589cb5fbe2d314fe5d2741"
integrity sha512-p9uqV/STRR3QJb9r1ijPiPOmF+vQtcvs8/9D4eDF2OdWOafr0prG/Cd9lZNzamGrejhOqh90mOgrK+RTBP9HLA==
dependencies:
hotkeys-js ">=3"
stimulus@^2.0.0:
version "2.0.0"
resolved "https://registry.yarnpkg.com/stimulus/-/stimulus-2.0.0.tgz#713c8b91a72ef90914b90955f0e705f004403047"
integrity sha512-xipy7BS5TVpg4fX6S8LhrYZp7cmHGjmk09WSAiVx1gF5S5g43IWsuetfUhIk8HfHUG+4MQ9nY0FQz4dRFLs/8w==
dependencies:
"@stimulus/core" "^2.0.0"
"@stimulus/webpack-helpers" "^2.0.0"
stream-browserify@^2.0.1:
version "2.0.2"
resolved "https://registry.npmjs.org/stream-browserify/-/stream-browserify-2.0.2.tgz"

Loading…
Cancel
Save