Browse Source

feat: refactor create logic

pull/954/head
natdeanlewissoftwire 3 years ago
parent
commit
910810e1be
  1. 37
      app/controllers/organisation_relationships_controller.rb
  2. 13
      app/views/organisation_relationships/housing_providers.html.erb

37
app/controllers/organisation_relationships_controller.rb

@ -30,37 +30,30 @@ class OrganisationRelationshipsController < ApplicationController
end end
def create_housing_provider def create_housing_provider
create(0) child_organisation_id = @organisation.id
end parent_organisation_id = related_organisation_id
def create_managing_agent
create(1)
end
def create(relationship_type)
if related_organisation_id.empty? if related_organisation_id.empty?
@organisation.errors.add :related_organisation_id, "You must choose a housing provider" @organisation.errors.add :related_organisation_id, "You must choose a housing provider"
@organisations = Organisation.where.not(id: @organisation.id).pluck(:id, :name) @organisations = Organisation.where.not(id: child_organisation_id).pluck(:id, :name)
render 'organisation_relationships/add_housing_provider' render 'organisation_relationships/add_housing_provider'
return return
end elsif OrganisationRelationship.exists?(child_organisation_id:, parent_organisation_id:, relationship_type: 0)
case relationship_type
when 0
if OrganisationRelationship.exists?(child_organisation_id: @organisation.id, parent_organisation_id: related_organisation_id, relationship_type: 0)
@organisation.errors.add :related_organisation_id, "You have already added this housing provider" @organisation.errors.add :related_organisation_id, "You have already added this housing provider"
@organisations = Organisation.where.not(id: @organisation.id).pluck(:id, :name) @organisations = Organisation.where.not(id: child_organisation_id).pluck(:id, :name)
render 'organisation_relationships/add_housing_provider' render 'organisation_relationships/add_housing_provider'
else return
@resource = OrganisationRelationship.new(child_organisation_id: @organisation.id, parent_organisation_id: related_organisation_id, relationship_type: 0) end
@resource.save! create(child_organisation_id, parent_organisation_id, 0)
redirect_to housing_providers_organisation_path(related_organisation_id:) redirect_to housing_providers_organisation_path(related_organisation_id:)
end end
when 1
@resource = OrganisationRelationship.new(child_organisation_id: related_organisation_id, parent_organisation_id: @organisation.id, relationship_type: 1) def create_managing_agent
@resource.save! create(related_organisation_id, @organisation.id, 1)
redirect_to managing_agents_organisation_path
end end
def create(child_organisation_id, parent_organisation_id, relationship_type)
@resource = OrganisationRelationship.new(child_organisation_id:, parent_organisation_id:, relationship_type:)
@resource.save!
end end
private private
@ -79,7 +72,7 @@ private
end end
def authenticate_scope! def authenticate_scope!
if current_user.organisation != organisation if current_user.organisation != organisation && !current_user.support?
render_not_found render_not_found
end end
end end

13
app/views/organisation_relationships/housing_providers.html.erb

@ -23,15 +23,22 @@
notification_banner.heading(text: "#{Organisation.find(params["related_organisation_id"]).name} is now one of your housing providers") notification_banner.heading(text: "#{Organisation.find(params["related_organisation_id"]).name} is now one of your housing providers")
end %> end %>
<% end %> <% end %>
<% if @total_count == 0 %>
<p class="govuk-body">This organisation does not currently have any housing providers.</p>
<% else %>
<p class="govuk-body">This organisation can submit logs for its housing providers.</p> <p class="govuk-body">This organisation can submit logs for its housing providers.</p>
<% end %>
<% else %> <% else %>
<%= render partial: "organisations/headings", locals: { main: "Your housing providers", sub: nil } %> <%= render partial: "organisations/headings", locals: { main: "Your housing providers", sub: nil } %>
<% if @total_count == 0 %>
<p class="govuk-body">Your organisation does not currently have any housing providers.</p>
<% else %>
<p class="govuk-body">Your organisation can submit logs for its housing providers.</p> <p class="govuk-body">Your organisation can submit logs for its housing providers.</p>
<% end %>
<% end %> <% end %>
<% if @total_count == 0 %>
<p class="govuk-body">This organisation does not currently have any housing providers.</p>
<% end %>
<% if current_user.support? %> <% if current_user.support? %>
<%= govuk_button_link_to "Add a housing provider", housing_providers_add_organisation_path(organisation_id: @organisation.id), html: { method: :get } %> <%= govuk_button_link_to "Add a housing provider", housing_providers_add_organisation_path(organisation_id: @organisation.id), html: { method: :get } %>
<% elsif current_user.data_coordinator? %> <% elsif current_user.data_coordinator? %>

Loading…
Cancel
Save