Skip to content

Commit

Permalink
Merge pull request #2140 from broadinstitute/jb-home-page-link
Browse files Browse the repository at this point in the history
Adding reusing component for placing link on home page (SCP-5803)
  • Loading branch information
bistline authored Sep 25, 2024
2 parents fe15b5d + 0235243 commit 0eaa7e3
Show file tree
Hide file tree
Showing 10 changed files with 173 additions and 9 deletions.
Binary file added app/assets/images/cellarium.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions app/controllers/site_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ def index
@cell_count = 0
end

@home_page_link = HomePageLink.published
end

def covid
Expand Down
8 changes: 7 additions & 1 deletion app/javascript/styles/_brand.scss
Original file line number Diff line number Diff line change
Expand Up @@ -252,11 +252,17 @@
}
}

#whitepaper-btn {
#home-page-link {
position: absolute;
top: 220px;
right: 15px;
z-index: 500;

img {
height: 24px;
object-fit: scale-down;
padding-right: 5px;
}
}

#latest-features-btn {
Expand Down
10 changes: 10 additions & 0 deletions app/javascript/styles/_global.scss
Original file line number Diff line number Diff line change
Expand Up @@ -941,6 +941,16 @@ figcaption.ck-editor__editable {
color: #C45500;
}

.btn-home-link {
color: #fff;
box-shadow: 8px 8px 12px #2b2b2b;
}

.btn-home-link:hover {
color: #fff;
filter: brightness(90%);
}

.link-darker-blue {
color: #1b476d
}
Expand Down
78 changes: 78 additions & 0 deletions app/models/home_page_link.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
class HomePageLink
include Mongoid::Document
include Mongoid::Timestamps

DEFAULT_CSS_CLASS='btn btn-home-link'.freeze
DEFAULT_BG_COLOR='#4999F9'.freeze

field :name, type: String
field :href, type: String
field :tooltip, type: String
field :bg_color, type: String, default: DEFAULT_BG_COLOR
field :css_class, type: String, default: DEFAULT_CSS_CLASS
field :published, type: Mongoid::Boolean, default: false
field :image, type: String

validates :name, :href, presence: true
validate :ensure_one_published_link

def publish!
update(published: true)
end

def unpublish!
update(published: false)
end

def reset_css!
puts "Resetting css_class to '#{DEFAULT_CSS_CLASS}'"
update(css_class: DEFAULT_CSS_CLASS)
end

def reset_bg_color!
puts "Resetting bg_color to '#{DEFAULT_BG_COLOR}'"
update(bg_color: DEFAULT_BG_COLOR)
end

def self.published
self.find_by(published: true)
end

def self.publish_last!
if published
puts "'#{published.name}' is already published"
return false
end

link = last
if link
puts "Publishing '#{link.name}'"
link.publish!
else
puts "Nothing to publish"
end
end

def self.unpublish!
if published.present?
puts "Unpublishing '#{published.name}'"
published.update(published: false)
else
puts "No published links"
return false
end
end

private

def ensure_one_published_link
if published && HomePageLink.where(published: true, :id.ne => self.id).exists?
existing = HomePageLink.published
errors.add(
:published,
"link exists: '#{existing.name}', please unpublish first with HomePageLink.unpublish!"
)
puts errors.full_messages.to_sentence
end
end
end
10 changes: 6 additions & 4 deletions app/views/layouts/application.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,12 @@
&copy; <%= Date.today.year %> The Broad Institute of MIT and Harvard
</div>
<div class="footer-text-block pull-left left-border-0-5">
<%= scp_link_to 'Privacy Policy', privacy_policy_path %>
&nbsp;
<%= scp_link_to "Terms of Service".html_safe, terms_of_service_path %>
&nbsp;
<%= scp_link_to 'Privacy Policy', privacy_policy_path %>&nbsp;
<%= scp_link_to 'Terms of Service', terms_of_service_path %>&nbsp;
<%= scp_link_to 'Portal whitepaper', "https://www.biorxiv.org/content/10.1101/2023.07.13.548886v1",
id: 'whitepaper-btn', data: { toggle: 'tooltip', placement: 'top' },
title: 'Read the Single Cell Portal whitepaper!', target: :_blank %>

