diff --git a/app/controllers/application_controller.rb b/app/controllers/application_controller.rb index 517c5a8..c7db3bf 100644 --- a/app/controllers/application_controller.rb +++ b/app/controllers/application_controller.rb @@ -548,10 +548,16 @@ class ApplicationController < ActionController::Base @action = action_name.to_sym @result[:first] = {name: current_menu[:name], link: current_menu[:link]} - @result[:second] = current_menu[:elements][current_controller] - if @result[:first][:name] == @result[:second][:name] + if current_menu[:elements][current_controller] + @result[:second] = current_menu[:elements][current_controller] + else @result[:second] = nil end + if @result[:second] + if @result[:first][:name] == @result[:second][:name] + @result[:second] = nil + end + end end end end diff --git a/app/helpers/breadcrumb_helper.rb b/app/helpers/breadcrumb_helper.rb index 84bac8c..2cce85a 100644 --- a/app/helpers/breadcrumb_helper.rb +++ b/app/helpers/breadcrumb_helper.rb @@ -1,29 +1,72 @@ +# DOC : +# ajouter =breadcrumb dans une vue +# pour personaliser le breadcrumb, il y a plusieurs options : +# passer un ou plusieurs parametre piur overrider l'existant par défaut +# :first_title premier element du breadcrumb +# :first_link premier path du breadcrumb +# +# :second_title second element du breadcrumb +# :second_link second path du breadcrumb +# +# :last_element le tout dernier element du breadcrumb +# +# :record est le nom du record pour un show ou un edit par exemple :record => @p_product.name +# +# :default_crud booléen qui active/neutralise la création du titre "Détail de .." ou "Modification .." + + module BreadcrumbHelper - def breadcrumb(options = {}) - options = {action: @action}.merge options + def breadcrumb(opts = {}) + default_opts = { + action: @action, + first_title: @result[:first][:name], + first_link: @result[:first][:link], + default_crud: true + } + + if @result[:second] + default_opts[:second_title] = @result[:second][:name] + default_opts[:second_link] = @result[:second][:link] + end + + options = default_opts.merge opts + capture_haml do haml_tag :h1 do unless @result.present? haml_concat "Erreur dans le BreadcrumbHelper" else - if options[:custom_last_item].present? - haml_concat(link_to @result[:first][:name], @result[:first][:link]) - haml_tag(:span) { haml_concat(link_to @result[:second][:name], @result[:second][:link]) } if @result[:second].present? + if options[:record].present? || options[:last_element].present? + haml_concat(link_to options[:first_title], options[:first_link]) + if options[:second_title].present? && options[:second_link].present? + haml_tag(:span) { haml_concat(link_to options[:second_title], options[:second_title]) } + elsif options[:second_title].present? + haml_tag(:span) {haml_concat( options[:second_title])} + end + else + if options[:second_title].present? + haml_concat(link_to options[:first_title], options[:first_link]) + haml_tag(:span) { haml_concat( options[:second_title]) } + else + haml_concat( options[:first_title]) + end + end + if options[:default_crud] case options[:action] when :new - haml_tag(:span) {haml_concat("Création #{@result[:first][:name].singularize.downcase}")} + haml_tag(:span) {haml_concat("Création #{options[:first_title].singularize.downcase}")} when :edit - haml_tag(:span) {haml_concat('Modification')} + haml_tag(:span) {haml_concat("Modification #{options[:record]}")} when :index - haml_tag(:span) {haml_concat('Liste')} + haml_tag(:span) {haml_concat("Liste")} + when :show + options[:record].present? ? haml_tag(:span) {haml_concat("Détail de #{options[:record]}")} : haml_tag(:span) {haml_concat("Détail")} + else + haml_tag(:span) {haml_concat(options[:record])} if options[:record].present? end - - haml_tag(:span) {haml_concat(options[:custom_last_item])} - else - @result[:second].present? ? haml_concat(link_to @result[:first][:name], @result[:first][:link]) : haml_concat( @result[:first][:name]) - haml_tag(:span) { haml_concat( @result[:second][:name]) } if @result[:second].present? end + haml_tag(:span) { haml_concat( options[:last_element]) } if options[:last_element].present? end end end