From 645fd60f197e50fa83c2282cb8a32c8768df2746 Mon Sep 17 00:00:00 2001 From: "Ben A. Morgan" Date: Tue, 21 Mar 2017 14:33:41 -0400 Subject: [PATCH] improve performance for n+1s --- .../administrate/application_controller.rb | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/app/controllers/administrate/application_controller.rb b/app/controllers/administrate/application_controller.rb index f1992704cd..849c1b70e3 100644 --- a/app/controllers/administrate/application_controller.rb +++ b/app/controllers/administrate/application_controller.rb @@ -5,6 +5,7 @@ class ApplicationController < ActionController::Base def index search_term = params[:search].to_s.strip resources = Administrate::Search.new(resource_resolver, search_term).run + resources = resources.includes(*resource_includes) if resource_includes.any? resources = order.apply(resources) resources = resources.page(params[:page]).per(records_per_page) page = Administrate::Page::Collection.new(dashboard, order: order) @@ -73,11 +74,7 @@ def destroy helper_method :nav_link_state def nav_link_state(resource) - if resource_name.to_s.pluralize == resource.to_s - :active - else - :inactive - end + resource_name.to_s.pluralize == resource.to_s ? :active : :inactive end helper_method :valid_action? @@ -111,6 +108,18 @@ def find_resource(param) resource_class.find(param) end + def resource_includes + association_classes = [ + Administrate::Field::HasMany, Administrate::Field::HasOne, + Administrate::Field::BelongsTo + ] + + dashboard.class::ATTRIBUTE_TYPES.map do |key, value| + key if association_classes.include?(value) || + association_classes.include?(value.try :deferred_class) + end.compact + end + def resource_params params.require(resource_name).permit(dashboard.permitted_attributes) end