Browse Source

Move stock owner updating to controller

CLDC-1324-remove-not-started
Jack S 2 years ago
parent
commit
306b353ea1
  1. 21
      app/controllers/form_controller.rb
  2. 6
      app/models/form/lettings/pages/stock_owner.rb

21
app/controllers/form_controller.rb

@ -4,6 +4,26 @@ class FormController < ApplicationController
before_action :find_resource_by_named_id, except: %i[review] before_action :find_resource_by_named_id, except: %i[review]
before_action :check_collection_period, only: %i[submit_form show_page] before_action :check_collection_period, only: %i[submit_form show_page]
def update_owning_org(log)
return unless current_user
return if current_user.support?
stock_owners = current_user.organisation.stock_owners
if current_user.organisation.holds_own_stock?
return if stock_owners.count >= 1
log.update!(owning_organisation: current_user.organisation)
else
return if stock_owners.count.zero?
return if stock_owners.count > 1
log.update!(owning_organisation: stock_owners.first)
end
false
end
def submit_form def submit_form
if @log if @log
@page = form.get_page(params[@log.model_name.param_key][:page]) @page = form.get_page(params[@log.model_name.param_key][:page])
@ -11,6 +31,7 @@ class FormController < ApplicationController
mandatory_questions_with_no_response = mandatory_questions_with_no_response(responses_for_page) mandatory_questions_with_no_response = mandatory_questions_with_no_response(responses_for_page)
if mandatory_questions_with_no_response.empty? && @log.update(responses_for_page.merge(updated_by: current_user)) if mandatory_questions_with_no_response.empty? && @log.update(responses_for_page.merge(updated_by: current_user))
update_owning_org(@log)
flash[:notice] = "You have successfully updated #{@page.questions.map(&:check_answer_label).first.downcase}" if previous_interruption_screen_page_id.present? flash[:notice] = "You have successfully updated #{@page.questions.map(&:check_answer_label).first.downcase}" if previous_interruption_screen_page_id.present?
redirect_to(successful_redirect_path) redirect_to(successful_redirect_path)
else else

6
app/models/form/lettings/pages/stock_owner.rb

@ -10,7 +10,7 @@ class Form::Lettings::Pages::StockOwner < ::Form::Page
] ]
end end
def routed_to?(log, current_user) def routed_to?(_log, current_user)
return false unless current_user return false unless current_user
return true if current_user.support? return true if current_user.support?
@ -18,13 +18,9 @@ class Form::Lettings::Pages::StockOwner < ::Form::Page
if current_user.organisation.holds_own_stock? if current_user.organisation.holds_own_stock?
return true if stock_owners.count >= 1 return true if stock_owners.count >= 1
log.update!(owning_organisation: current_user.organisation)
else else
return false if stock_owners.count.zero? return false if stock_owners.count.zero?
return true if stock_owners.count > 1 return true if stock_owners.count > 1
log.update!(owning_organisation: stock_owners.first)
end end
false false

Loading…
Cancel
Save