Skip to content

Nested Bradcrumb Structure

Andreas Dausenau edited this page Jun 27, 2022 · 1 revision

Nested Bradcrumb Structure

As described in layout ez-on-rails uses load to render breadcrumbs.

Loaf uses a breadcrumb hook to define the breadcrumbs for controllers and actions. This is ez to handle for default controllers and actions. But if you use some more modular structure in your application and organize your scaffolds in namespaces, this can result in a large amount of copy pasta code. This is because you need to define the breadcrumb for your model entry point in every controller that is located in the namespace.

Consider you have a namespace Content and you have a model Content::Article and Content::BlogPost. If you want to provide an action that is called if the user calls just the content path, you have to provide the breadcrumb to this path in the Articles and the BlogPost controller.

This is especially a case if you make use of the dashboards that provide among others some ability to have an overview over this namespaces actions. Consider you have a Content::DashboardController and want to have the breadcrumb to its index action in the Articles and BlogPost Actions, you need to copy the Breadcrumb call to both controllers.

We suggest to make use of a workaround using inheritance. As mentioned in the layouts page, the application controller of ez-on-rails already sets the breadcrumb to the root path. Hence, every controller that inherits from the EzOnRails::ApplicationController gets as first breadcrumb the root_path of your application. We can make use of this and add more inheritance levels.

If you create an abstract ContentController that inherits from EzOnRails::ApplicationController, you can place some breadcrumb to the content dashboard here. The Articles- and BlogPost Controller just need to inherit from this controller. An thats it, you have now the breadcrumb to the content dashboard for every controller in the Content namespace that inherits from the ContentController.

Note that ez-on-rails distincts between EzOnRails::ApplicationController and EzOnRails::ResourceController__. But the resource controller inherits from the application controller. But since we only can have single inheritance, we need to define both controllers. We have to define ContentResourceController that inherits from the resource controller and ContentController that inherits from the application controller, if we have some controllers with and without underlying resources. But having one to two sources of reused code is much better than having n sources.

Example:

class ContentController < EzOnRails::ResourceController
  breadcrumb 'Content Dashboard', { controller: '/content/dashboard', action: 'index' } 
end

...

class Content::ArticlesController < ContentController
   breadcrumb 'Articles', { controller: '/content/articles', action: 'index' } 
end

...

class Content::BlogPostsController < ContentController
   breadcrumb 'BlogPosts', { controller: '/content/articles', action: 'index' } 
end
Clone this wiki locally