module SpudBannersHelper
# Returns banner html for a given identifier
# Generated HTML will be cached if your application uses fragment caching
#
# * set_or_identifier: A reference to a SpudBannerSet, or the name of a known banner set passed as a symbol (ie, :home)
# * options:
# - limit: Max number of banners to return
# - background_image: Pass true to return the banners as CSS background-image properties rather than tags
#
def spud_banners_for_set(set_or_identifier, options = {})
banner_set = if set_or_identifier.is_a?(SpudBannerSet)
set_or_identifier
else
SpudBannerSet.find_with_identifier(set_or_identifier)
end
return '' if banner_set.blank?
cache(banner_set) do
concat spud_banners_set_tag(banner_set, options)
end
end
# Returns banner html for a given banner_set
#
# * banner_set: A reference to a SpudBannerSet
# * options:
# - limit: Max number of banners to return
# - background_image: Pass true to return the banners as CSS background-image properties rather than tags
#
def spud_banners_set_tag(banner_set, options = {})
background_image = options.delete(:background_image)
limit = options.delete(:limit) || false
banners_query = banner_set.banners.active.limit(limit)
if block_given?
banners_query.each do |banner|
yield(banner)
end
else
content_tag(:div, :class => "spud-banner-set #{options[:wrapper_class]}", 'data-id' => banner_set.id) do
banners_query.map do |banner|
if background_image
concat(content_tag(:div, :class => "spud-banner #{options[:banner_class]}", 'data-id' => banner.id, :style => "background-image:url('#{banner.banner.url(:banner)}');") do
if banner.rich_text
content_tag :div, raw(banner.rich_text), class: 'spud-banner-rich-text'
end
end)
else
concat(content_tag(:div, :class => "spud-banner #{options[:banner_class]}", 'data-id' => banner.id) { spud_banner_tag(banner, options) })
end
end
end
end
end
# Returns an HTML tag for a single banner
# May return either an or an depending on the banner
#
def spud_banner_tag(banner, _options = {})
if banner.link_to.blank?
spud_banner_image_tag(banner)
else
link_to(banner.link_to, target: banner.link_target) do
spud_banner_image_tag(banner)
end
end
end
# Returns an HTML tag for a single banner
#
def spud_banner_image_tag(banner)
image_tag(banner.banner.url(:banner), alt: banner.alt, title: banner.title)
end
end