Skip to content

Commit

Permalink
show institution logos (#3642)
Browse files Browse the repository at this point in the history
  • Loading branch information
conorom authored May 10, 2024
1 parent 0744b0a commit 7622043
Show file tree
Hide file tree
Showing 4 changed files with 132 additions and 2 deletions.
5 changes: 5 additions & 0 deletions app/assets/stylesheets/application/heliotrope.scss
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,11 @@ div.skip {
align-items: center;
padding: 1em;
border-bottom: 1px solid #e5e5e5;

.institution-logo {
height: 42px;
margin: 7px 0 7px 0;
}
}

///////////////////////////////////
Expand Down
21 changes: 21 additions & 0 deletions app/models/greensub/institution.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,27 @@ def agent_id
id
end

def logo
# always prefer horizontal logo if set
expected_logo_path = if horizontal_logo.present?
File.join('img', 'institutions', 'horizontal', horizontal_logo)
elsif vertical_logo.present?
File.join('img', 'institutions', 'vertical', vertical_logo)
end

# Plenty of scope for the logo DB entries and actual images to be out-of-sync. Hence all the file checking here.
# The latter are uploaded to the GitHub repo while the former use the Fulcrum dashboard Institutions edit form.
# Maybe down the line we'll use CarrierWave for them, though that usually comes with its own issues.

if expected_logo_path.present? && File.exist?(Rails.root.join('public', expected_logo_path).to_s)
# adds a preceding slash, which is needed to use a public path in views
File.join('', expected_logo_path)
else
Rails.logger.info "Institution logo listed in DB does not exist in public folder: #{expected_logo_path}"
nil
end
end

private

def type
Expand Down
6 changes: 5 additions & 1 deletion app/views/shared/_brand_press_auth.html.erb
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
<% if auth.publisher_restricted_content? %>
<div class="brand-auth">
<% if auth.institution? %>
Provided by <%= auth.institution.display_name %>
<% if auth.institution.logo.present? %>
<%= image_tag auth.institution.logo, class: 'institution-logo', alt: "Logo for #{auth.institution.display_name}" %>
<% else %>
<%= auth.institution.display_name %>
<% end %>
<% else %>
<span>
Get access to more books. <strong><%= link_to "Log in with your institution.",
Expand Down
102 changes: 101 additions & 1 deletion spec/models/greensub/institution_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,18 @@

RSpec.describe Greensub::Institution, type: :model do
context 'instance' do
subject { described_class.new(id: id, identifier: identifier, name: name, display_name: display_name, entity_id: entity_id, in_common: in_common) }
subject { described_class.new(id: id, identifier: identifier, name: name, display_name: display_name,
entity_id: entity_id, in_common: in_common,
horizontal_logo: horizontal_logo, vertical_logo: vertical_logo) }

let(:id) { 1 }
let(:identifier) { 'identifier' }
let(:name) { 'name' }
let(:display_name) { 'display_name' }
let(:entity_id) { 'entity_id' }
let(:in_common) { true }
let(:horizontal_logo) { nil }
let(:vertical_logo) { nil }

it { is_expected.to be_a Greensub::Licensee }
it { expect(subject.agent_type).to eq :Institution }
Expand All @@ -38,6 +42,102 @@
it { expect(subject.shibboleth?).to be false }
end
end

describe '#logo' do
# Plenty of scope for the logo DB entries and actual images to be out-of-sync. Hence all the file checking here.
# The latter are uploaded to the GitHub repo while the former use the Fulcrum dashboard Institutions edit form.
# Maybe down the line we'll use CarrierWave for them, though that usually comes with its own issues.

before do
allow(File).to receive(:exist?).and_call_original
allow(Rails.logger).to receive(:info)
end

context 'neither logo field is set' do
it { expect(subject.logo).to be nil }
end

context 'horizontal_logo is set' do
let(:horizontal_logo) { 'blah_horizontal.png' }
let(:logo_path) { Rails.root.join('public', 'img', 'institutions', 'horizontal', horizontal_logo).to_s }

context 'the file does not exist' do
it { expect(subject.logo).to be nil }
end

context 'the file exists' do
before do
allow(File).to receive(:exist?).with(logo_path).and_return(true)
end

it 'uses the horizontal logo' do
expect(subject.logo).to eq(File.join('', 'img', 'institutions', 'horizontal', horizontal_logo))
end
end
end

context 'vertical_logo is set' do
let(:vertical_logo) { 'blah_vertical.png' }
let(:logo_path) { Rails.root.join('public', 'img', 'institutions', 'vertical', vertical_logo).to_s }

context 'the file does not exist' do
it { expect(subject.logo).to be nil }
end

context 'the file exists' do
before do
allow(File).to receive(:exist?).with(logo_path).and_return(true)
end

it 'uses the vertical logo' do
expect(subject.logo).to eq(File.join('', 'img', 'institutions', 'vertical', vertical_logo))
end
end
end

context 'both horizontal_logo and vertical_logo are set' do
let(:horizontal_logo) { 'blah_horizontal.png' }
let(:vertical_logo) { 'blah_vertical.png' }
let(:horizontal_logo_path) { Rails.root.join('public', 'img', 'institutions', 'horizontal', horizontal_logo).to_s }
let(:vertical_logo_path) { Rails.root.join('public', 'img', 'institutions', 'vertical', vertical_logo).to_s }

context 'neither file exists' do
it { expect(subject.logo).to be nil }
end

context 'the horizontal_logo file exists' do
before do
allow(File).to receive(:exist?).with(horizontal_logo_path).and_return(true)
end

it 'uses the horizontal logo' do
expect(subject.logo).to eq(File.join('', 'img', 'institutions', 'horizontal', horizontal_logo))
end
end

context 'the vertical_logo file exists' do
before do
allow(File).to receive(:exist?).with(vertical_logo_path).and_return(true)
end

it 'returns nil and logs the missing preferred/horizontal logo' do
expect(subject.logo).to be nil
expect(Rails.logger).to have_received(:info).with("Institution logo listed in DB does not exist in public folder: #{File.join('img', 'institutions', 'horizontal', horizontal_logo)}")
end
end

context 'both files exist' do
before do
allow(File).to receive(:exist?).with(horizontal_logo_path).and_return(true)
allow(File).to receive(:exist?).with(vertical_logo_path).and_return(true)
end

it 'uses the preferred horizontal logo' do
expect(subject.logo).to eq(File.join('', 'img', 'institutions', 'horizontal', horizontal_logo))
end
end
end
end
end

context 'validation' do
Expand Down

0 comments on commit 7622043

Please sign in to comment.