Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve performance for n+1s #817

Merged
merged 3 commits into from
May 2, 2017
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions app/controllers/administrate/application_controller.rb
Original file line number Diff line number Diff line change
@@ -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?

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Line is too long. [82/80]

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hmm this really looks like it needs to be moved to the Administrate::Page::Collection model.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's try that and see how it goes?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Well, I thought about moving it to the collection class, but that didn't work out the way I thought it would. Instead, it's been moved to the dashboard and we pass the includes from there.

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,10 @@ def find_resource(param)
resource_class.find(param)
end

def resource_includes
dashboard.association_includes
end

def resource_params
params.require(resource_name).permit(dashboard.permitted_attributes)
end
11 changes: 11 additions & 0 deletions lib/administrate/base_dashboard.rb
Original file line number Diff line number Diff line change
@@ -52,6 +52,17 @@ def display_resource(resource)
"#{resource.class} ##{resource.id}"
end

def association_includes
association_classes = [Field::HasMany, Field::HasOne, Field::BelongsTo]

collection_attributes.map do |key|
field = self.class::ATTRIBUTE_TYPES[key]

next key if association_classes.include?(field)
key if association_classes.include?(field.try :deferred_class)
end.compact
end

private

def attribute_not_found_message(attr)
7 changes: 6 additions & 1 deletion lib/administrate/field/has_many.rb
Original file line number Diff line number Diff line change
@@ -38,7 +38,8 @@ def permitted_attribute
end

def resources(page = 1)
order.apply(data).page(page).per(limit)
resources = order.apply(data).page(page).per(limit)
includes.any? ? resources.includes(*includes) : resources
end

def more_than_limit?
@@ -47,6 +48,10 @@ def more_than_limit?

private

def includes
associated_dashboard.association_includes
end

def candidate_resources
if options.key?(:includes)
includes = options.fetch(:includes)