From 55b25c2913e35b198c58e6f863dbe75b5f2d9643 Mon Sep 17 00:00:00 2001 From: Andrea Dal Ponte Date: Tue, 16 Jan 2018 22:40:21 +0100 Subject: [PATCH 1/3] Eager load paper_trail item association to avoid N+1 queries --- lib/rails_admin/extensions/paper_trail/auditing_adapter.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/lib/rails_admin/extensions/paper_trail/auditing_adapter.rb b/lib/rails_admin/extensions/paper_trail/auditing_adapter.rb index c1e15231f1..b13f98cd83 100644 --- a/lib/rails_admin/extensions/paper_trail/auditing_adapter.rb +++ b/lib/rails_admin/extensions/paper_trail/auditing_adapter.rb @@ -64,7 +64,9 @@ def initialize(controller, user_class = 'User', version_class = '::Version') end def latest - @version_class.order('id DESC').limit(100).collect { |version| VersionProxy.new(version, @user_class) } + @version_class + .order(id: :desc).includes(:item).limit(100) + .collect { |version| VersionProxy.new(version, @user_class) } end def delete_object(_object, _model, _user) From f7a4b8448ccc8026e544036843d6555efdb4f303 Mon Sep 17 00:00:00 2001 From: Andrea Dal Ponte Date: Tue, 16 Jan 2018 23:21:36 +0100 Subject: [PATCH 2/3] Make auditing versions limit configurable into dashboard --- lib/rails_admin/config/actions/dashboard.rb | 6 +++++- lib/rails_admin/extensions/history/auditing_adapter.rb | 4 ++-- lib/rails_admin/extensions/history/history.rb | 4 ++-- lib/rails_admin/extensions/paper_trail/auditing_adapter.rb | 4 ++-- 4 files changed, 11 insertions(+), 7 deletions(-) diff --git a/lib/rails_admin/config/actions/dashboard.rb b/lib/rails_admin/config/actions/dashboard.rb index e32fc0e1ed..6cde59fd12 100644 --- a/lib/rails_admin/config/actions/dashboard.rb +++ b/lib/rails_admin/config/actions/dashboard.rb @@ -12,9 +12,13 @@ class Dashboard < RailsAdmin::Config::Actions::Base nil end + register_instance_option :auditing_versions_limit do + 50 + end + register_instance_option :controller do proc do - @history = @auditing_adapter && @auditing_adapter.latest || [] + @history = @auditing_adapter && @auditing_adapter.latest(@action.auditing_versions_limit) || [] if @action.statistics? @abstract_models = RailsAdmin::Config.visible_models(controller: self).collect(&:abstract_model) diff --git a/lib/rails_admin/extensions/history/auditing_adapter.rb b/lib/rails_admin/extensions/history/auditing_adapter.rb index 46a6425a48..6326d067e4 100644 --- a/lib/rails_admin/extensions/history/auditing_adapter.rb +++ b/lib/rails_admin/extensions/history/auditing_adapter.rb @@ -8,8 +8,8 @@ def initialize(controller, user_class = User) require 'rails_admin/extensions/history/history' end - def latest - ::RailsAdmin::History.latest + def latest(count = 100) + ::RailsAdmin::History.latest(count) end def delete_object(object, model, user) diff --git a/lib/rails_admin/extensions/history/history.rb b/lib/rails_admin/extensions/history/history.rb index 1e96c3b4a3..d497ccdf43 100644 --- a/lib/rails_admin/extensions/history/history.rb +++ b/lib/rails_admin/extensions/history/history.rb @@ -11,8 +11,8 @@ class History < ActiveRecord::Base default_scope { order('id DESC') } class << self - def latest - limit(100) + def latest(count = 100) + limit(count) end def create_history_item(message, object, abstract_model, user) diff --git a/lib/rails_admin/extensions/paper_trail/auditing_adapter.rb b/lib/rails_admin/extensions/paper_trail/auditing_adapter.rb index b13f98cd83..daa3c9f344 100644 --- a/lib/rails_admin/extensions/paper_trail/auditing_adapter.rb +++ b/lib/rails_admin/extensions/paper_trail/auditing_adapter.rb @@ -63,9 +63,9 @@ def initialize(controller, user_class = 'User', version_class = '::Version') end end - def latest + def latest(count = 100) @version_class - .order(id: :desc).includes(:item).limit(100) + .order(id: :desc).includes(:item).limit(count) .collect { |version| VersionProxy.new(version, @user_class) } end From 8e7e9bc687747ecc74a1e01c56e2896618a2944b Mon Sep 17 00:00:00 2001 From: Andrea Dal Ponte Date: Tue, 23 Jan 2018 16:29:40 +0100 Subject: [PATCH 3/3] Fix: rubocop offences --- lib/rails_admin/extensions/paper_trail/auditing_adapter.rb | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/rails_admin/extensions/paper_trail/auditing_adapter.rb b/lib/rails_admin/extensions/paper_trail/auditing_adapter.rb index daa3c9f344..048ff94f95 100644 --- a/lib/rails_admin/extensions/paper_trail/auditing_adapter.rb +++ b/lib/rails_admin/extensions/paper_trail/auditing_adapter.rb @@ -64,9 +64,9 @@ def initialize(controller, user_class = 'User', version_class = '::Version') end def latest(count = 100) - @version_class - .order(id: :desc).includes(:item).limit(count) - .collect { |version| VersionProxy.new(version, @user_class) } + @version_class. + order(id: :desc).includes(:item).limit(count). + collect { |version| VersionProxy.new(version, @user_class) } end def delete_object(_object, _model, _user)