From 5ecde8c8bda9371501acf8a9d3c7246e4a8f5400 Mon Sep 17 00:00:00 2001 From: kiragrammel Date: Sun, 22 Oct 2023 17:57:05 +0200 Subject: [PATCH] Generalize breadcrumbs for nested resources * Make programming groups breadcrumb navigation clickable --- app/models/programming_group.rb | 4 ++-- .../_breadcrumbs_and_title.html.slim | 23 +++++++++++++++---- db/migrate/20181129093207_drop_errors.rb | 4 ++-- 3 files changed, 23 insertions(+), 8 deletions(-) diff --git a/app/models/programming_group.rb b/app/models/programming_group.rb index dbec341ff..bd9d4473e 100644 --- a/app/models/programming_group.rb +++ b/app/models/programming_group.rb @@ -28,8 +28,8 @@ def internal_user? false end - def self.nested_resource? - true + def self.parent_resource + Exercise end def programming_group? diff --git a/app/views/application/_breadcrumbs_and_title.html.slim b/app/views/application/_breadcrumbs_and_title.html.slim index 730a54b19..91be5cca4 100644 --- a/app/views/application/_breadcrumbs_and_title.html.slim +++ b/app/views/application/_breadcrumbs_and_title.html.slim @@ -1,10 +1,19 @@ - model = controller_path.classify.constantize rescue nil - if model - object = model.find_by(id: params[:id]) - - if model.try(:nested_resource?) - - root_element = model.model_name.human(count: 2) - - if object - - current_element = object + - if (parent_model = model.try(:parent_resource)) + - parent_route_key = parent_model.model_name.singular_route_key + - if params["#{parent_route_key}_id"].present? + - 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 - root_element = link_to_if(current_user && policy(model).index?, model.model_name.human(count: 2), send(:"#{model.model_name.collection}_path")) - if object @@ -21,6 +30,12 @@ - content_for :breadcrumbs do .container.mb-4 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? li.breadcrumb-item.small = root_element diff --git a/db/migrate/20181129093207_drop_errors.rb b/db/migrate/20181129093207_drop_errors.rb index 213e22e1c..18d62d22b 100644 --- a/db/migrate/20181129093207_drop_errors.rb +++ b/db/migrate/20181129093207_drop_errors.rb @@ -11,8 +11,8 @@ class Error < ApplicationRecord validates :message, presence: true - def self.nested_resource? - true + def self.parent_resource + ExecutionEnvironment end delegate :to_s, to: :id