<% if @selected_branding_group.present? %>
<%= link_to "<i class='fas fa-chevron-circle-left fa-fw'></i> Return to Single Cell Portal".html_safe, site_path %>
<% end %>
Expand Down
11 changes: 11 additions & 0 deletions app/views/site/_home_page_link.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<% image_link = @home_page_link.image.present? ? "#{image_tag(@home_page_link.image)}" : nil %>
<style>
#home-page-link {
background-color: <%= @home_page_link.bg_color %>;
}
</style>
<%=
link_to "#{image_link}#{@home_page_link.name}".html_safe, @home_page_link.href, title: @home_page_link.tooltip,
data: { toggle: 'tooltip', placement: 'left'}, class: @home_page_link.css_class,
id: 'home-page-link', target: :_blank
%>
7 changes: 3 additions & 4 deletions app/views/site/_main_banner_content.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,9 @@
latest_feature_announcements_path, class: 'btn btn-warning-scp', id: 'latest-features-btn',
data: { toggle: 'tooltip', placement: 'left' }, title: 'Learn about new Single Cell Portal features!' %>
<% end %>
<%= link_to "<span class='badge btn-warning-scp-icon'></span> " \
"Portal whitepaper".html_safe,
"https://www.biorxiv.org/content/10.1101/2023.07.13.548886v1", class: 'btn btn-warning-scp', id: 'whitepaper-btn',
data: { toggle: 'tooltip', placement: 'left' }, title: 'Read the Single Cell Portal whitepaper!', target: :_blank %>
<% if @home_page_link.present? %>
<%= render partial: 'home_page_link' %>
<% end %>
<% if @selected_branding_group.present? %>
<% content_for(:html_title) { "#{@selected_branding_group.name} - Collections - Single Cell Portal" } %>
<% if @selected_branding_group.banner_image.file %>
Expand Down
9 changes: 9 additions & 0 deletions test/controllers/site_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -249,4 +249,13 @@ def teardown
assert @study.publications.empty?
end
end

test 'should show home page link' do
get site_path
assert_select '#home-page-link', 0
HomePageLink.create(name: 'Foo', href: 'http://foo.com', published: true)
get site_path
assert_select '#home-page-link', 1
HomePageLink.delete_all
end
end
48 changes: 48 additions & 0 deletions test/models/home_page_link_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
require "test_helper"

class HomePageLinkTest < ActiveSupport::TestCase

before(:all) do
@home_page_link = HomePageLink.create(name: 'Broad Institute', href: 'https://broadinstitute.org')
end

teardown do
HomePageLink.unpublish!
end

after(:all) do
HomePageLink.delete_all
end

test 'should publish/unpublish link' do
assert_nil HomePageLink.published
HomePageLink.publish_last!
assert HomePageLink.published.present?
link = HomePageLink.published
link.unpublish!
assert_nil HomePageLink.published
link.publish!
assert HomePageLink.published.present?
HomePageLink.unpublish!
assert_nil HomePageLink.published
end

test 'should reset css/color attributes' do
@home_page_link.update(css_class: 'foo', bg_color: '#ff0000')
@home_page_link.reset_css!
@home_page_link.reload
assert_equal HomePageLink::DEFAULT_CSS_CLASS, @home_page_link.css_class
@home_page_link.reset_bg_color!
@home_page_link.reload
assert_equal HomePageLink::DEFAULT_BG_COLOR, @home_page_link.bg_color
end

test 'should ensure only one link is published' do
assert_not HomePageLink.published.present?
HomePageLink.publish_last!
assert HomePageLink.published.present?
new_link = HomePageLink.new(name: 'Google', href: 'https://google.com', published: true)
assert_not new_link.valid?
assert_includes new_link.errors.attribute_names, :published
end
end

0 comments on commit 0eaa7e3

Please sign in to comment.