Skip to content

Commit

Permalink
Merge pull request #968 from gamecat2d/967-displaying-navbar
Browse files Browse the repository at this point in the history
967: displaying the navbar conditionnaly
  • Loading branch information
h-m-m authored May 29, 2021
2 parents c5b0ae1 + e9d6f9f commit 6403402
Show file tree
Hide file tree
Showing 10 changed files with 52 additions and 37 deletions.
6 changes: 0 additions & 6 deletions app/controllers/announcements_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
class AnnouncementsController < ApplicationController
skip_before_action :authenticate_user!, only: %i[index show new create]

layout :determine_layout, only: %i[new show]

def index
@announcements = policy_scope(Announcement).order(created_at: :desc)
respond_to do |format|
Expand Down Expand Up @@ -54,8 +52,4 @@ def redirect_after_create
redirect_to thank_you_path, notice: notice
end
end

def determine_layout
'without_navbar' unless context.system_settings.display_navbar?
end
end
2 changes: 0 additions & 2 deletions app/controllers/asks_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

class AsksController < PublicController
layout :determine_layout, only: %i[new create]

def index
redirect_to contributions_path
end
Expand Down
6 changes: 0 additions & 6 deletions app/controllers/community_resources_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@
class CommunityResourcesController < ApplicationController
skip_before_action :authenticate_user!, only: %i[index show new create]

layout :determine_layout, only: %i[new show]

def index
@community_resources = policy_scope(CommunityResource).includes(:organization).order(created_at: :desc)
end
Expand Down Expand Up @@ -57,10 +55,6 @@ def service_areas
@service_areas ||= ServiceArea.i18n.pluck(:name, :id) || []
end

def determine_layout
'without_navbar' unless context.system_settings.display_navbar?
end

def redirect_after_create
notice = "Community resource was successfully submitted."
if context.can_admin?
Expand Down
2 changes: 0 additions & 2 deletions app/controllers/offers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

class OffersController < PublicController
layout :determine_layout, only: %i[new create]

def index
redirect_to contributions_path
end
Expand Down
8 changes: 0 additions & 8 deletions app/controllers/public_pages_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

# FIXME: Extract actions into separate controllers or consolidate with existing ones
class PublicPagesController < PublicController
layout :determine_layout

def about
@about_us_text = HtmlSanitizer.new(@system_setting.about_us_text).sanitize
end
Expand All @@ -27,10 +25,4 @@ def version
color: 'blue'
}
end

private

def determine_layout
'without_navbar' unless @system_setting.display_navbar?
end
end
2 changes: 0 additions & 2 deletions app/controllers/thank_you_controller.rb
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
# frozen_string_literal: true

class ThankYouController < PublicController
layout 'without_navbar'

def show
@organization = context.host_organization
@system_setting = context.system_settings
Expand Down
18 changes: 18 additions & 0 deletions app/lib/navbar_visibility.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
module NavbarVisibility
NAVBAR_HIDEABLE = {
'announcements' => ['new', 'create'],
'asks' => ['new', 'create'],
'community_ressources' => ['new', 'show'],
'offers' => ['new', 'create'],
'public_pages' => ['about', 'contributions', 'landing_page', 'version'],
'thank_you' => ['show']
}

def self.shown?(controller, action, display_navbar)
display_navbar || !hidden?(controller, action)
end

def self.hidden?(controller, action)
NAVBAR_HIDEABLE[controller]&.include?(action)
end
end
4 changes: 3 additions & 1 deletion app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,9 @@
<%= render "layouts/head" %>

<body>
<%= render "layouts/navbar" %>
<% if NavbarVisibility.shown?(controller_name, action_name, context.system_settings.display_navbar?) %>
<%= render "layouts/navbar" %>
<% end %>
<%= render "layouts/notices_and_alerts" %>
<%= render "layouts/main" %>
</body>
Expand Down
10 changes: 0 additions & 10 deletions app/views/layouts/without_navbar.html.erb

This file was deleted.

31 changes: 31 additions & 0 deletions spec/lib/navbar_visibility_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
require 'rails_helper'

RSpec.describe NavbarVisibility do
context "when the settings are 'on' to displaying the navbar" do
let(:display_navbar) { true }

it "should return true for the pages where the navbar can be hidden" do
subject::NAVBAR_HIDEABLE.each do |controller, actions|
actions.each { |action| expect(subject.shown?(controller, action, display_navbar)).to eq(true) }
end
end

it "should return true for the pages not concerned by the displaying option" do
expect(subject.shown?('always_displayed_controller', 'index', display_navbar))
end
end

context "when the settings are 'off' to displaying the navbar" do
let(:display_navbar) { false }

it "should return false for the pages where the navbar can be hidden" do
subject::NAVBAR_HIDEABLE.each do |controller, actions|
actions.each { |action| expect(subject.shown?(controller, action, display_navbar)).to eq(false) }
end
end

it "should return true for the pages not concerned by the displaying option" do
expect(subject.shown?('always_displayed_controller', 'index', display_navbar))
end
end
end

0 comments on commit 6403402

Please sign in to comment.