5 changed files with 19 additions and 33 deletions
@ -1,26 +1,17 @@
|
||||
class OrganisationRelationship < ApplicationRecord |
||||
belongs_to :child_organisation, class_name: "Organisation" |
||||
belongs_to :parent_organisation, class_name: "Organisation" |
||||
validate :validate_housing_provider_relationship, on: :housing_provider |
||||
validate :validate_managing_agent_relationship, on: :managing_agent |
||||
validates :parent_organisation_id, presence: { message: "You must choose a housing provider" }, on: :housing_provider |
||||
validates :child_organisation_id, presence: { message: "You must choose a managing agent" }, on: :managing_agent |
||||
validates :parent_organisation_id, uniqueness: { scope: :child_organisation_id, message: "You have already added this housing provider" }, on: :housing_provider |
||||
validates :child_organisation_id, uniqueness: { scope: :parent_organisation_id, message: "You have already added this managing agent" }, on: :managing_agent |
||||
validate :validate_housing_provider_owns_stock, on: :housing_provider |
||||
|
||||
private |
||||
|
||||
def validate_housing_provider_relationship |
||||
if parent_organisation_id.blank? |
||||
errors.add :related_organisation_id, "You must choose a housing provider" |
||||
elsif OrganisationRelationship.exists?(child_organisation:, parent_organisation:) |
||||
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? |
||||
errors.add :related_organisation_id, "You must choose a managing agent" |
||||
elsif OrganisationRelationship.exists?(child_organisation:, parent_organisation:) |
||||
errors.add :related_organisation_id, "You have already added this managing agent" |
||||
def validate_housing_provider_owns_stock |
||||
if parent_organisation_id.present? && !parent_organisation.holds_own_stock |
||||
errors.add :parent_organisation_id, I18n.t("validations.scheme.owning_organisation.does_not_own_stock") |
||||
end |
||||
end |
||||
end |
||||
|
||||
@ -1,3 +1,3 @@
|
||||
<% answers = question.answer_options.map { |key, value| OpenStruct.new(id: key, name: value) } %> |
||||
<%= f.govuk_collection_select :related_organisation_id, answers, :id, :name, label: { hidden: true }, "data-controller": "accessible-autocomplete" do %> |
||||
<%= f.govuk_collection_select field, answers, :id, :name, label: { hidden: true }, "data-controller": "accessible-autocomplete" do %> |
||||
<% end %> |
||||
|
||||
Loading…
Reference in new issue