Skip to content

Commit

Permalink
improve performance for n+1s
Browse files Browse the repository at this point in the history
  • Loading branch information
BenMorganIO committed Apr 29, 2017
1 parent 3d98118 commit 23eda32
Showing 1 changed file with 14 additions and 5 deletions.
19 changes: 14 additions & 5 deletions app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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?
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 23eda32

Please sign in to comment.