Sha256: d9b003359bde2ee2ffeebc635559524c044168251e6bb374c983a283b147b6b0

Contents?: true

Size: 1.73 KB

Versions: 2

Compression:

Stored size: 1.73 KB

Contents

module Alchemy
  # Renders an essence picture view
  class EssencePictureView
    include ActionView::Helpers::AssetTagHelper
    include ActionView::Helpers::UrlHelper
    include Rails.application.routes.url_helpers

    attr_reader :content, :essence, :html_options, :options, :picture

    DEFAULT_OPTIONS = {
      show_caption: true,
      disable_link: false
    }

    def initialize(content, options = {}, html_options = {})
      @content = content
      @options = DEFAULT_OPTIONS.merge(content.settings).merge(options)
      @html_options = html_options
      @essence = content.essence
      @picture = essence.picture
    end

    def render
      return if picture.blank?

      output = caption ? img_tag + caption : img_tag

      if is_linked?
        output = link_to(output, url_for(essence.link), {
          title: essence.link_title.presence,
          target: essence.link_target == "blank" ? "_blank" : nil,
          data: {link_target: essence.link_target.presence}
        })
      end

      if caption
        content_tag(:figure, output, {class: essence.css_class.presence}.merge(html_options))
      else
        output
      end
    end

    private

    def caption
      return unless show_caption?
      @_caption ||= content_tag(:figcaption, essence.caption)
    end

    def img_tag
      @_img_tag ||= image_tag(
        essence.picture_url(options), {
          alt: essence.alt_tag.presence,
          title: essence.title.presence,
          class: caption ? nil : essence.css_class.presence
        }.merge(caption ? {} : html_options)
      )
    end

    def show_caption?
      options[:show_caption] && essence.caption.present?
    end

    def is_linked?
      !options[:disable_link] && essence.link.present?
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
alchemy_cms-3.4.2 app/models/alchemy/essence_picture_view.rb
alchemy_cms-3.4.1 app/models/alchemy/essence_picture_view.rb