Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add dashboard configuration to (not) be displayed in navigation #1418

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What would you think of making this name more abstract or generic? Something like RelationalRecordsDashboard or AssociationsDashboard ? I found myself wondering if LineItem had any significance, and am curious to know if anyone else paused at that.

Thanks for working towards an enhancement to administrate!

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
robinboening marked this conversation as resolved.
Show resolved Hide resolved
@configuration ||= Configuration.new
end

def configure
yield configuration
end

def display_in_navigation?
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
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Metrics/LineLength: Line is too long. [89/80]

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