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.27 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.26 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.45 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.25 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.23 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.44 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.22 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.43 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.21 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.42 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.20 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.41 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.19 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.40 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.18 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.39 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.17 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.38 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.5.16 app/view_models/workarea/storefront/content_blocks/video_view_model.rb
workarea-storefront-3.4.37 app/view_models/workarea/storefront/content_blocks/video_view_model.rb