From 4565e4382ef1b5939c8067137d14cf599c08f390 Mon Sep 17 00:00:00 2001 From: Sam Seed Date: Fri, 20 Jan 2023 10:13:28 +0000 Subject: [PATCH] wip --- app/controllers/form_controller.rb | 4 ++-- app/models/form.rb | 2 +- .../lettings/questions/managing_organisation.rb | 16 ++++------------ app/models/form/question.rb | 2 +- app/models/log.rb | 4 ++-- 5 files changed, 10 insertions(+), 18 deletions(-) diff --git a/app/controllers/form_controller.rb b/app/controllers/form_controller.rb index b0468c875..faeb281b2 100644 --- a/app/controllers/form_controller.rb +++ b/app/controllers/form_controller.rb @@ -96,7 +96,7 @@ private next unless question_params if %w[checkbox validation_override].include?(question.type) - question.answer_options.keys.reject { |x| x.match(/divider/) }.each do |option| + question.answer_options(@log, current_user).keys.reject { |x| x.match(/divider/) }.each do |option| result[option] = question_params.include?(option) ? 1 : 0 end else @@ -167,7 +167,7 @@ private def question_missing_response?(responses_for_page, question) if %w[checkbox validation_override].include?(question.type) - answered = question.answer_options.keys.reject { |x| x.match(/divider/) }.map do |option| + answered = question.answer_options(@log, current_user).keys.reject { |x| x.match(/divider/) }.map do |option| session["fields"][option] = @log[option] = params[@log.model_name.param_key][question.id].include?(option) ? 1 : 0 params[@log.model_name.param_key][question.id].exclude?(option) end diff --git a/app/models/form.rb b/app/models/form.rb index 5f6544086..f77f9e931 100644 --- a/app/models/form.rb +++ b/app/models/form.rb @@ -159,7 +159,7 @@ class Form questions.reject { |q| q.page.routed_to?(log, current_user) || q.derived? || callback_questions.include?(q.id) } || [] end - def reset_not_routed_questions(log) + def reset_not_routed_questions(log, current_user = nil) enabled_questions = enabled_page_questions(log) enabled_question_ids = enabled_questions.map(&:id) diff --git a/app/models/form/lettings/questions/managing_organisation.rb b/app/models/form/lettings/questions/managing_organisation.rb index e2e3b3e3d..dabaef364 100644 --- a/app/models/form/lettings/questions/managing_organisation.rb +++ b/app/models/form/lettings/questions/managing_organisation.rb @@ -1,5 +1,4 @@ class Form::Lettings::Questions::ManagingOrganisation < ::Form::Question - attr_accessor :current_user, :log def initialize(id, hsh, page) super @@ -10,7 +9,7 @@ class Form::Lettings::Questions::ManagingOrganisation < ::Form::Question @answer_options = answer_options end - def answer_options + def answer_options(log = nil, current_user = nil) opts = { "" => "Select an option" } return opts unless ActiveRecord::Base.connected? @@ -33,19 +32,13 @@ class Form::Lettings::Questions::ManagingOrganisation < ::Form::Question end def displayed_answer_options(log, user) - @current_user = user - @log = log - - answer_options + answer_options(log, user) end def label_from_value(value, log = nil, user = nil) - @log = log - @current_user = user - return unless value - answer_options[value] + answer_options(log, user)[value] end def derived? @@ -53,8 +46,7 @@ class Form::Lettings::Questions::ManagingOrganisation < ::Form::Question end def hidden_in_check_answers?(log, user = nil) - @current_user = user - @current_user.nil? || !@page.routed_to?(log, user) + user.nil? || !@page.routed_to?(log, user) end def enabled diff --git a/app/models/form/question.rb b/app/models/form/question.rb index cbfc6aae2..ef66dffab 100644 --- a/app/models/form/question.rb +++ b/app/models/form/question.rb @@ -107,7 +107,7 @@ class Form::Question end def displayed_answer_options(log, _current_user = nil) - answer_options.select do |_key, val| + answer_options().select do |_key, val| !val.is_a?(Hash) || !val["depends_on"] || form.depends_on_met(val["depends_on"], log) end end diff --git a/app/models/log.rb b/app/models/log.rb index 0d0df6451..697977001 100644 --- a/app/models/log.rb +++ b/app/models/log.rb @@ -73,10 +73,10 @@ private subsection_statuses.all? { |status| not_started_statuses.include?(status) } end - def reset_invalidated_dependent_fields! + def reset_invalidated_dependent_fields!(user = nil) return unless form - form.reset_not_routed_questions(self) + form.reset_not_routed_questions(self, user) reset_created_by! end