From 126446cd7d7977304d40e6605911b6bd0df279e1 Mon Sep 17 00:00:00 2001 From: natdeanlewissoftwire Date: Mon, 12 Dec 2022 11:53:57 +0000 Subject: [PATCH] feat: use correct model for form --- .../organisation_relationships_controller.rb | 14 +++++++------- app/models/organisation_relationship.rb | 14 ++++++-------- .../add_housing_provider.html.erb | 2 +- .../add_managing_agent.html.erb | 2 +- 4 files changed, 15 insertions(+), 17 deletions(-) diff --git a/app/controllers/organisation_relationships_controller.rb b/app/controllers/organisation_relationships_controller.rb index e903f676a..29c572f62 100644 --- a/app/controllers/organisation_relationships_controller.rb +++ b/app/controllers/organisation_relationships_controller.rb @@ -39,11 +39,11 @@ class OrganisationRelationshipsController < ApplicationController def create_housing_provider child_organisation = @organisation - if params[:organisation][:related_organisation_id].present? + if params[:organisation_relationship].present? && params[:organisation_relationship][:related_organisation_id].present? parent_organisation = related_organisation end - @resource = OrganisationRelationship.new(child_organisation:, parent_organisation:) - if @resource.save(context: :housing_provider) + @organisation_relationship = OrganisationRelationship.new(child_organisation:, parent_organisation:) + if @organisation_relationship.save(context: :housing_provider) flash[:notice] = "#{related_organisation.name} is now one of #{current_user.data_coordinator? ? 'your' : "this organisation's"} housing providers" redirect_to housing_providers_organisation_path else @@ -54,11 +54,11 @@ class OrganisationRelationshipsController < ApplicationController def create_managing_agent parent_organisation = @organisation - if params[:organisation][:related_organisation_id].present? + if params[:organisation_relationship].present? && params[:organisation_relationship][:related_organisation_id].present? child_organisation = related_organisation end - @resource = OrganisationRelationship.new(child_organisation:, parent_organisation:) - if @resource.save(context: :managing_agent) + @organisation_relationship = OrganisationRelationship.new(child_organisation:, parent_organisation:) + if @organisation_relationship.save(context: :managing_agent) flash[:notice] = "#{related_organisation.name} is now one of #{current_user.data_coordinator? ? 'your' : "this organisation's"} managing agents" redirect_to managing_agents_organisation_path else @@ -107,7 +107,7 @@ private end def related_organisation - @related_organisation ||= Organisation.find(params[:organisation][:related_organisation_id]) + @related_organisation ||= Organisation.find(params[:organisation_relationship][:related_organisation_id]) end def target_organisation diff --git a/app/models/organisation_relationship.rb b/app/models/organisation_relationship.rb index 687277f92..72f74cbb9 100644 --- a/app/models/organisation_relationship.rb +++ b/app/models/organisation_relationship.rb @@ -1,7 +1,6 @@ class OrganisationRelationship < ApplicationRecord belongs_to :child_organisation, class_name: "Organisation" belongs_to :parent_organisation, class_name: "Organisation" - validates :parent_organisation_id, uniqueness: { scope: [:child_organisation_id] } validate :validate_housing_provider_relationship, on: :housing_provider validate :validate_managing_agent_relationship, on: :managing_agent @@ -9,20 +8,19 @@ private def validate_housing_provider_relationship if parent_organisation_id.blank? - child_organisation.errors.add :related_organisation_id, "You must choose a housing provider" + errors.add :related_organisation_id, "You must choose a housing provider" elsif OrganisationRelationship.exists?(child_organisation:, parent_organisation:) - child_organisation.errors.add :related_organisation_id, "You have already added this housing provider" - elsif !parent_organisation.holds_own_stock - errors.add :parent_organisation_id - child_organisation.errors.add :related_organisation_id, I18n.t("validations.scheme.owning_organisation.does_not_own_stock") + errors.add :related_organisation_id, "You have already added this housing provider" + elsif parent_organisation_id.present? && !parent_organisation.holds_own_stock + errors.add :related_organisation_id, I18n.t("validations.scheme.owning_organisation.does_not_own_stock") end end def validate_managing_agent_relationship if child_organisation_id.blank? - parent_organisation.errors.add :related_organisation_id, "You must choose a managing agent" + errors.add :related_organisation_id, "You must choose a managing agent" elsif OrganisationRelationship.exists?(child_organisation:, parent_organisation:) - parent_organisation.errors.add :related_organisation_id, "You have already added this managing agent" + errors.add :related_organisation_id, "You have already added this managing agent" end end end diff --git a/app/views/organisation_relationships/add_housing_provider.html.erb b/app/views/organisation_relationships/add_housing_provider.html.erb index 3cce90db1..4171e89af 100644 --- a/app/views/organisation_relationships/add_housing_provider.html.erb +++ b/app/views/organisation_relationships/add_housing_provider.html.erb @@ -1,4 +1,4 @@ -<%= form_with model: @organisation, url: housing_providers_organisation_path, method: "post", local: true do |f| %> +<%= form_with model: @organisation_relationship, url: housing_providers_organisation_path, method: "post", local: true do |f| %> <% if current_user.support? %> <%= render partial: "organisations/headings", locals: { main: @organisation.name, sub: nil } %> <%= render SubNavigationComponent.new(items: secondary_items(request.path, @organisation.id)) %> diff --git a/app/views/organisation_relationships/add_managing_agent.html.erb b/app/views/organisation_relationships/add_managing_agent.html.erb index bf5c380d5..5bf3ac354 100644 --- a/app/views/organisation_relationships/add_managing_agent.html.erb +++ b/app/views/organisation_relationships/add_managing_agent.html.erb @@ -1,4 +1,4 @@ -<%= form_with model: @organisation, url: managing_agents_organisation_path, method: "post", local: true do |f| %> +<%= form_with model: @organisation_relationship, url: managing_agents_organisation_path, method: "post", local: true do |f| %> <% if current_user.support? %> <%= render partial: "organisations/headings", locals: { main: @organisation.name, sub: nil } %> <%= render SubNavigationComponent.new(items: secondary_items(request.path, @organisation.id)) %>