From 9f5d8ecf2b47cab181bf585e6213d993346e7ff4 Mon Sep 17 00:00:00 2001 From: Colin Date: Sun, 8 Sep 2024 00:28:39 -0400 Subject: [PATCH] In navbar, if 2+ lines add a link to the line switcher (#3272) * if 2+ lines add a switcher * ha 1+ --- app/helpers/lines_helper.rb | 14 +++++++++++++- test/helpers/lines_helper_test.rb | 23 ++++++++++++++++------- 2 files changed, 29 insertions(+), 8 deletions(-) diff --git a/app/helpers/lines_helper.rb b/app/helpers/lines_helper.rb index 7de38f02f..71b2533b6 100644 --- a/app/helpers/lines_helper.rb +++ b/app/helpers/lines_helper.rb @@ -1,10 +1,22 @@ # Convenience function for displaying lines around the app. module LinesHelper def current_line_display + return if !session[:line_id] + + # If multiple lines, link to the switcher + if Line.count > 1 + return content_tag :li do + link_to t('navigation.current_line.helper') + ": #{current_line.name}", + new_line_path, + class: 'nav-link navbar-text-alt' + end + end + + # Otherwise just display the content content_tag :li do content_tag :span, t('navigation.current_line.helper') + ": #{current_line.name}", class: 'nav-link navbar-text-alt' - end if session[:line_id] + end end def current_line diff --git a/test/helpers/lines_helper_test.rb b/test/helpers/lines_helper_test.rb index 47bcc8a29..2b27e8dbf 100644 --- a/test/helpers/lines_helper_test.rb +++ b/test/helpers/lines_helper_test.rb @@ -4,25 +4,34 @@ class LinesHelperTest < ActionView::TestCase include ERB::Util describe 'convenience methods' do - before do + it 'should return empty if not set' do + assert_nil current_line + assert_nil current_line_display + end + + it 'should show a link if 2+ lines' do @lines = [ create(:line, name: 'DC'), create(:line, name: 'MD'), create(:line, name: 'VA') ] - end - - it 'should return current line' do - assert_nil current_line - assert_nil current_line_display @lines.each do |line| session[:line_id] = line.id session[:line_name] = line.name assert_equal current_line_display, - "
  • Your current line: #{session[:line_name]}
  • " + "
  • Your current line: #{session[:line_name]}
  • " assert_equal current_line, line end end + + it 'should show text if just one line' do + line = create(:line, name: 'DC') + session[:line_id] = line.id + session[:line_name] = line.name + assert_equal current_line_display, + "
  • Your current line: #{session[:line_name]}
  • " + assert_equal current_line, line + end end end