Simplify conditions to render a banner

This commit is contained in:
Javi Martín
2021-01-14 15:22:20 +01:00
parent 957a73f837
commit c3147a1bb6
4 changed files with 22 additions and 12 deletions

View File

@@ -1,3 +1,5 @@
<div class="banner" style="background-color:<%= banner.background_color %>;"> <% if banner %>
<%= sanitize link, attributes: %w[href style] %> <div class="banner" style="background-color:<%= banner.background_color %>;">
</div> <%= sanitize link, attributes: %w[href style] %>
</div>
<% end %>

View File

@@ -1,5 +0,0 @@
module BannersHelper
def has_banners?
@banners.present? && @banners.count > 0
end
end

View File

@@ -1,5 +1,3 @@
<% if has_banners? || controller.class == Admin::BannersController %> <% banner ||= @banners&.sample %>
<% banner ||= @banners.sample %>
<%= render Shared::BannerComponent.new(banner) %> <%= render Shared::BannerComponent.new(banner) %>
<% end %>

View File

@@ -0,0 +1,15 @@
require "rails_helper"
describe Shared::BannerComponent, type: :component do
it "renders given a banner" do
render_inline Shared::BannerComponent.new(create(:banner, title: "Vote now!"))
expect(page.find(".banner")).to have_content "Vote now!"
end
it "does not render anything given nil" do
render_inline Shared::BannerComponent.new(nil)
expect(page).not_to have_css ".banner"
end
end