Permalink
Browse files

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.
  • Loading branch information...
1 parent b16054e commit ef255e5119cd585e1f3bb36428651b9d7dd9704f @gregbell gregbell committed Oct 29, 2011
Showing with 29 additions and 10 deletions.
  1. +9 −3 features/support/env.rb
  2. +1 −1 lib/active_admin/application.rb
  3. +7 −2 lib/active_admin/reloader.rb
  4. +12 −4 spec/unit/reloader_spec.rb
View
@@ -9,18 +9,24 @@
require File.expand_path('../../../spec/support/detect_rails_version', __FILE__)
ENV["RAILS"] = detect_rails_version
+ENV["RAILS_ENV"] ||= "cucumber"
+ENV['RAILS_ROOT'] = File.expand_path("../../../spec/rails/rails-#{ENV["RAILS"]}", __FILE__)
+
+
require 'rubygems'
require "bundler"
Bundler.setup
-ENV["RAILS_ENV"] ||= "cucumber"
-ENV['RAILS_ROOT'] = File.expand_path("../../../spec/rails/rails-#{ENV["RAILS"]}", __FILE__)
-
# Create the test app if it doesn't exists
unless File.exists?(ENV['RAILS_ROOT'])
system 'rake setup'
end
+# Ensure the Active Admin load path is happy
+require 'rails'
+require 'active_admin'
+ActiveAdmin.application.load_paths = [ENV['RAILS_ROOT'] + "/app/admin"]
+
require ENV['RAILS_ROOT'] + '/config/environment'
# Setup autoloading of ActiveAdmin and the load path
@@ -208,7 +208,7 @@ def remove_active_admin_load_paths_from_rails_autoload_and_eager_load
end
def attach_reloader
- ActiveAdmin::Reloader.new(Rails.version).attach!
+ ActiveAdmin::Reloader.new(self, Rails.version).attach!
end
@@ -6,16 +6,21 @@ class Reloader
# @param [String] rails_version
# The version of Rails we're using. We use this to switch between
# the correcr Rails reloader class.
- def initialize(rails_version)
+ def initialize(app, rails_version)
+ @app = app
@rails_version = rails_version.to_s
end
# Attach to Rails and perform the reload on each request.
def attach!
- reloader_class.to_prepare do
+ file_update_checker = ActiveSupport::FileUpdateChecker.new(@app.load_paths) do
ActiveAdmin.application.unload!
Rails.application.reload_routes!
end
+
+ reloader_class.to_prepare do
+ file_update_checker.execute_if_updated
+ end
end
def reloader_class
View
@@ -4,25 +4,33 @@
begin
ActionDispatch::Reloader
rescue
- module ActionDispatch; module Reloader; end; end
+ module ActionDispatch; module Reloader; def self.to_prepare; end; end; end
end
begin
ActionDispatch::Callbacks
rescue
- module ActionDispatch; module Callbacks; end; end
+ module ActionDispatch; module Callbacks; def self.to_prepare; end; end; end
end
describe ActiveAdmin::Reloader do
+ let(:mock_app){ mock(:load_paths => []) }
+
it "should use ActionDispatch::Reloader if rails 3.1" do
- reloader = ActiveAdmin::Reloader.new '3.1.0'
+ reloader = ActiveAdmin::Reloader.new mock_app, '3.1.0'
reloader.reloader_class.should == ActionDispatch::Reloader
end
it "should use ActionDispatch::Callbacks if rails 3.0" do
- reloader = ActiveAdmin::Reloader.new '3.0.0'
+ reloader = ActiveAdmin::Reloader.new mock_app, '3.0.0'
reloader.reloader_class.should == ActionDispatch::Callbacks
end
+
+ it "should initialize a new file update checker" do
+ ActiveSupport::FileUpdateChecker.should_receive(:new).with(mock_app.load_paths).and_return(mock(:execute_if_updated => true))
+ ActiveAdmin::Reloader.new(mock_app, '3.1.0').attach!
+ end
+
end

0 comments on commit ef255e5

Please sign in to comment.