Class: ActiveAdmin::Router

Inherits:
Object
  • Object
show all
Defined in:
lib/active_admin/router.rb

Instance Method Summary collapse

Constructor Details

#initialize(application) ⇒ Router

Returns a new instance of Router.



3
4
5
# File 'lib/active_admin/router.rb', line 3

def initialize(application)
  @application = application
end

Instance Method Details

#apply(router) ⇒ Object

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


15
16
17
18
# File 'lib/active_admin/router.rb', line 15

def apply(router)
  define_basic_routes router
  define_resource_routes router
end

#define_basic_routes(router) ⇒ Object

Define any necessary dashboard routes and root



21
22
23
24
25
26
27
28
29
30
31
32
33
# File 'lib/active_admin/router.rb', line 21

def define_basic_routes(router)
  router.instance_exec(@application.namespaces.values, self) do |namespaces, aa_router|
    namespaces.each do |namespace|
      if namespace.root?
        instance_eval &aa_router.root_and_dashboard_routes(namespace)
      else
        namespace(namespace.name) do
          instance_eval &aa_router.root_and_dashboard_routes(namespace)
        end
      end
    end
  end
end

#define_resource_routes(router) ⇒ Object

Define the routes for each resource



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/active_admin/router.rb', line 45

def define_resource_routes(router)
  resource_routes = method(:resource_routes)

  router.instance_exec(@application.namespaces, self) do |namespaces, aa_router|
    resources = namespaces.values.collect{|n| n.resources.resources }.flatten
    resources.each do |config|
      route_definition_block = aa_router.resource_routes(config)

      # Add in the parent if it exists
      if config.belongs_to?
        routes_for_belongs_to = route_definition_block.dup
        route_definition_block = Proc.new do
          # If its optional, make the normal resource routes
          instance_eval &routes_for_belongs_to if config.belongs_to_config.optional?

          # Make the nested belongs_to routes
          # :only is set to nothing so that we don't clobber any existing routes on the resource
          resources config.belongs_to_config.target.resource_name.plural, :only => [] do
            instance_eval &routes_for_belongs_to
          end
        end
      end

      # Add on the namespace if required
      unless config.namespace.root?
        routes_in_namespace = route_definition_block.dup
        route_definition_block = Proc.new do
          namespace config.namespace.name do
            instance_eval(&routes_in_namespace)
          end
        end
      end

      instance_eval &route_definition_block
    end
  end
end

#resource_routes(config) ⇒ Object



83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
# File 'lib/active_admin/router.rb', line 83

def resource_routes(config)
  Proc.new do
    case config
    when Resource
      resources config.resource_name.route_key, :only => config.defined_actions do
        # Define any member actions
        member do
          config.member_actions.each do |action|
            [*action.http_verb].each do |http_verb|
              # eg: get :comment
              send(http_verb, action.name)
            end
          end
        end

        # Define any collection actions
        collection do
          config.collection_actions.each do |action|
            send(action.http_verb, action.name)
          end

          post :batch_action
        end
      end
    when Page
      match "/#{config.underscored_resource_name}" => "#{config.underscored_resource_name}#index"
      config.page_actions.each do |action|
        match "/#{config.underscored_resource_name}/#{action.name}" => "#{config.underscored_resource_name}##{action.name}", :via => action.http_verb
      end
    else
      raise "Unsupported config class: #{config.class}"
    end
  end

end

#root_and_dashboard_routes(namespace) ⇒ Object



35
36
37
38
39
40
41
42
# File 'lib/active_admin/router.rb', line 35

def root_and_dashboard_routes(namespace)
  Proc.new do
    root :to => (namespace.root_to || "dashboard#index")
    if ActiveAdmin::Dashboards.built?
      match '/dashboard' => 'dashboard#index', :as => 'dashboard'
    end
  end
end