Browse Source

feat: add eager loading of housing_providers

CLDC-1661-new-page-for-housing-providers
natdeanlewissoftwire 2 years ago
parent
commit
cfd3ef956f
  1. 4
      app/controllers/organisation_relationships_controller.rb
  2. 8
      app/controllers/organisations_controller.rb
  3. 11
      app/helpers/navigation_items_helper.rb
  4. 2
      app/models/organisation.rb

4
app/controllers/organisation_relationships_controller.rb

@ -9,6 +9,10 @@ class OrganisationRelationshipsController < ApplicationController
@managing_agents = organisation.managing_agents
end
def housing_providers
@housing_providers = organisation.housing_providers
end
private
def organisation

8
app/controllers/organisations_controller.rb

@ -136,13 +136,7 @@ class OrganisationsController < ApplicationController
end
def housing_providers
housing_providers =
Organisation.joins(:child_organisation_relationships)
.where(organisation_relationships: {
child_organisation_id: @organisation.id,
relationship_type: OrganisationRelationship.relationship_types[:owning],
})
.order(:name)
housing_providers = @organisation.housing_providers
unpaginated_filtered_housing_providers = filtered_collection(housing_providers, search_term)
respond_to do |format|
format.html do

11
app/helpers/navigation_items_helper.rb

@ -2,7 +2,7 @@ module NavigationItemsHelper
NavigationItem = Struct.new(:text, :href, :current, :classes)
def primary_items(path, current_user)
items = if current_user.support?
if current_user.support?
[
NavigationItem.new("Organisations", organisations_path, organisations_current?(path)),
NavigationItem.new("Users", "/users", users_current?(path)),
@ -24,12 +24,9 @@ module NavigationItemsHelper
FeatureToggle.sales_log_enabled? ? NavigationItem.new("Sales logs", sales_logs_path, sales_logs_current?(path)) : nil,
NavigationItem.new("Users", users_organisation_path(current_user.organisation), subnav_users_path?(path)),
NavigationItem.new("About your organisation", "/organisations/#{current_user.organisation.id}", subnav_details_path?(path)),
NavigationItem.new("Housing providers", housing_providers_organisation_path(current_user.organisation), housing_providers_current?(path)),
NavigationItem.new("Housing providers", housing_providers_organisation_path(current_user.organisation), subnav_housing_providers_path?(path)),
].compact
end
# figure out correct rules
items << managing_agents_item(path)
end
def secondary_items(path, current_organisation_id)
@ -81,10 +78,6 @@ private
path == "/organisations" || path.include?("/organisations/")
end
def housing_providers_current?(path)
path == "/housing-providers"
end
def subnav_housing_providers_path?(path)
path.include?("/organisations") && path.include?("/housing-providers")
end

2
app/models/organisation.rb

@ -15,6 +15,8 @@ class Organisation < ApplicationRecord
has_many :managing_agent_relationships, -> { where(relationship_type: OrganisationRelationship::MANAGING) }, foreign_key: :child_organisation_id, class_name: "OrganisationRelationship"
has_many :managing_agents, through: :managing_agent_relationships, source: :parent_organisation
has_many :housing_provider_relationships, -> { where(relationship_type: OrganisationRelationship::OWNING) }, foreign_key: :child_organisation_id, class_name: "OrganisationRelationship"
has_many :housing_providers, through: :housing_provider_relationships, source: :parent_organisation
scope :search_by_name, ->(name) { where("name ILIKE ?", "%#{name}%") }
scope :search_by, ->(param) { search_by_name(param) }

Loading…
Cancel
Save