Skip to content

Commit

Permalink
Refactor tab_item helper to support testing and selection of child ro…
Browse files Browse the repository at this point in the history
…utes
  • Loading branch information
asalant committed Jan 22, 2020
1 parent b75b4a8 commit 6ba1967
Show file tree
Hide file tree
Showing 5 changed files with 33 additions and 15 deletions.
4 changes: 1 addition & 3 deletions app/controllers/visits_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,7 @@ def destroy
end

def today
today = Date.today
params.merge! :year => today.year, :month => today.month, :day => today.day
day
redirect_to today_visits_path(:organization_key => @organization.key)
end

def day
Expand Down
13 changes: 7 additions & 6 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,14 +30,15 @@ def time_ago_in_words(from_time, include_seconds=false)
distance_of_time_in_words(from_time, Time.zone.now, include_seconds)
end

def tab_item(label, path)
if label === 'Home'
"<li class='#{request.path == path ? 'selected' : ''}'><a href='#{path}'>#{label}</a></li>"
elsif label === 'Visits'
"<li class='#{request.path.include?('/visits/') ? 'selected' : ''}'><a href='#{path}'>#{label}</a></li>"
def tab_item(label, tab_path, request_path, params = { :select_children => false})
selected = false
if params[:select_children]
selected = request_path.start_with?(tab_path)
else
"<li class='#{request.path.start_with?(path) ? 'selected' : ''}'><a href='#{path}'>#{label}</a></li>"
selected = tab_path == request_path
end
puts label, tab_path, request_path, params, selected
"<li class='#{selected ? 'selected' : ''}'><a href='#{tab_path}'>#{label}</a></li>"
end

def today_visits_path(params={})
Expand Down
12 changes: 6 additions & 6 deletions app/views/layouts/application.html.haml
Original file line number Diff line number Diff line change
Expand Up @@ -46,15 +46,15 @@
%div#application_nav.flex-column
%ul.tabs
-if @organization && [email protected]_record?
= tab_item('Home', organization_path(@organization))
= tab_item('Visits', today_visits_path(:organization_key => @organization.key))
= tab_item('Reports', report_path(:action => 'index', :organization_key => @organization.key))
= tab_item('Home', organization_path(@organization), request.path, )
= tab_item('Visits', default_visits_path(:organization_key => @organization.key), request.path, :select_children => true)
= tab_item('Reports', report_path(:action => 'index', :organization_key => @organization.key), request.path, :select_children => true)
-if permit? "admin or (manager of :organization)"
=tab_item('Settings', edit_organization_path(@organization))
=tab_item('Settings', edit_organization_path(@organization), request.path)
-else
=tab_item('Home', root_path)
=tab_item('Home', root_path, request.path)
-if logged_in? && !current_user.organization.nil?
= tab_item(current_user.organization.name, organization_path(current_user.organization))
= tab_item(current_user.organization.name, organization_path(current_user.organization), request.path)
%div.content#application_body.flex-container
= yield
%div{:style => 'clear:both;'}
Expand Down
3 changes: 3 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,9 @@
:controller => 'visits', :action => 'day',
:requirements => {:year => /\d{4}/, :day => /\d{1,2}/, :month => /\d{1,2}/}

map.default_visits ':organization_key/visits',
:controller => 'visits', :action => 'today'

map.reports ':organization_key/reports', :controller => 'reports', :action => 'index'
map.report ':organization_key/reports/:action', :controller => 'reports'

Expand Down
16 changes: 16 additions & 0 deletions test/functional/application_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
require 'test_helper'

class ApplicationHelperTest < ActiveSupport::TestCase

def test_tab_item
assert_match /selected/, tab_item('Home', '/', '/')
assert_match /selected/, tab_item('Home', '/sfbk', '/sfbk')
assert_no_match /selected/, tab_item('Home', '/sfbk', '/sfbk/edit')
assert_match /selected/, tab_item('Visits', '/sfbk/visits', '/sfbk/visits', :select_children => true)
assert_match /selected/, tab_item('Visits', '/sfbk/visits', '/sfbk/visits/2020/1/21', :select_children => true)
assert_match /selected/, tab_item('Reports', '/sfbk/reports', '/sfbk/reports', :select_children => true)
assert_match /selected/, tab_item('Reports', '/sfbk/reports', '/sfbk/reports/visits', :select_children => true)
assert_match /selected/, tab_item('Settings', '/sfbk/edit', '/sfbk/edit')
assert_no_match /selected/, tab_item('Settings', '/sfbk', '/sfbk/edit')
end
end

0 comments on commit 6ba1967

Please sign in to comment.