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.5.15 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.36 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.14 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.35 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.13 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.34 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.12 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.33 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.11 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.10 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.32 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.9 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.31 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.8 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.30 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.7 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.29 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.6 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.28 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.5 app/view_models/workarea/storefront/content_blocks/video_view_model.rb