Skip to content

Commit

Permalink
In navbar, if 2+ lines add a link to the line switcher (#3272)
Browse files Browse the repository at this point in the history
* if 2+ lines add a switcher

* ha 1+
  • Loading branch information
colinxfleming authored Sep 8, 2024
1 parent 30d0fbc commit 9f5d8ec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 8 deletions.
14 changes: 13 additions & 1 deletion app/helpers/lines_helper.rb
Original file line number Diff line number Diff line change
@@ -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
Expand Down
23 changes: 16 additions & 7 deletions test/helpers/lines_helper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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,
"<li><span class=\"nav-link navbar-text-alt\">Your current line: #{session[:line_name]}</span></li>"
"<li><a class=\"nav-link navbar-text-alt\" href=\"/lines/new\">Your current line: #{session[:line_name]}</a></li>"
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,
"<li><span class=\"nav-link navbar-text-alt\">Your current line: #{session[:line_name]}</span></li>"
assert_equal current_line, line
end
end
end

0 comments on commit 9f5d8ec

Please sign in to comment.