Generalize breadcrumbs for nested resources

* Make programming groups breadcrumb navigation clickable
This commit is contained in:
kiragrammel
2023-10-22 17:57:05 +02:00
committed by Sebastian Serth
parent bf8f7b471c
commit 5ecde8c8bd
3 changed files with 23 additions and 8 deletions

View File

@ -28,8 +28,8 @@ class ProgrammingGroup < ApplicationRecord
false false
end end
def self.nested_resource? def self.parent_resource
true Exercise
end end
def programming_group? def programming_group?

View File

@ -1,10 +1,19 @@
- model = controller_path.classify.constantize rescue nil - model = controller_path.classify.constantize rescue nil
- if model - if model
- object = model.find_by(id: params[:id]) - object = model.find_by(id: params[:id])
- if model.try(:nested_resource?) - if (parent_model = model.try(:parent_resource))
- root_element = model.model_name.human(count: 2) - parent_route_key = parent_model.model_name.singular_route_key
- if object - if params["#{parent_route_key}_id"].present?
- current_element = object - parent_object = object.try(parent_route_key) || parent_model.find_by(id: params["#{parent_route_key}_id"])
- parent_element = link_to_if(current_user && policy(parent_object).show?, parent_object, send(:"#{parent_route_key}_path", parent_object))
- parent_root_element = link_to_if(current_user && policy(parent_model).index?, parent_model.model_name.human(count: 2), send(:"#{parent_model.model_name.collection}_path"))
- root_element = link_to_if(current_user && policy(model).index?, model.model_name.human(count: 2), send(:"#{parent_route_key}_#{model.model_name.collection}_path", parent_object))
- if object
- current_element = link_to_if(current_user && policy(object).show?, object, send(:"#{parent_route_key}_#{model.model_name.singular}_path", parent_object, object))
- else
- root_element = link_to_if(current_user && policy(model).index?, model.model_name.human(count: 2), send(:"#{model.model_name.collection}_path"))
- if object
- current_element = link_to_if(current_user && policy(object).show?, object, send(:"#{model.model_name.singular}_path", object))
- else - else
- root_element = link_to_if(current_user && policy(model).index?, model.model_name.human(count: 2), send(:"#{model.model_name.collection}_path")) - root_element = link_to_if(current_user && policy(model).index?, model.model_name.human(count: 2), send(:"#{model.model_name.collection}_path"))
- if object - if object
@ -21,6 +30,12 @@
- content_for :breadcrumbs do - content_for :breadcrumbs do
.container.mb-4 .container.mb-4
ul.breadcrumb.bg-body-secondary.px-3.py-2 ul.breadcrumb.bg-body-secondary.px-3.py-2
- if defined?(parent_root_element) && parent_root_element.present?
li.breadcrumb-item.small
= parent_root_element
li.breadcrumb-item.small
= parent_element
- title = "#{parent_object} - #{title}"
- if root_element.present? - if root_element.present?
li.breadcrumb-item.small li.breadcrumb-item.small
= root_element = root_element

View File

@ -11,8 +11,8 @@ class DropErrors < ActiveRecord::Migration[5.2]
validates :message, presence: true validates :message, presence: true
def self.nested_resource? def self.parent_resource
true ExecutionEnvironment
end end
delegate :to_s, to: :id delegate :to_s, to: :id