Skip to content

Commit c2103ad

Browse files
committed
Only reload Active Admin if active admin files have changed
This is a HUGE performance increase in development mode. Before this Active Admin was reloading on every single request to the servers (assets included). Now we only reload the Active Admin files if something in the AA load paths has changed.
1 parent 7f263d9 commit c2103ad

File tree

4 files changed

+29
-10
lines changed

4 files changed

+29
-10
lines changed

features/support/env.rb

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,18 +9,24 @@
99
require File.expand_path('../../../spec/support/detect_rails_version', __FILE__)
1010
ENV["RAILS"] = detect_rails_version
1111

12+
ENV["RAILS_ENV"] ||= "cucumber"
13+
ENV['RAILS_ROOT'] = File.expand_path("../../../spec/rails/rails-#{ENV["RAILS"]}", __FILE__)
14+
15+
1216
require 'rubygems'
1317
require "bundler"
1418
Bundler.setup
1519

16-
ENV["RAILS_ENV"] ||= "cucumber"
17-
ENV['RAILS_ROOT'] = File.expand_path("../../../spec/rails/rails-#{ENV["RAILS"]}", __FILE__)
18-
1920
# Create the test app if it doesn't exists
2021
unless File.exists?(ENV['RAILS_ROOT'])
2122
system 'rake setup'
2223
end
2324

25+
# Ensure the Active Admin load path is happy
26+
require 'rails'
27+
require 'active_admin'
28+
ActiveAdmin.application.load_paths = [ENV['RAILS_ROOT'] + "/app/admin"]
29+
2430
require ENV['RAILS_ROOT'] + '/config/environment'
2531

2632
# Setup autoloading of ActiveAdmin and the load path

lib/active_admin/application.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ def remove_active_admin_load_paths_from_rails_autoload_and_eager_load
211211
end
212212

213213
def attach_reloader
214-
ActiveAdmin::Reloader.new(Rails.version).attach!
214+
ActiveAdmin::Reloader.new(self, Rails.version).attach!
215215
end
216216

217217

lib/active_admin/reloader.rb

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,16 +6,21 @@ class Reloader
66
# @param [String] rails_version
77
# The version of Rails we're using. We use this to switch between
88
# the correcr Rails reloader class.
9-
def initialize(rails_version)
9+
def initialize(app, rails_version)
10+
@app = app
1011
@rails_version = rails_version.to_s
1112
end
1213

1314
# Attach to Rails and perform the reload on each request.
1415
def attach!
15-
reloader_class.to_prepare do
16+
file_update_checker = ActiveSupport::FileUpdateChecker.new(@app.load_paths) do
1617
ActiveAdmin.application.unload!
1718
Rails.application.reload_routes!
1819
end
20+
21+
reloader_class.to_prepare do
22+
file_update_checker.execute_if_updated
23+
end
1924
end
2025

2126
def reloader_class

spec/unit/reloader_spec.rb

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,25 +4,33 @@
44
begin
55
ActionDispatch::Reloader
66
rescue
7-
module ActionDispatch; module Reloader; end; end
7+
module ActionDispatch; module Reloader; def self.to_prepare; end; end; end
88
end
99

1010
begin
1111
ActionDispatch::Callbacks
1212
rescue
13-
module ActionDispatch; module Callbacks; end; end
13+
module ActionDispatch; module Callbacks; def self.to_prepare; end; end; end
1414
end
1515

1616

1717
describe ActiveAdmin::Reloader do
1818

19+
let(:mock_app){ mock(:load_paths => []) }
20+
1921
it "should use ActionDispatch::Reloader if rails 3.1" do
20-
reloader = ActiveAdmin::Reloader.new '3.1.0'
22+
reloader = ActiveAdmin::Reloader.new mock_app, '3.1.0'
2123
reloader.reloader_class.should == ActionDispatch::Reloader
2224
end
2325

2426
it "should use ActionDispatch::Callbacks if rails 3.0" do
25-
reloader = ActiveAdmin::Reloader.new '3.0.0'
27+
reloader = ActiveAdmin::Reloader.new mock_app, '3.0.0'
2628
reloader.reloader_class.should == ActionDispatch::Callbacks
2729
end
30+
31+
it "should initialize a new file update checker" do
32+
ActiveSupport::FileUpdateChecker.should_receive(:new).with(mock_app.load_paths).and_return(mock(:execute_if_updated => true))
33+
ActiveAdmin::Reloader.new(mock_app, '3.1.0').attach!
34+
end
35+
2836
end

0 commit comments

Comments
 (0)