Extract banner to a component
This commit is contained in:
3
app/components/shared/banner_component.html.erb
Normal file
3
app/components/shared/banner_component.html.erb
Normal file
@@ -0,0 +1,3 @@
|
||||
<div class="banner" style="background-color:<%= banner.background_color %>;">
|
||||
<%= sanitize link, attributes: %w[href style] %>
|
||||
</div>
|
||||
16
app/components/shared/banner_component.rb
Normal file
16
app/components/shared/banner_component.rb
Normal 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
|
||||
@@ -2,11 +2,4 @@ module BannersHelper
|
||||
def has_banners?
|
||||
@banners.present? && @banners.count > 0
|
||||
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
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
<% if has_banners? || controller.class == Admin::BannersController %>
|
||||
<% banner ||= @banners.sample %>
|
||||
<div class="banner" style="background-color:<%= banner.background_color %>;">
|
||||
<%= sanitize banner_target_link(banner), attributes: %w[href style] %>
|
||||
</div>
|
||||
|
||||
<%= render Shared::BannerComponent.new(banner) %>
|
||||
<% end %>
|
||||
|
||||
Reference in New Issue
Block a user