-
Notifications
You must be signed in to change notification settings - Fork 26
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2140 from broadinstitute/jb-home-page-link
Adding reusing component for placing link on home page (SCP-5803)
- Loading branch information
Showing
10 changed files
with
173 additions
and
9 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -61,6 +61,7 @@ def index | |
@cell_count = 0 | ||
end | ||
|
||
@home_page_link = HomePageLink.published | ||
end | ||
|
||
def covid | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
%> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |