Skip to content

Commit

Permalink
Add configuration for dashboards to (not) display in navigation
Browse files Browse the repository at this point in the history
+ Introduces configuration for Dashboards. (Easily extendedable for further configuration options)
+ By default every Dashboard is configured to be displayed in the navigation.

How to remove a Dashboard from navigation:

```
class LineItemDashboard < Administrate::BaseDashboard
  configure do |config|
    config.navigation = false
  end
end
```
  • Loading branch information
robinboening committed Sep 7, 2019
1 parent b53e691 commit 540d098
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 4 deletions.
2 changes: 1 addition & 1 deletion app/views/administrate/application/_navigation.html.erb
Expand Up @@ -8,7 +8,7 @@ as defined by the routes in the `admin/` namespace
%>

<nav class="navigation" role="navigation">
<% Administrate::Namespace.new(namespace).resources.each do |resource| %>
<% Administrate::Navigation.new(namespace).resources.each do |resource| %>
<%= link_to(
display_resource_name(resource),
[namespace, resource_index_route_key(resource)],
Expand Down
14 changes: 12 additions & 2 deletions docs/getting_started.md
Expand Up @@ -43,8 +43,18 @@ Rails.application.routes.draw do
end
```

The routes can be customized to show or hide
different models on the dashboard.
Removing resources from the routes will hide them on the dashboard.

There might be situations where resources shall be hidden in the navigation
but still be nested in another resource. In this case you keep the route but configure the dashboard to not be displayed in the navigation.

```ruby
class LineItemDashboard < Administrate::BaseDashboard
configure do |config|
config.navigation = false
end
end
```

Each `FooDashboard` specifies which attributes should be displayed
on the admin dashboard for the `Foo` resource.
Expand Down
22 changes: 22 additions & 0 deletions lib/administrate/base_dashboard.rb
Expand Up @@ -17,6 +17,20 @@ module Administrate
class BaseDashboard
include Administrate

class << self
def configuration
@configuration ||= Configuration.new
end

def configure
yield configuration
end

def display_in_navigation?
self.configuration.navigation == true
end
end

def attribute_types
self.class::ATTRIBUTE_TYPES
end
Expand Down Expand Up @@ -87,5 +101,13 @@ def attribute_includes(attributes)
key if association_classes.include?(field.try(:deferred_class))
end.compact
end

class Configuration
attr_accessor :navigation

def initialize
@navigation = true
end
end
end
end
1 change: 1 addition & 0 deletions lib/administrate/engine.rb
Expand Up @@ -14,6 +14,7 @@
require "administrate/search"
require "administrate/namespace"
require "administrate/namespace/resource"
require "administrate/navigation"

module Administrate
class Engine < ::Rails::Engine
Expand Down
9 changes: 9 additions & 0 deletions lib/administrate/namespace/resource.rb
Expand Up @@ -3,6 +3,9 @@ class Namespace
class Resource
attr_reader :namespace, :resource

delegate :display_in_navigation?, to: :dashboard_class
delegate :dashboard_class, to: :resource_resolver

def initialize(namespace, resource)
@namespace = namespace
@resource = resource
Expand All @@ -23,6 +26,12 @@ def name
def path
name.to_s.gsub("/", "_")
end

private

def resource_resolver
@resource_resolver ||= ResourceResolver.new("#{namespace}/#{resource}")
end
end
end
end
10 changes: 10 additions & 0 deletions lib/administrate/navigation.rb
@@ -0,0 +1,10 @@
module Administrate
class Navigation
attr_reader :resources

def initialize(namespace)
namespace = Administrate::Namespace.new(namespace)
@resources = namespace.resources.select(&:display_in_navigation?)
end
end
end
25 changes: 24 additions & 1 deletion spec/features/navigation_spec.rb
@@ -1,6 +1,8 @@
require "rails_helper"

describe "navigation" do
let(:navigation) { find(".navigation") }

it "highlights the link to the current page's resource type" do
visit admin_customers_path

Expand All @@ -9,6 +11,28 @@
expect(active_link.text).to eq "Customers"
end

it "displays links to resources from admin namespace" do
visit admin_root_path

expect(navigation).to have_link("Customers")
expect(navigation).to have_link("Line Items")
expect(navigation).to have_link("Log Entries")
expect(navigation).to have_link("Products")
expect(navigation).to have_link("Product Meta Tags")
expect(navigation).to have_link("Payments")
expect(navigation).to have_link("Series")
expect(navigation).to have_link("Blog/Posts")
end

it "does not display links to resources configured to not be rendered in navigation" do
LineItemDashboard.configuration.navigation = false

visit admin_root_path
expect(navigation).not_to have_link("Line Items")

LineItemDashboard.configuration.navigation = true # Reset to default
end

it "displays translated name of model" do
translations = {
activerecord: {
Expand All @@ -24,7 +48,6 @@
with_translations(:en, translations) do
visit admin_customers_path

navigation = find(".navigation")
expect(navigation).to have_link("Users")
expect(page).to have_header("Users")
end
Expand Down

0 comments on commit 540d098

Please sign in to comment.