Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Ensure that subroutes also display the selected nav, take 2 #54

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
10 changes: 8 additions & 2 deletions app/helpers/application_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,14 @@ 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)
"<li class='#{request.path == path ? '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
selected = tab_path == request_path
end
"<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
5 changes: 5 additions & 0 deletions test/functional/visits_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -116,4 +116,9 @@ def test_visits_for_day
assert_equal 0, assigns(:groups)[:volunteers].size
assert_equal 2, assigns(:groups)[:patrons].size
end

def test_visits_for_today
get :today, :organization_key => 'sfbk'
assert_redirected_to today_visits_path(:organization_key => 'sfbk')
end
end