Permalink
Browse files

Initial work on more thorough config reset, refs #275, #289, #515

  • Loading branch information...
1 parent 55aa1cf commit 42aba16e8601ba5cc2595e523f73faec331573cf @kaapa kaapa committed Jul 11, 2011
@@ -31,15 +31,15 @@ def get_object
private
def _authenticate!
- instance_eval &RailsAdmin.authenticate_with
+ instance_eval &RailsAdmin::Config.authenticate_with
end
def _authorize!
- instance_eval &RailsAdmin.authorize_with
+ instance_eval &RailsAdmin::Config.authorize_with
end
def _current_user
- instance_eval &RailsAdmin.current_user_method
+ instance_eval &RailsAdmin::Config.current_user_method
end
def set_plugin_name
@@ -1,5 +1,9 @@
if defined?(::ActiveRecord)
class ActiveRecord::Base
+ def self.rails_admin(&block)
+ RailsAdmin::Config.model(self, &block)
+ end
+
def rails_admin_default_object_label_method
"#{self.class.to_s} ##{self.try :id}"
end
View
@@ -8,152 +8,57 @@
require 'rails_admin/support/core_extensions'
module RailsAdmin
- class AuthenticationNotConfigured < StandardError; end
-
- # RailsAdmin is setup to try and authenticate with warden
- # If warden is found, then it will try to authenticate
- #
- # This is valid for custom warden setups, and also devise
- # If you're using the admin setup for devise, you should set RailsAdmin to use the admin
- #
- # By default, this will raise in any of the following environments
- # * production
- # * beta
- # * uat
- # * staging
+ # Copy of initializer blocks for reset and reinitialization
#
- # @see RailsAdmin.authenticate_with
- # @see RailsAdmin.authorize_with
- DEFAULT_AUTHENTICATION = Proc.new do
- warden = request.env['warden']
- if warden
- warden.authenticate!
- else
- if %w(production beta uat staging).include?(Rails.env)
- raise AuthenticationNotConfigured, "See RailsAdmin.authenticate_with or setup Devise / Warden"
- end
- end
- end
-
- DEFAULT_AUTHORIZE = Proc.new {}
-
- DEFAULT_CURRENT_USER = Proc.new do
- warden = request.env["warden"]
- if warden
- warden.user
- elsif respond_to?(:current_user)
- current_user
- else
- raise "See RailsAdmin.current_user_method or setup Devise / Warden"
- end
- end
+ # @see RailsAdmin.reset
+ @initializers = []
- # Setup authentication to be run as a before filter
- # This is run inside the controller instance so you can setup any authentication you need to
- #
- # By default, the authentication will run via warden if available
- # and will run the default.
- #
- # If you use devise, this will authenticate the same as _authenticate_user!_
- #
- # @example Devise admin
- # RailsAdmin.authenticate_with do
- # authenticate_admin!
- # end
- #
- # @example Custom Warden
- # RailsAdmin.authenticate_with do
- # warden.authenticate! :scope => :paranoid
- # end
- #
- # @see RailsAdmin::DEFAULT_AUTHENTICATION
- def self.authenticate_with(&blk)
- @authenticate = blk if blk
- @authenticate || DEFAULT_AUTHENTICATION
+ def self.authenticate_with(&block)
+ 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
- # Setup authorization to be run as a before filter
- # This is run inside the controller instance so you can setup any authorization you need to.
- #
- # By default, there is no authorization.
- #
- # @example Custom
- # RailsAdmin.authorize_with do
- # redirect_to root_path unless warden.user.is_admin?
- # end
- #
- # To use an authorization adapter, pass the name of the adapter. For example,
- # to use with CanCan[https://github.com/ryanb/cancan], pass it like this.
- #
- # @example CanCan
- # RailsAdmin.authorize_with :cancan
- #
- # See the wiki[https://github.com/sferik/rails_admin/wiki] for more on authorization.
- #
- # @see RailsAdmin::DEFAULT_AUTHORIZE
def self.authorize_with(*args, &block)
- extension = args.shift
-
- if(extension)
- @authorize = Proc.new {
- @authorization_adapter = AUTHORIZATION_ADAPTERS[extension].new(*([self] + args).compact)
- }
- else
- @authorize = block if block
- end
-
- @authorize || DEFAULT_AUTHORIZE
+ 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
- # Setup a different method to determine the current user or admin logged in.
- # This is run inside the controller instance and made available as a helper.
- #
- # By default, _request.env["warden"].user_ or _current_user_ will be used.
- #
- # @example Custom
- # RailsAdmin.current_user_method do
- # current_admin
- # end
- #
- # @see RailsAdmin::DEFAULT_CURRENT_USER
def self.current_user_method(&block)
- @current_user = block if block
- @current_user || DEFAULT_CURRENT_USER
+ 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
- # Setup configuration using an extension-provided ConfigurationAdapter
- #
- # @example Custom configuration for role-based setup.
- # RailsAdmin.configure_with(:custom) do |config|
- # config.models = ['User', 'Comment']
- # config.roles = {
- # 'Admin' => :all,
- # 'User' => ['User']
- # }
- # end
- #
- # RailsAdmin.config do
- # # standard config still applies...
- # end
- def self.configure_with(extension)
- configuration = CONFIGURATION_ADAPTERS[extension].new
- yield(configuration) if block_given?
+ def self.configure_with(extension, &block)
+ 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
# Setup RailsAdmin
#
- # If a model class is provided as the first argument model specific
- # configuration is loaded and returned.
+ # 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.
#
- # Otherwise yields self for general configuration to be used in
- # an initializer.
+ # Otherwise returns RailsAdmin::Config class.
#
- # @see RailsAdmin::Config.load
+ # @see RailsAdmin::Config
def self.config(entity = nil, &block)
- if not entity
+ if entity
+ RailsAdmin::Config.model(entity, &block)
+ elsif block_given?
+ @initializers << block
yield RailsAdmin::Config
else
- RailsAdmin::Config.model(entity, &block)
+ RailsAdmin::Config
end
end
+
+ # Reset RailsAdmin configuration to defaults and then reapply all stored
+ # initialization blocks.
+ def self.reset
+ RailsAdmin::Config.reset
+ @initializers.clear.each {|block| self.config(&block)}
+ end
end
Oops, something went wrong.

0 comments on commit 42aba16

Please sign in to comment.