Sha256: 86b8cac2d227fb89e7fc726fa3102207b773eca420c054ed19c713edf16b9828

Contents?: true

Size: 1.29 KB

Versions: 62

Compression:

Stored size: 1.29 KB

Contents

module Workarea
  module Storefront
    module ContentBlocks
      class VideoViewModel < ContentBlockViewModel
        def locals
          super.merge(
            frame_styles: frame_styles,
            embed: embed
          )
        end

        def frame_styles
          aspect_ratio.present? ? "padding-bottom: #{aspect_ratio}%; height: 0;" : nil
        end

        def embed
          raw_embed
            .gsub(
              /<iframe/,
              "<iframe title='#{t('workarea.storefront.content_blocks.video', url: video_url)}'"
            )
            .gsub(/ frameborder=["'][^"']*["']/, '')
        end

        private

        def aspect_ratio
          return nil if width.nil? || height.nil?
          height / width * 100
        end

        def width
          find_dimension(/width=["'](\d+)["']/)
        end

        def height
          find_dimension(/height=["'](\d+)["']/)
        end

        def find_dimension(regex)
          results = raw_embed.match(regex)
          if results.present?
            value = results.captures[0].to_f
            value > 0 ? value : nil
          end
        end

        def raw_embed
          model.data['embed'] || ''
        end

        def video_url
          raw_embed.match(/https?:\/\/[\S]+/)[0]
        end
      end
    end
  end
end

Version data entries

62 entries across 62 versions & 1 rubygems

Version Path
workarea-storefront-3.4.13 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.12 app/view_models/workarea/storefront/content_blocks/video_view_model.rb