diff --git a/app/controllers/schemes_controller.rb b/app/controllers/schemes_controller.rb index 5c2fe355f..2d4acc349 100644 --- a/app/controllers/schemes_controller.rb +++ b/app/controllers/schemes_controller.rb @@ -145,7 +145,6 @@ class SchemesController < ApplicationController if confirm_secondary_page? page redirect_to scheme_secondary_client_group_path(@scheme, check_answers: "true") else - @scheme.update!(secondary_client_group: nil) if @scheme.has_other_client_group == "No" redirect_to scheme_check_answers_path(@scheme) end else diff --git a/app/models/derived_variables/scheme_variables.rb b/app/models/derived_variables/scheme_variables.rb new file mode 100644 index 000000000..ed06ed3c2 --- /dev/null +++ b/app/models/derived_variables/scheme_variables.rb @@ -0,0 +1,7 @@ +module DerivedVariables::SchemeVariables + def set_derived_fields! + if has_other_client_group == "No" + self.secondary_client_group = nil + end + end +end diff --git a/app/models/scheme.rb b/app/models/scheme.rb index fdb5cf394..4d2b5ef8b 100644 --- a/app/models/scheme.rb +++ b/app/models/scheme.rb @@ -1,4 +1,6 @@ class Scheme < ApplicationRecord + include DerivedVariables::SchemeVariables + belongs_to :owning_organisation, class_name: "Organisation" has_many :locations, dependent: :delete_all has_many :lettings_logs, class_name: "LettingsLog", dependent: :delete_all @@ -23,6 +25,8 @@ class Scheme < ApplicationRecord validate :validate_confirmed validate :validate_owning_organisation + before_validation :set_derived_fields! + auto_strip_attributes :service_name SENSITIVE = { @@ -188,7 +192,7 @@ class Scheme < ApplicationRecord end def validate_confirmed - required_attributes = attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units has_other_client_group deactivation_date deactivation_date_type] + required_attributes = attribute_names - %w[id created_at updated_at old_id old_visible_id confirmed end_date sensitive secondary_client_group total_units deactivation_date deactivation_date_type] if confirmed == true required_attributes.any? do |attribute| diff --git a/app/services/imports/scheme_location_import_service.rb b/app/services/imports/scheme_location_import_service.rb index e01cd1106..1530c8143 100644 --- a/app/services/imports/scheme_location_import_service.rb +++ b/app/services/imports/scheme_location_import_service.rb @@ -33,6 +33,7 @@ module Imports support_type: attributes["support_type"], intended_stay: attributes["intended_stay"], primary_client_group: attributes["primary_client_group"], + has_other_client_group: attributes["has_other_client_group"], secondary_client_group: attributes["secondary_client_group"], sensitive: attributes["sensitive"], # These values were set by the scheme import (management groups) @@ -56,6 +57,7 @@ module Imports support_type: attributes["support_type"], intended_stay: attributes["intended_stay"], primary_client_group: attributes["primary_client_group"], + has_other_client_group: attributes["has_other_client_group"], secondary_client_group: attributes["secondary_client_group"], sensitive: attributes["sensitive"], } @@ -83,6 +85,7 @@ module Imports attributes["primary_client_group"] = string_or_nil(xml_doc, "client-group-1") attributes["secondary_client_group"] = string_or_nil(xml_doc, "client-group-2") attributes["secondary_client_group"] = nil if attributes["primary_client_group"] == attributes["secondary_client_group"] + attributes["has_other_client_group"] = attributes["secondary_client_group"].present? ? "Yes" : "No" attributes["sensitive"] = sensitive(xml_doc) attributes["start_date"] = parse_date(xml_doc, "start-date") attributes["end_date"] = parse_date(xml_doc, "end-date")