From 35aa85d9613a90d9773feab57ce0db20c866f3bc Mon Sep 17 00:00:00 2001 From: Cory Lown Date: Fri, 21 Feb 2025 10:02:32 -0500 Subject: [PATCH] Link masthead title and subtitle to app or exhibit home --- app/assets/stylesheets/modules/layout.scss | 4 +++ .../masthead_title_component.html.erb | 4 +++ app/components/masthead_title_component.rb | 28 +++++++++++++++++++ app/views/shared/_masthead.html.erb | 16 ++--------- .../masthead_title_component_spec.rb | 25 +++++++++++++++++ 5 files changed, 63 insertions(+), 14 deletions(-) create mode 100644 app/components/masthead_title_component.html.erb create mode 100644 app/components/masthead_title_component.rb create mode 100644 spec/components/masthead_title_component_spec.rb diff --git a/app/assets/stylesheets/modules/layout.scss b/app/assets/stylesheets/modules/layout.scss index bb080bde1..a3ce78e5f 100644 --- a/app/assets/stylesheets/modules/layout.scss +++ b/app/assets/stylesheets/modules/layout.scss @@ -47,6 +47,10 @@ } .masthead { + .masthead-title a { + --bs-link-hover-decoration: none; + } + .masthead-navigation { li { --sul-link-font-weight: 700; diff --git a/app/components/masthead_title_component.html.erb b/app/components/masthead_title_component.html.erb new file mode 100644 index 000000000..6fc4174be --- /dev/null +++ b/app/components/masthead_title_component.html.erb @@ -0,0 +1,4 @@ +
+ <%= title %> + <%= subtitle %> +
diff --git a/app/components/masthead_title_component.rb b/app/components/masthead_title_component.rb new file mode 100644 index 000000000..fee18d020 --- /dev/null +++ b/app/components/masthead_title_component.rb @@ -0,0 +1,28 @@ +# frozen_string_literal: true + +# Draws the title in the masthead +class MastheadTitleComponent < ViewComponent::Base + def initialize(title:, subtitle:) + @title = title + @subtitle = subtitle + super + end + + def title + tag.h1 link(@title), class: 'site-title h2' + end + + def subtitle + return unless @subtitle + + tag.small link(@subtitle), class: 'py-2 fs-4' + end + + private + + def link(link_text) + return link_to link_text, helpers.spotlight.exhibit_path(helpers.current_exhibit) if helpers.current_exhibit + + link_to link_text, helpers.spotlight.exhibits_path + end +end diff --git a/app/views/shared/_masthead.html.erb b/app/views/shared/_masthead.html.erb index 648f479ba..55ed9c2ab 100644 --- a/app/views/shared/_masthead.html.erb +++ b/app/views/shared/_masthead.html.erb @@ -6,20 +6,8 @@
-
-

- <% if content_for? :masthead %> - <%= content_for :masthead %> - <% else %> - <%= masthead_heading_content %> - <% end %> -

- - <% if masthead_subheading_content %> - <%= masthead_subheading_content %> - <% end %> -
- + <%= render MastheadTitleComponent.new(title: content_for(:masthead) || masthead_heading_content, subtitle: masthead_subheading_content) %> +
diff --git a/spec/components/masthead_title_component_spec.rb b/spec/components/masthead_title_component_spec.rb new file mode 100644 index 000000000..1ef52047c --- /dev/null +++ b/spec/components/masthead_title_component_spec.rb @@ -0,0 +1,25 @@ +# frozen_string_literal: true + +require 'rails_helper' + +RSpec.describe MastheadTitleComponent, type: :component do + subject(:rendered) do + Capybara::Node::Simple.new(render_inline(described_class.new(title: 'Exhibit Title', + subtitle: 'Exhibit Subtitle')).to_s) + end + + context 'when there is no current exhibit' do + it { expect(rendered).to have_link('Exhibit Title', href: '/') } + it { expect(rendered).to have_link('Exhibit Subtitle', href: '/') } + end + + context 'when there is a current exhibit' do + before do + allow(vc_test_controller).to receive(:current_exhibit) + .and_return(create(:exhibit, slug: 'exhibit-slug')) + end + + it { expect(rendered).to have_link('Exhibit Title', href: '/exhibit-slug') } + it { expect(rendered).to have_link('Exhibit Subtitle', href: '/exhibit-slug') } + end +end