Sha256: 112866ddc4ceaad1f33c8f77c802ca90d7577126bb5ea309e931555c92a76e8f

Contents?: true

Size: 1.27 KB

Versions: 12

Compression:

Stored size: 1.27 KB

Contents

# frozen_string_literal: true

# A mixin for tags that returns the file as their content.
module ComfortableMexicanSofa::Content::Tag::Mixins
  module FileContent

    # @param [ActiveStorage::Blob] file
    # @param ["link", "image", "url"] as
    # @param [{String => String}] variant_attrs ImageMagick variant attributes
    # @param [String] label alt text for `as: "image"`, link text for `as: "link"`
    # @return [String]
    def content(file: self.file, as: self.as, variant_attrs: self.variant_attrs, label: self.label)
      return "" unless file

      url_helpers = Rails.application.routes.url_helpers

      attachment_url =
        if variant_attrs.present? && file.image?
          variant = file.variant(combine_options: variant_attrs)
          url_helpers.rails_representation_path(variant, only_path: true)
        else
          url_helpers.rails_blob_path(file, only_path: true)
        end

      case as
      when "link"
        "<a href='#{attachment_url}'#{html_class_attribute} target='_blank'>#{label}</a>"
      when "image"
        "<img src='#{attachment_url}'#{html_class_attribute} alt='#{label}'/>"
      else
        attachment_url
      end
    end

  private

    def html_class_attribute
      return if @class.blank?
      " class='#{@class}'"
    end

  end
end

Version data entries

12 entries across 12 versions & 6 rubygems

Version Path
comfortable_mexican_sofa-2.0.19 lib/comfortable_mexican_sofa/content/tags/mixins/file_content.rb
PixelForce_ETS-0.0.2 lib/comfortable_mexican_sofa/content/tags/mixins/file_content.rb
PixelForce_ETS-0.0.1 lib/comfortable_mexican_sofa/content/tags/mixins/file_content.rb
kcyEtsTest2-0.0.4 lib/comfortable_mexican_sofa/content/tags/mixins/file_content.rb
kcyEtsTest2-0.0.3 lib/comfortable_mexican_sofa/content/tags/mixins/file_content.rb
kcyEtsTest2-0.0.2 lib/comfortable_mexican_sofa/content/tags/mixins/file_content.rb
kcyEtsTest2-0.0.1 lib/comfortable_mexican_sofa/content/tags/mixins/file_content.rb
kcyEtsTest-0.0.1 lib/comfortable_mexican_sofa/content/tags/mixins/file_content.rb
kcyEtsTest-2.2.19 lib/comfortable_mexican_sofa/content/tags/mixins/file_content.rb
ComfortableMexicanSofa-2.0.18 lib/comfortable_mexican_sofa/content/tags/mixins/file_content.rb
kcy1-1.0 lib/comfortable_mexican_sofa/content/tags/mixins/file_content.rb
comfortable_mexican_sofa-2.0.18 lib/comfortable_mexican_sofa/content/tags/mixins/file_content.rb