From f7166524f7339052e579ac3f3e93b20d395e5466 Mon Sep 17 00:00:00 2001 From: Piers Chambers Date: Thu, 14 Sep 2017 21:53:43 -0400 Subject: [PATCH] Change constructor signature for ActiveAdmin::Router (internal use only). --- lib/active_admin/application.rb | 16 ++++++++++------ lib/active_admin/router.rb | 22 ++++++++-------------- 2 files changed, 18 insertions(+), 20 deletions(-) diff --git a/lib/active_admin/application.rb b/lib/active_admin/application.rb index c272d33b4c9..d51e574af7f 100644 --- a/lib/active_admin/application.rb +++ b/lib/active_admin/application.rb @@ -126,14 +126,18 @@ def files load_paths.flatten.compact.uniq.flat_map{ |path| Dir["#{path}/**/*.rb"] } end - def router - @router ||= Router.new(self) - end - - # One-liner called by user's config/routes.rb file + # 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] def routes(rails_router) load! - router.apply(rails_router) + Router.new(router: rails_router, namespaces: namespaces).apply end # Adds before, around and after filters to all controllers. diff --git a/lib/active_admin/router.rb b/lib/active_admin/router.rb index 87cffb22429..7575ee9b332 100644 --- a/lib/active_admin/router.rb +++ b/lib/active_admin/router.rb @@ -1,19 +1,13 @@ module ActiveAdmin + # @private class Router - def initialize(application) - @application = application + attr_reader :namespaces, :router + + def initialize(router:, namespaces:) + @router, @namespaces = router, namespaces 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 router [ActionDispatch::Routing::Mapper] - def apply(router) + def apply(router = @router) define_root_routes(router) define_resources_routes(router) end @@ -21,7 +15,7 @@ def apply(router) private def define_root_routes(router) - @application.namespaces.each do |namespace| + namespaces.each do |namespace| if namespace.root? router.root namespace.root_to_options.merge(to: namespace.root_to) else @@ -34,7 +28,7 @@ def define_root_routes(router) # Defines the routes for each resource def define_resources_routes(router) - resources = @application.namespaces.flat_map{ |n| n.resources.values } + resources = namespaces.flat_map{ |n| n.resources.values } resources.each do |config| define_resource_routes(router, config) end