Skip to content

Commit

Permalink
Change constructor signature for ActiveAdmin::Router (internal use on…
Browse files Browse the repository at this point in the history
…ly).
  • Loading branch information
varyonic committed Sep 15, 2017
1 parent 16de3c7 commit f716652
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 20 deletions.
16 changes: 10 additions & 6 deletions lib/active_admin/application.rb
Expand Up @@ -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.
Expand Down
22 changes: 8 additions & 14 deletions lib/active_admin/router.rb
@@ -1,27 +1,21 @@
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

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
Expand All @@ -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
Expand Down

0 comments on commit f716652

Please sign in to comment.