Browse Source

Merge branch 'main' into organisation_page

pull/114/head
baarkerlounger 4 years ago
parent
commit
157f9a99d4
  1. 3
      app/controllers/case_logs_controller.rb
  2. 18
      app/models/case_log.rb
  3. 9
      app/views/layouts/application.html.erb
  4. 26
      spec/features/user_spec.rb
  5. 17
      spec/requests/case_log_controller_spec.rb

3
app/controllers/case_logs_controller.rb

@ -57,8 +57,7 @@ class CaseLogsController < ApplicationController
def submit_form def submit_form
form = FormHandler.instance.get_form("2021_2022") form = FormHandler.instance.get_form("2021_2022")
@case_log = CaseLog.find(params[:id]) @case_log = CaseLog.find(params[:id])
@case_log.page_id = params[:case_log][:page] page = form.get_page(params[:case_log][:page])
page = form.get_page(@case_log.page_id)
responses_for_page = responses_for_page(page) responses_for_page = responses_for_page(page)
if @case_log.update(responses_for_page) && @case_log.has_no_unresolved_soft_errors? if @case_log.update(responses_for_page) && @case_log.has_no_unresolved_soft_errors?
redirect_path = form.next_page_redirect_path(page, @case_log) redirect_path = form.next_page_redirect_path(page, @case_log)

18
app/models/case_log.rb

@ -1,6 +1,6 @@
class CaseLogValidator < ActiveModel::Validator class CaseLogValidator < ActiveModel::Validator
# Validations methods need to be called 'validate_<page_name>' to run on model save # Validations methods need to be called 'validate_' to run on model save
# or 'validate_' to run on submit as well # or form page submission
include HouseholdValidations include HouseholdValidations
include PropertyValidations include PropertyValidations
include FinancialValidations include FinancialValidations
@ -8,16 +8,8 @@ class CaseLogValidator < ActiveModel::Validator
include DateValidations include DateValidations
def validate(record) def validate(record)
# If we've come from the form UI we only want to validate the specific fields validation_methods = public_methods.select { |method| method.starts_with?("validate_") }
# that have just been submitted. If we're submitting a log via API or Bulk Upload validation_methods.each { |meth| public_send(meth, record) }
# we want to validate all data fields.
page_to_validate = record.page_id
if page_to_validate
public_send("validate_#{page_to_validate}", record) if respond_to?("validate_#{page_to_validate}")
else
validation_methods = public_methods.select { |method| method.starts_with?("validate_") }
validation_methods.each { |meth| public_send(meth, record) }
end
end end
private private
@ -49,8 +41,6 @@ class CaseLog < ApplicationRecord
scope :for_organisation, ->(org) { where(owning_organisation: org).or(where(managing_organisation: org)) } scope :for_organisation, ->(org) { where(owning_organisation: org).or(where(managing_organisation: org)) }
attr_accessor :page_id
enum status: { "not_started" => 0, "in_progress" => 1, "completed" => 2 } enum status: { "not_started" => 0, "in_progress" => 1, "completed" => 2 }
enum ethnic: DbEnums.ethnic enum ethnic: DbEnums.ethnic

9
app/views/layouts/application.html.erb

@ -38,7 +38,14 @@
logotype: 'GOV.UK', logotype: 'GOV.UK',
service_name: 'Share Lettings and Sales for Social Housing in England Data with DLUHC', service_name: 'Share Lettings and Sales for Social Housing in England Data with DLUHC',
service_url: '/' service_url: '/'
) ) do |component|
if current_user.nil?
component.navigation_item(text: 'Case logs', href: '/case_logs')
elsif
component.navigation_item(text: 'Your account', href: '/users/account')
component.navigation_item(text: 'Sign out', href: destroy_user_session_path, options: {:method => :delete})
end
end
%> %>
</header> </header>
<aside class="govuk-width-container"> <aside class="govuk-width-container">

26
spec/features/user_spec.rb

@ -58,8 +58,23 @@ RSpec.describe "User Features" do
end end
end end
context "If a not logged in user tries to access pages that need permissions" do context "If user not logged in" do
it "redirects to log in page" do it "'Your account' link does not display" do
visit("/case_logs")
expect(page).to have_no_link("Your account")
end
it "Can navigate and sign in page with sign in button" do
visit("/")
expect(page).to have_link("Case logs")
click_link("Case logs")
fill_in("user_email", with: user.email)
fill_in("user_password", with: "pAssword1")
click_button("Sign in")
expect(page).to have_current_path("/case_logs")
end
it "tries to access account page, redirects to log in page" do
visit("/users/account") visit("/users/account")
expect(page).to have_content("Sign in to your account to submit CORE data") expect(page).to have_content("Sign in to your account to submit CORE data")
end end
@ -73,6 +88,13 @@ RSpec.describe "User Features" do
click_button("Sign in") click_button("Sign in")
end end
it "shows 'Your account' link in navigation if logged in and redirect to correct page" do
visit("/case_logs")
expect(page).to have_link("Your account")
click_link("Your account")
expect(page).to have_current_path("/users/account")
end
it "main page is present and accessible" do it "main page is present and accessible" do
visit("/users/account") visit("/users/account")
expect(page).to have_content("Your account") expect(page).to have_content("Your account")

17
spec/requests/case_log_controller_spec.rb

@ -359,6 +359,23 @@ RSpec.describe CaseLogsController, type: :request do
it "re-renders the same page with errors if validation fails" do it "re-renders the same page with errors if validation fails" do
expect(response).to have_http_status(:redirect) expect(response).to have_http_status(:redirect)
end end
let(:params) do
{
id: case_log.id,
case_log: {
page: page_id,
age1: answer,
age2: 2000
},
}
end
it "only updates answers that apply to the page being submitted" do
case_log.reload
expect(case_log.age1).to eq(answer)
expect(case_log.age2).to be nil
end
end end
end end
end end

Loading…
Cancel
Save