Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Config lazy setup, refs #275, #289, #515

  • Loading branch information...
commit 5a089c7b6703ae496ccc93bc0933607f4577236a 1 parent e6ca7d9
@kaapa kaapa authored
View
43 lib/rails_admin.rb
@@ -8,28 +8,34 @@
require 'rails_admin/support/core_extensions'
module RailsAdmin
- # Copy of initializer blocks for reset and reinitialization
+ # Copy of initializer blocks for initialization
#
- # @see RailsAdmin.reset
+ # @see RailsAdmin.setup
@initializers = []
+ # Whether or not the initializers have been run
+ #
+ # @see RailsAdmin.reset
+ # @see RailsAdmin.setup
+ @initialized = false
+
def self.authenticate_with(&block)
- ActiveSupport::Deprecation.warn("'#{self.name}.authenticate_with { }' is deprecated, use 'RailsAdmin.config{|c| c.authenticate_with { } instead", caller)
+ ActiveSupport::Deprecation.warn("'#{self.name}.authenticate_with { }' is deprecated, use 'RailsAdmin.config{|c| c.authenticate_with }' instead", caller)
self.config {|c| c.authenticate_with(&block) }
end
def self.authorize_with(*args, &block)
- ActiveSupport::Deprecation.warn("'#{self.name}.authorize_with { }' is deprecated, use 'RailsAdmin.config{|c| c.authorize_with { } instead", caller)
+ ActiveSupport::Deprecation.warn("'#{self.name}.authorize_with { }' is deprecated, use 'RailsAdmin.config{|c| c.authorize_with }' instead", caller)
self.config {|c| c.authorize_with(*args, &block) }
end
def self.current_user_method(&block)
- ActiveSupport::Deprecation.warn("'#{self.name}.current_user_method { }' is deprecated, use 'RailsAdmin.config{|c| c.current_user_method { } instead", caller)
+ ActiveSupport::Deprecation.warn("'#{self.name}.current_user_method { }' is deprecated, use 'RailsAdmin.config{|c| c.current_user_method }' instead", caller)
self.config {|c| c.current_user_method(&block) }
end
def self.configure_with(extension, &block)
- ActiveSupport::Deprecation.warn("'#{self.name}.configure_with { }' is deprecated, use 'RailsAdmin.config{|c| c.configure_with { } instead", caller)
+ ActiveSupport::Deprecation.warn("'#{self.name}.configure_with { }' is deprecated, use 'RailsAdmin.config{|c| c.configure_with }' instead", caller)
self.config {|c| c.configure_with(extension, &block) }
end
@@ -38,8 +44,10 @@ def self.configure_with(extension, &block)
# Given the first argument is a model class, a model class name
# or an abstract model object proxies to model configuration method.
#
- # If only a block is passed, yields RailsAdmin::Config. The block will be
- # stored for resetting the engine before each request in development mode.
+ # If only a block is passed it is stored to initializer stack to be evaluated
+ # on first request in production mode and on each request in development. If
+ # initialization has already occured (in other words RailsAdmin.setup has
+ # been called) the block will be added to stack and evaluated at once.
#
# Otherwise returns RailsAdmin::Config class.
#
@@ -49,16 +57,27 @@ def self.config(entity = nil, &block)
RailsAdmin::Config.model(entity, &block)
elsif block_given?
@initializers << block
- yield RailsAdmin::Config
+ block.call(RailsAdmin::Config) if @initialized
else
RailsAdmin::Config
end
end
- # Reset RailsAdmin configuration to defaults and then reapply all stored
- # initialization blocks.
+ # Reset RailsAdmin configuration to defaults
def self.reset
RailsAdmin::Config.reset
- @initializers.clear.each {|block| self.config(&block)}
+ @initialized = false
+ end
+
+ # Apply all initializers stored on application startup
+ def self.setup
+ @initializers.each {|block| block.call(RailsAdmin::Config) } unless @initialized
+ @initialized = true
+ end
+
+ # Reset RailsAdmin including initializers
+ def self.test_reset!
+ self.reset
+ @initializers.clear
end
end
View
9 lib/rails_admin/engine.rb
@@ -9,11 +9,14 @@ class Engine < Rails::Engine
end
end
- config.to_prepare do
- if @reset_rails_admin
+ ActionDispatch::Callbacks.before do
+ RailsAdmin.setup
+ end
+
+ if Rails.env == "development"
+ ActionDispatch::Callbacks.after do
RailsAdmin.reset
end
- @reset_rails_admin = true
end
end
end
View
3  spec/spec_helper.rb
@@ -51,7 +51,7 @@
config.include Warden::Test::Helpers
config.before(:each) do
- RailsAdmin::Config.reset
+ RailsAdmin.setup
RailsAdmin::Config.excluded_models = [RelTest, FieldTest]
RailsAdmin::AbstractModel.instance_variable_get("@models").clear
@@ -72,6 +72,7 @@
end
config.after(:each) do
+ RailsAdmin.test_reset!
Warden.test_reset!
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.