From 55d23d6914b60c10b2ac0300ea637570c260dfe6 Mon Sep 17 00:00:00 2001 From: Yana Agun Siswanto Date: Sun, 12 Jan 2020 01:35:20 +0700 Subject: [PATCH] _navigation.html.erb: Hide resources without index --- .../administrate/application/_navigation.html.erb | 2 +- lib/administrate/namespace.rb | 15 +++++++++++++-- lib/administrate/namespace/resource.rb | 5 +++-- spec/example_app/config/routes.rb | 2 +- spec/features/navigation_spec.rb | 6 ++++++ 5 files changed, 24 insertions(+), 6 deletions(-) diff --git a/app/views/administrate/application/_navigation.html.erb b/app/views/administrate/application/_navigation.html.erb index e4a40a1771..2cc8c5d21e 100644 --- a/app/views/administrate/application/_navigation.html.erb +++ b/app/views/administrate/application/_navigation.html.erb @@ -15,6 +15,6 @@ as defined by the routes in the `admin/` namespace display_resource_name(resource), [namespace, resource_index_route_key(resource)], class: "navigation__link navigation__link--#{nav_link_state(resource)}" - ) %> + ) if resource.actions.include?("index") %> <% end %> diff --git a/lib/administrate/namespace.rb b/lib/administrate/namespace.rb index 6a86c23e1d..c0cb256bc8 100644 --- a/lib/administrate/namespace.rb +++ b/lib/administrate/namespace.rb @@ -5,9 +5,20 @@ def initialize(namespace) end def resources - @resources ||= routes.map(&:first).uniq.map do |path| - Resource.new(namespace, path) + return @resources if @resources + + _resources = {} + _path = 0 + _action = 1 + routes.each do |route| + if _resources[route[_path]].present? + _resources[route[_path]].actions << route[_action] + else + _resources[route[_path]] = Resource.new(namespace, route[_path], [route[_action]]) + end end + + @resources = _resources.values end def routes diff --git a/lib/administrate/namespace/resource.rb b/lib/administrate/namespace/resource.rb index 19ab0213c7..62b31b3016 100644 --- a/lib/administrate/namespace/resource.rb +++ b/lib/administrate/namespace/resource.rb @@ -1,11 +1,12 @@ module Administrate class Namespace class Resource - attr_reader :namespace, :resource + attr_reader :namespace, :resource, :actions - def initialize(namespace, resource) + def initialize(namespace, resource, actions = []) @namespace = namespace @resource = resource + @actions = Set.new(actions) end def to_s diff --git a/spec/example_app/config/routes.rb b/spec/example_app/config/routes.rb index 8e64fe1e2e..fe4b7cc9ab 100644 --- a/spec/example_app/config/routes.rb +++ b/spec/example_app/config/routes.rb @@ -5,7 +5,7 @@ resources :log_entries resources :orders resources :products - resources :product_meta_tags + resources :product_meta_tags, except: [:index] resources :payments, only: [:index, :show] resources :series diff --git a/spec/features/navigation_spec.rb b/spec/features/navigation_spec.rb index 0358778dea..b55e699bed 100644 --- a/spec/features/navigation_spec.rb +++ b/spec/features/navigation_spec.rb @@ -36,4 +36,10 @@ expect(page).to have_header("Users") end end + + it 'hides link to resources without index page' do + visit admin_customers_path + navigation = find(".navigation") + expect(navigation).not_to have_link("Product Meta Tags") + end end