baarkerlounger
3 years ago
3 changed files with 100 additions and 24 deletions
@ -1,43 +1,33 @@
|
||||
module NavigationItemsHelper |
||||
NavigationItem = Struct.new(:text, :href, :current, :classes) |
||||
|
||||
def primary_items(current_user, user) |
||||
def primary_items(path, current_user) |
||||
if current_user.support? |
||||
[ |
||||
NavigationItem.new("Organisations", organisations_path, organisation_current?), |
||||
NavigationItem.new("Users", users_path, users_current?(current_user, user)), |
||||
NavigationItem.new("Logs", case_logs_path, logs_current?), |
||||
NavigationItem.new("Organisations", organisations_path, organisation_current?(path)), |
||||
NavigationItem.new("Users", "/users", users_current?(path)), |
||||
NavigationItem.new("Logs", case_logs_path, logs_current?(path)), |
||||
] |
||||
else |
||||
[ |
||||
NavigationItem.new("Logs", case_logs_path, logs_current?), |
||||
NavigationItem.new("Users", users_organisation_path(current_user.organisation), users_current?(current_user, user)), |
||||
NavigationItem.new("About your organisation", "/organisations/#{current_user.organisation.id}", organisation_current?), |
||||
NavigationItem.new("Logs", case_logs_path, logs_current?(path)), |
||||
NavigationItem.new("Users", users_organisation_path(current_user.organisation), users_current?(path)), |
||||
NavigationItem.new("About your organisation", "/organisations/#{current_user.organisation.id}", organisation_current?(path)), |
||||
] |
||||
end |
||||
end |
||||
|
||||
private |
||||
|
||||
def current?(current_controller, controllers) |
||||
current_controller.controller_name.in?(Array.wrap(controllers)) |
||||
def logs_current?(path) |
||||
path.include?("/logs") |
||||
end |
||||
|
||||
def current_action?(current_controller, action) |
||||
current_controller.action_name == action |
||||
def users_current?(path) |
||||
path.include?("/users") |
||||
end |
||||
|
||||
def logs_current? |
||||
current?(controller, %w[case_logs form]) |
||||
end |
||||
|
||||
def users_current?(current_user, user) |
||||
return false if current_user == user |
||||
|
||||
current?(controller, %w[users]) || current_action?(controller, "users") |
||||
end |
||||
|
||||
def organisation_current? |
||||
current?(controller, %w[organisations]) && !current_action?(controller, "users") |
||||
def organisation_current?(path) |
||||
path.include?("/organisations") && !path.include?("/users") |
||||
end |
||||
end |
||||
|
@ -0,0 +1,86 @@
|
||||
require "rails_helper" |
||||
|
||||
RSpec.describe NavigationItemsHelper do |
||||
let(:current_user) { FactoryBot.create(:user, :data_coordinator) } |
||||
|
||||
let(:users_path) { "/organisations/#{current_user.organisation.id}/users" } |
||||
let(:organisation_path) { "/organisations/#{current_user.organisation.id}" } |
||||
|
||||
describe "#primary items" do |
||||
context "when the user is a data coordinator" do |
||||
context "when the user is on the users page" do |
||||
let(:expected_navigation_items) do |
||||
[ |
||||
NavigationItemsHelper::NavigationItem.new("Logs", "/logs", false), |
||||
NavigationItemsHelper::NavigationItem.new("Users", users_path, true), |
||||
NavigationItemsHelper::NavigationItem.new("About your organisation", organisation_path, false), |
||||
] |
||||
end |
||||
|
||||
it "returns navigation items with the users item set as current" do |
||||
expect(primary_items(users_path, current_user)).to eq(expected_navigation_items) |
||||
end |
||||
end |
||||
|
||||
context "when the user is on their organisation details page" do |
||||
let(:expected_navigation_items) do |
||||
[ |
||||
NavigationItemsHelper::NavigationItem.new("Logs", "/logs", false), |
||||
NavigationItemsHelper::NavigationItem.new("Users", users_path, false), |
||||
NavigationItemsHelper::NavigationItem.new("About your organisation", organisation_path, true), |
||||
] |
||||
end |
||||
|
||||
it "returns navigation items with the users item set as current" do |
||||
expect(primary_items("#{organisation_path}/details", current_user)).to eq(expected_navigation_items) |
||||
end |
||||
end |
||||
|
||||
context "when the user is on the account page" do |
||||
let(:expected_navigation_items) do |
||||
[ |
||||
NavigationItemsHelper::NavigationItem.new("Logs", "/logs", false), |
||||
NavigationItemsHelper::NavigationItem.new("Users", "/organisations/#{current_user.organisation.id}/users", false), |
||||
NavigationItemsHelper::NavigationItem.new("About your organisation", organisation_path, false), |
||||
] |
||||
end |
||||
|
||||
it "returns navigation items with the users item set as current" do |
||||
expect(primary_items("/account", current_user)).to eq(expected_navigation_items) |
||||
end |
||||
end |
||||
end |
||||
|
||||
context "when the user is a support user" do |
||||
let(:current_user) { FactoryBot.create(:user, :support) } |
||||
|
||||
context "when the user is on the users page" do |
||||
let(:expected_navigation_items) do |
||||
[ |
||||
NavigationItemsHelper::NavigationItem.new("Organisations", "/organisations", false), |
||||
NavigationItemsHelper::NavigationItem.new("Users", "/users", true), |
||||
NavigationItemsHelper::NavigationItem.new("Logs", "/logs", false), |
||||
] |
||||
end |
||||
|
||||
it "returns navigation items with the users item set as current" do |
||||
expect(primary_items("/users", current_user)).to eq(expected_navigation_items) |
||||
end |
||||
end |
||||
|
||||
context "when the user is on the account page" do |
||||
let(:expected_navigation_items) do |
||||
[ |
||||
NavigationItemsHelper::NavigationItem.new("Organisations", "/organisations", false), |
||||
NavigationItemsHelper::NavigationItem.new("Users", "/users", false), |
||||
NavigationItemsHelper::NavigationItem.new("Logs", "/logs", false), |
||||
] |
||||
end |
||||
|
||||
it "returns navigation items with the users item set as current" do |
||||
expect(primary_items("/account", current_user)).to eq(expected_navigation_items) |
||||
end |
||||
end |
||||
end |
||||
end |
||||
end |
Loading…
Reference in new issue