Skip to content

Commit

Permalink
_navigation.html.erb: Hide resources without index
Browse files Browse the repository at this point in the history
  • Loading branch information
bekicot committed Jan 11, 2020
1 parent 8fe64e0 commit 55d23d6
Show file tree
Hide file tree
Showing 5 changed files with 24 additions and 6 deletions.
2 changes: 1 addition & 1 deletion app/views/administrate/application/_navigation.html.erb
Expand Up @@ -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 %>
</nav>
15 changes: 13 additions & 2 deletions lib/administrate/namespace.rb
Expand Up @@ -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
Expand Down
5 changes: 3 additions & 2 deletions 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
Expand Down
2 changes: 1 addition & 1 deletion spec/example_app/config/routes.rb
Expand Up @@ -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

Expand Down
6 changes: 6 additions & 0 deletions spec/features/navigation_spec.rb
Expand Up @@ -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

0 comments on commit 55d23d6

Please sign in to comment.