require'active_admin/resource_collection'moduleActiveAdmin# Namespaces are the basic organizing principle for resources within Active Admin## Each resource is registered into a namespace which defines:# * the namespaceing for routing# * the module to namespace controllers# * the menu which gets displayed (other resources in the same namespace)## For example:## ActiveAdmin.register Post, namespace: :admin## Will register the Post model into the "admin" namespace. This will namespace the# urls for the resource to "/admin/posts" and will set the controller to# Admin::PostsController## You can also register to the "root" namespace, which is to say no namespace at all.## ActiveAdmin.register Post, namespace: false## This will register the resource to an instantiated namespace called :root. The# resource will be accessible from "/posts" and the controller will be PostsController.#classNamespaceclass<<selfdefsetting(name,default)Deprecation.warn"This method does not do anything and will be removed."endendRegisterEvent='active_admin.namespace.register'.freezeattr_reader:application,:resources,:menusdefinitialize(application,name)@application=application@name=name.to_s.underscore@resources=ResourceCollection.newregister_moduleunlessroot?build_menu_collectionenddefname@name.to_symenddefsettings@settings||=SettingsNode.build(application.namespace_settings)enddefrespond_to_missing?(method,include_private=false)settings.respond_to?(method)||superenddefmethod_missing(method,*args)settings.respond_to?(method)?settings.send(method,*args):superend# Register a resource into this namespace. The preffered method to access this is to# use the global registration ActiveAdmin.register which delegates to the proper# namespace instance.defregister(resource_class,options={},&block)config=find_or_build_resource(resource_class,options)# Register the resourceregister_resource_controller(config)parse_registration_block(config,&block)ifblock_given?reset_menu!# Dispatch a registration eventActiveSupport::Notifications.publishActiveAdmin::Resource::RegisterEvent,config# Return the configconfigenddefregister_page(name,options={},&block)config=build_page(name,options)# Register the resourceregister_page_controller(config)parse_page_registration_block(config,&block)ifblock_given?reset_menu!configenddefroot?name==:rootend# Returns the name of the module if required. Will be nil if none# is required.## eg:# Namespace.new(:admin).module_name # => 'Admin'# Namespace.new(:root).module_name # => nil#defmodule_nameroot??nil:@name.camelizeenddefroute_prefixroot??nil:@nameend# Unload all the registered resources for this namespacedefunload!unload_resources!reset_menu!end# Returns the first registered ActiveAdmin::Resource instance for a given classdefresource_for(klass)resources[klass]enddeffetch_menu(name)@menus.fetch(name)enddefreset_menu!@menus.clear!end# Add a callback to be ran when we build the menu## @param [Symbol] name The name of the menu. Default: :default# @yield [ActiveAdmin::Menu] The block to be ran when the menu is built## @return [void]defbuild_menu(name=DEFAULT_MENU)@menus.before_builddo|menus|menus.menunamedo|menu|yieldmenuendendend# The default logout menu item## @param [ActiveAdmin::MenuItem] menu The menu to add the logout link to# @param [Fixnum] priority The numeric priority for the order in which it appears# @param [Hash] html_options An options hash to pass along to link_to#defadd_logout_button_to_menu(menu,priority=20,html_options={})iflogout_link_pathhtml_options=html_options.reverse_merge(method: logout_link_method||:get)menu.addid: 'logout',priority: priority,html_options: html_options,label: ->{I18n.t'active_admin.logout'},url: ->{render_or_call_method_or_proc_onself,active_admin_namespace.logout_link_path},if: :current_active_admin_user?endend# The default user session menu item## @param [ActiveAdmin::MenuItem] menu The menu to add the logout link to# @param [Fixnum] priority The numeric priority for the order in which it appears# @param [Hash] html_options An options hash to pass along to link_to#defadd_current_user_to_menu(menu,priority=10,html_options={})ifcurrent_user_methodmenu.addid: 'current_user',priority: priority,html_options: html_options,label: ->{display_namecurrent_active_admin_user},url: ->{auto_url_for(current_active_admin_user)},if: :current_active_admin_user?endendprotecteddefbuild_menu_collection@menus=MenuCollection.new@menus.on_builddobuild_default_utility_navresources.eachdo|resource|resource.add_to_menu(@menus)endendend# Builds the default utility navigation in top right header with current user & logout buttondefbuild_default_utility_navreturnif@menus.exists?:utility_navigation@menus.menu:utility_navigationdo|menu|add_current_user_to_menumenuadd_logout_button_to_menumenuendend# Either returns an existing Resource instance or builds a new one.deffind_or_build_resource(resource_class,options)resources.addResource.new(self,resource_class,options)enddefbuild_page(name,options)resources.addPage.new(self,name,options)end# TODO: replace `eval` with `Class.new`defregister_page_controller(config)eval"class ::#{config.controller_name} < ActiveAdmin::PageController; end"config.controller.active_admin_config=configenddefunload_resources!resources.eachdo|resource|parent=(module_name||'Object').constantizename=resource.controller_name.split('::').lastparent.send(:remove_const,name)ifparent.const_defined?(name,false)# Remove circular referencesresource.controller.active_admin_config=nilifresource.is_a?(Resource)&&resource.dslresource.dsl.run_registration_block{@config=nil}endend@resources=ResourceCollection.newend# Creates a ruby module to namespace all the classes in if requireddefregister_moduleunlessObject.const_defined?module_nameObject.const_setmodule_name,Module.newendend# TODO: replace `eval` with `Class.new`defregister_resource_controller(config)eval"class ::#{config.controller_name} < ActiveAdmin::ResourceController; end"config.controller.active_admin_config=configenddefparse_registration_block(config,&block)config.dsl=ResourceDSL.new(config)config.dsl.run_registration_block(&block)enddefparse_page_registration_block(config,&block)PageDSL.new(config).run_registration_block(&block)endclassStoreincludeEnumerabledelegate:[],:[]=,:empty?,to: :@namespacesdefinitialize@namespaces={}enddefeach(&block)@namespaces.values.each(&block)enddefnames@namespaces.keysendendendend