# frozen_string_literal: truerequire"active_admin/router"require"active_admin/application_settings"require"active_admin/namespace_settings"moduleActiveAdminclassApplicationclass<<selfdefsetting(name,default)ApplicationSettings.registername,defaultenddefinheritable_setting(name,default)NamespaceSettings.registername,defaultendenddefsettings@settings||=SettingsNode.build(ApplicationSettings)enddefnamespace_settings@namespace_settings||=SettingsNode.build(NamespaceSettings)enddefrespond_to_missing?(method,include_private=false)[settings,namespace_settings].any?{|sets|sets.respond_to?(method)}||superenddefmethod_missing(method,*args)ifsettings.respond_to?(method)settings.send(method,*args)elsifnamespace_settings.respond_to?(method)namespace_settings.send(method,*args)elsesuperendendattr_reader:namespacesdefinitialize@namespaces=Namespace::Store.newendincludeAssetRegistration# Event that gets triggered on load of Active AdminBeforeLoadEvent="active_admin.application.before_load".freezeAfterLoadEvent="active_admin.application.after_load".freeze# Runs before the app's AA initializerdefsetup!register_default_assetsend# Runs after the app's AA initializerdefprepare!remove_active_admin_load_paths_from_rails_autoload_and_eager_loadattach_reloaderend# Registers a brand new configuration for the given resource.defregister(resource,options={},&block)ns=options.fetch(:namespace){default_namespace}namespace(ns).registerresource,options,&blockend# Creates a namespace for the given name## Yields the namespace if a block is given## @return [Namespace] the new or existing namespacedefnamespace(name)name||=:rootnamespace=namespaces[name.to_sym]||=beginnamespace=Namespace.new(self,name)ActiveSupport::Notifications.instrumentActiveAdmin::Namespace::RegisterEvent,{active_admin_namespace: namespace}namespaceendyield(namespace)ifblock_given?namespaceend# Register a page## @param name [String] The page name# @option [Hash] Accepts option :namespace.# @&block The registration block.#defregister_page(name,options={},&block)ns=options.fetch(:namespace){default_namespace}namespace(ns).register_pagename,options,&blockend# Whether all configuration files have been loadeddefloaded?@@loaded||=falseend# Removes all defined controllers from memory. Useful in# development, where they are reloaded on each request.defunload!namespaces.each&:unload!@@loaded=falseend# Loads all ruby files that are within the load_paths setting.# To reload everything simply call `ActiveAdmin.unload!`defload!unlessloaded?ActiveSupport::Notifications.instrumentBeforeLoadEvent,{active_admin_application: self}# before_load hookfiles.each{|file|loadfile}# load filesnamespace(default_namespace)# init AA resourcesActiveSupport::Notifications.instrumentAfterLoadEvent,{active_admin_application: self}# after_load hook@@loaded=trueendenddefload(file)DatabaseHitDuringLoad.capture{super}end# Returns ALL the files to be loadeddeffilesload_paths.flatten.compact.uniq.flat_map{|path|Dir["#{path}/**/*.rb"].sort}end# Creates all the necessary routes for the ActiveAdmin configurations## Use this within the routes.rb file:## Application.routes.draw do |map|# ActiveAdmin.routes(self)# end## @param rails_router [ActionDispatch::Routing::Mapper]defroutes(rails_router)load!Router.new(router: rails_router,namespaces: namespaces).applyend# Adds before, around and after filters to all controllers.# Example usage:# ActiveAdmin.before_action :authenticate_admin!#AbstractController::Callbacks::ClassMethods.public_instance_methods.select{|m|m.match(/_action\z/)}.eachdo|name|define_methodnamedo|*args,&block|ActiveSupport.on_load(:active_admin_controller)dopublic_sendname,*args,&blockendendenddefcontrollers_for_filterscontrollers=[BaseController]controllers.push*Devise.controllers_for_filtersifDependency.devise?controllersendprivatedefregister_default_assetsregister_stylesheet"active_admin.css",media: "all"register_javascript"active_admin.js"end# Since app/admin is alphabetically before app/models, we have to remove it# from the host app's +autoload_paths+ to prevent missing constant errors.## As well, we have to remove it from +eager_load_paths+ to prevent the# files from being loaded twice in production.defremove_active_admin_load_paths_from_rails_autoload_and_eager_loadActiveSupport::Dependencies.autoload_paths-=load_pathsRails.application.config.eager_load_paths-=load_pathsend# Hook into the Rails code reloading mechanism so that things are reloaded# properly in development mode.## If any of the app files (e.g. models) has changed, we need to reload all# the admin files. If the admin files themselves has changed, we need to# regenerate the routes as well.defattach_reloaderRails.application.config.after_initializedo|app|unload_active_admin=->{ActiveAdmin.application.unload!}ifapp.config.reload_classes_only_on_change# Rails is about to unload all the app files (e.g. models), so we# should first unload the classes generated by Active Admin, otherwise# they will contain references to the stale (unloaded) classes.ActiveSupport::Reloader.to_prepare(prepend: true,&unload_active_admin)else# If the user has configured the app to always reload app files after# each request, so we should unload the generated classes too.ActiveSupport::Reloader.to_complete(&unload_active_admin)endadmin_dirs={}load_paths.eachdo|path|admin_dirs[path]=[:rb]endroutes_reloader=app.config.file_watcher.new([],admin_dirs)doapp.reload_routes!endapp.reloaders<<routes_reloaderActiveSupport::Reloader.to_preparedo# Rails might have reloaded the routes for other reasons (e.g.# routes.rb has changed), in which case Active Admin would have been# loaded via the `ActiveAdmin.routes` call in `routes.rb`.## Otherwise, we should check if any of the admin files are changed# and force the routes to reload if necessary. This would again causes# Active Admin to load via `ActiveAdmin.routes`.## Finally, if Active Admin is still not loaded at this point, then we# would need to load it manually.unlessActiveAdmin.application.loaded?routes_reloader.execute_if_updatedActiveAdmin.application.load!endendendendendend