diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index d83690e..6f0aa6c 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -1,5 +1,8 @@ class ApplicationController < ActionController::Base # Prevent CSRF attacks by raising an exception. # For APIs, you may want to use :null_session instead. protect_from_forgery with: :exception + + attr_writer :breadcrumb + end diff --git a/app/helpers/breadcrumb_helper.rb b/app/helpers/breadcrumb_helper.rb new file mode 100644 index 0000000..d871ac8 --- /dev/null +++ b/app/helpers/breadcrumb_helper.rb @@ -0,0 +1,44 @@ +module BreadcrumbHelper + # Domain + # Domain / example.com + # Domain / example.com / ns1.example.com IN A + # Domain / example.com / new + def breadcrumbs(leaf) + stack = [] + crumbs = [] + stack.push leaf if leaf + + while crumb = stack.pop # rubocop:disable Lint/AssignmentInCondition + case crumb + when Record + if crumb.persisted? + crumbs.push( + name: "#{crumb.name} IN #{crumb.type}", + link: domain_record_path(crumb.domain_id, crumb)) + else + crumbs.push(name: :new) + end + stack.push crumb.domain + when Domain + if crumb.persisted? + crumbs.push(name: crumb.name, link: domain_path(crumb)) + else + crumbs.push(name: :new) + end + end + end + + crumbs.push(name: 'Domains', link: '/') + + crumbs.reverse! + crumbs.each { |c| + # Last element should not be a link + if c == crumbs.last + yield c[:name] + else + yield link_to(c[:name], c[:link]) + end + } + end + +end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 3fdd9a6..2d72ed7 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -1,29 +1,30 @@ Base <%= stylesheet_link_tag 'application', media: 'all' %> <%= javascript_include_tag 'application' %> <%= csrf_meta_tags %> <%= render "shared/nav" %>
<%= flash_messages %> + <%= render "shared/breadcrumbs" %> <%= yield %>
diff --git a/app/views/shared/_breadcrumbs.html.erb b/app/views/shared/_breadcrumbs.html.erb new file mode 100644 index 0000000..690003b --- /dev/null +++ b/app/views/shared/_breadcrumbs.html.erb @@ -0,0 +1,5 @@ +