Extract banner to a component

This commit is contained in:
Javi Martín
2021-01-14 15:19:41 +01:00
parent 885a9ad765
commit 957a73f837
4 changed files with 21 additions and 10 deletions

View File

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

View File

@@ -0,0 +1,16 @@
class Shared::BannerComponent < ApplicationComponent
attr_reader :banner
def initialize(banner)
@banner = banner
end
private
def link
link_to banner.target_url do
tag.h2(banner.title, style: "color:#{banner.font_color}") +
tag.h3(banner.description, style: "color:#{banner.font_color}")
end
end
end

View File

@@ -2,11 +2,4 @@ module BannersHelper
def has_banners? def has_banners?
@banners.present? && @banners.count > 0 @banners.present? && @banners.count > 0
end end
def banner_target_link(banner)
link_to banner.target_url do
tag.h2(banner.title, style: "color:#{banner.font_color}") +
tag.h3(banner.description, style: "color:#{banner.font_color}")
end
end
end end

View File

@@ -1,6 +1,5 @@
<% if has_banners? || controller.class == Admin::BannersController %> <% if has_banners? || controller.class == Admin::BannersController %>
<% banner ||= @banners.sample %> <% banner ||= @banners.sample %>
<div class="banner" style="background-color:<%= banner.background_color %>;">
<%= sanitize banner_target_link(banner), attributes: %w[href style] %> <%= render Shared::BannerComponent.new(banner) %>
</div>
<% end %> <% end %>