Browse Source

feat: use correct model for form

CLDC-1730-stock-owning-validation2
natdeanlewissoftwire 2 years ago
parent
commit
126446cd7d
  1. 14
      app/controllers/organisation_relationships_controller.rb
  2. 14
      app/models/organisation_relationship.rb
  3. 2
      app/views/organisation_relationships/add_housing_provider.html.erb
  4. 2
      app/views/organisation_relationships/add_managing_agent.html.erb

14
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

14
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

2
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)) %>

2
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)) %>

Loading…
Cancel
Save