diff --git a/app/controllers/administrate/application_controller.rb b/app/controllers/administrate/application_controller.rb index f1992704cd..ae2ea23bdd 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,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 diff --git a/lib/administrate/base_dashboard.rb b/lib/administrate/base_dashboard.rb index 04d140dd4e..72ea20f220 100644 --- a/lib/administrate/base_dashboard.rb +++ b/lib/administrate/base_dashboard.rb @@ -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) diff --git a/lib/administrate/field/has_many.rb b/lib/administrate/field/has_many.rb index 04ddcac017..999890acf8 100644 --- a/lib/administrate/field/has_many.rb +++ b/lib/administrate/field/has_many.rb @@ -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)