Skip to content

armgit / spree

Public Imported from GitHub
0

Open Source Platform for DTC, B2B Commerce, Marketplaces & Omnichannel. REST APIs, TypeScript SDKs, and production-ready Next.js storefront. Self-host it. Own your data. No vendor lock-in. Zero platform fees.

Fix embedding videos in the Video Page Section (#12467)

Łukasz Adamczyk6555b6d87c3f
diff --git a/admin/lib/spree/admin.rb b/admin/lib/spree/admin.rb
index de6a3e82db..757cc868b4 100644
--- a/admin/lib/spree/admin.rb
+++ b/admin/lib/spree/admin.rb
@@ -19,7 +19,6 @@ require 'local_time'
require 'payment_icons'
require 'dartsass-rails'
require 'tinymce-rails'
-require 'oembed'
require 'spree/admin/action_callbacks'
require 'spree/admin/callbacks'
diff --git a/admin/spree_admin.gemspec b/admin/spree_admin.gemspec
index 60429a06fd..b81957b1e9 100644
--- a/admin/spree_admin.gemspec
+++ b/admin/spree_admin.gemspec
@@ -46,5 +46,4 @@ Gem::Specification.new do |s|
s.add_dependency 'stimulus-rails'
s.add_dependency 'sprockets', '>= 4.0'
s.add_dependency 'tinymce-rails', '~> 7.0'
- s.add_dependency 'ruby-oembed', '~> 0.18'
end
diff --git a/core/app/models/spree/page_sections/video.rb b/core/app/models/spree/page_sections/video.rb
index 0e5539023a..6746bb10f0 100644
--- a/core/app/models/spree/page_sections/video.rb
+++ b/core/app/models/spree/page_sections/video.rb
@@ -3,6 +3,9 @@ module Spree
class Video < Spree::PageSection
alias video asset
+ include Spree::MemoizedData
+ MEMOIZED_METHODS = %w(video_embed)
+
after_create :create_video_embed, if: -> { preferred_youtube_video_url.present? }
after_update :update_video_embed, if: -> { video_url_changed? || video_embed.nil? }
after_destroy :destroy_video_embed
@@ -68,7 +71,6 @@ module Spree
embed.url = preferred_youtube_video_url
embed.raw_html = oembed_response.html
embed.thumbnail_url = oembed_response.thumbnail_url
- embed.raw_data = oembed_response.fields
embed.save
update(preferred_youtube_video_embed_id: embed.id) if embed.persisted?
diff --git a/admin/config/initializers/oembed.rb b/core/config/initializers/oembed.rb
similarity index 100%
rename from admin/config/initializers/oembed.rb
rename to core/config/initializers/oembed.rb
diff --git a/core/lib/spree/core.rb b/core/lib/spree/core.rb
index 98c6a637ba..ca64a75a3c 100644
--- a/core/lib/spree/core.rb
+++ b/core/lib/spree/core.rb
@@ -31,6 +31,7 @@ require 'activerecord-typedstore'
require 'request_store'
require 'wannabe_bool'
require 'geocoder'
+require 'oembed'
# This is required because ActiveModel::Validations#invalid? conflicts with the
# invalid state of a Payment. In the future this should be removed.
diff --git a/core/lib/spree/testing_support/factories/page_section_factory.rb b/core/lib/spree/testing_support/factories/page_section_factory.rb
index 430cfda120..694fd7901e 100644
--- a/core/lib/spree/testing_support/factories/page_section_factory.rb
+++ b/core/lib/spree/testing_support/factories/page_section_factory.rb
@@ -18,5 +18,7 @@ FactoryBot.define do
factory :rich_text_page_section, class: Spree::PageSections::RichText
factory :newsletter_page_section, class: Spree::PageSections::Newsletter
+
+ factory :video_page_section, class: Spree::PageSections::Video
end
end
diff --git a/core/spec/models/spree/page_sections/video_spec.rb b/core/spec/models/spree/page_sections/video_spec.rb
new file mode 100644
index 0000000000..d85c1f276f
--- /dev/null
+++ b/core/spec/models/spree/page_sections/video_spec.rb
@@ -0,0 +1,135 @@
+require 'spec_helper'
+
+RSpec.describe Spree::PageSections::Video do
+ let(:valid_oembed_response) do
+ double(
+ OEmbed::Response::Video,
+ fields: {
+ 'title' => 'RailsConf 2020 CE - Advanced ActionText: Attaching any Model in rich text by Chris Oliver',
+ 'author_name' => 'Confreaks',
+ 'author_url' => 'https://www.youtube.com/@Confreaks',
+ 'type' => 'video',
+ 'height' => 113,
+ 'width' => 200,
+ 'version' => '1.0',
+ 'provider_name' => 'YouTube',
+ 'provider_url' => 'https://www.youtube.com/',
+ 'thumbnail_height' => 360,
+ 'thumbnail_width' => 480,
+ 'thumbnail_url' => 'https://i.ytimg.com/vi/2iGBuLQ3S0c/hqdefault.jpg',
+ 'html' => "<iframe width=\"200\" height=\"113\" src=\"https://www.youtube.com/embed/2iGBuLQ3S0c?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen title=\"RailsConf 2020 CE - Advanced ActionText: Attaching any Model in rich text by Chris Oliver\"></iframe>"
+ }
+ )
+ end
+
+ describe 'after_create :create_video_embed' do
+ subject { create(:video_page_section, preferred_youtube_video_url: url) }
+
+ let(:url) { 'https://www.youtube.com/watch?v=2iGBuLQ3S0c' }
+ let(:video_embed) { subject.video_embed }
+
+ context 'when the URL is valid' do
+ before do
+ allow(OEmbed::Providers).to receive(:get).with(url).and_return(valid_oembed_response)
+ end
+
+ it 'creates the video embed attachment' do
+ expect { subject }.to change(ActionText::VideoEmbed, :count).by(1)
+
+ expect(subject.preferred_youtube_video_url).to eq(url)
+ expect(subject.preferred_youtube_video_embed_id.to_s).to eq(video_embed.id.to_s)
+
+ expect(video_embed).to be_present
+ expect(video_embed.url).to eq(url)
+ expect(video_embed.thumbnail_url).to include('2iGBuLQ3S0c')
+
+ expect(video_embed.raw_html).to start_with('<iframe')
+ expect(video_embed.raw_html).to include('src="https://www.youtube.com/embed/2iGBuLQ3S0c?feature=oembed"')
+ end
+ end
+
+ context 'when the URL is invalid' do
+ before do
+ allow(OEmbed::Providers).to receive(:get).with(url).and_raise(OEmbed::Error)
+ end
+
+ it 'creates no video embed attachment' do
+ expect { subject }.not_to change(ActionText::VideoEmbed, :count)
+
+ expect(subject.preferred_youtube_video_url).to eq(url)
+ expect(subject.preferred_youtube_video_embed_id).to be_nil
+
+ expect(video_embed).to be_nil
+ end
+ end
+ end
+
+ describe 'after_update :update_video_embed' do
+ subject { video_page_section.update(preferred_youtube_video_url: url_2) }
+
+ let(:video_page_section) { create(:video_page_section, preferred_youtube_video_url: url_1) }
+
+ let(:url_1) { 'https://www.youtube.com/watch?v=2iGBuLQ3S0c' }
+ let(:url_2) { 'https://www.youtube.com/watch?v=2iGBuLQ3S0d' }
+
+ let(:video_embed) { video_page_section.reload.video_embed }
+
+ let(:valid_oembed_response_2) do
+ double(
+ OEmbed::Response::Video,
+ fields: {
+ 'title' => 'RailsConf 2020 CE - Advanced ActionText: Attaching any Model in rich text by Chris Oliver',
+ 'author_name' => 'Confreaks',
+ 'author_url' => 'https://www.youtube.com/@Confreaks',
+ 'type' => 'video',
+ 'height' => 113,
+ 'width' => 200,
+ 'version' => '1.0',
+ 'provider_name' => 'YouTube',
+ 'provider_url' => 'https://www.youtube.com/',
+ 'thumbnail_height' => 360,
+ 'thumbnail_width' => 480,
+ 'thumbnail_url' => 'https://i.ytimg.com/vi/2iGBuLQ3S0d/hqdefault.jpg',
+ 'html' => "<iframe width=\"200\" height=\"113\" src=\"https://www.youtube.com/embed/2iGBuLQ3S0d?feature=oembed\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share\" referrerpolicy=\"strict-origin-when-cross-origin\" allowfullscreen title=\"RailsConf 2020 CE - Advanced ActionText: Attaching any Model in rich text by Chris Oliver\"></iframe>"
+ }
+ )
+ end
+
+ context 'when the URL is valid' do
+ before do
+ allow(OEmbed::Providers).to receive(:get).with(url_1).and_return(valid_oembed_response)
+ allow(OEmbed::Providers).to receive(:get).with(url_2).and_return(valid_oembed_response_2)
+
+ video_page_section
+ end
+
+ it 'updates the video embed attachment' do
+ subject
+
+ expect(video_page_section.reload.preferred_youtube_video_url).to eq(url_2)
+ expect(video_page_section.preferred_youtube_video_embed_id.to_s).to eq(video_embed.id.to_s)
+
+ expect(video_embed).to be_present
+ expect(video_embed.url).to eq(url_2)
+ expect(video_embed.thumbnail_url).to include('2iGBuLQ3S0d')
+
+ expect(video_embed.raw_html).to start_with('<iframe')
+ expect(video_embed.raw_html).to include('src="https://www.youtube.com/embed/2iGBuLQ3S0d?feature=oembed"')
+ end
+ end
+
+ context 'when the URL is invalid' do
+ before do
+ allow(OEmbed::Providers).to receive(:get).with(url_1).and_return(valid_oembed_response)
+ allow(OEmbed::Providers).to receive(:get).with(url_2).and_raise(OEmbed::Error)
+
+ video_page_section
+ end
+
+ it 'updates the page section with no video embed attachment' do
+ expect { subject }.to change(ActionText::VideoEmbed, :count).by(-1)
+ expect(video_embed).to be_nil
+ end
+ end
+ end
+end
diff --git a/core/spree_core.gemspec b/core/spree_core.gemspec
index 1c6d4efbd1..b9c00f4509 100644
--- a/core/spree_core.gemspec
+++ b/core/spree_core.gemspec
@@ -39,6 +39,7 @@ Gem::Specification.new do |s|
s.add_dependency 'cancancan', '~> 3.2'
s.add_dependency 'countries'
s.add_dependency 'friendly_id', '~> 5.2', '>= 5.2.1'
+ s.add_dependency 'geocoder'
s.add_dependency 'highline', '>= 2', '< 4' # Necessary for the install generator
s.add_dependency 'kaminari', '~> 1.2'
s.add_dependency 'money', '~> 6.13'
@@ -47,6 +48,7 @@ Gem::Specification.new do |s|
s.add_dependency 'paranoia', '>= 2.4'
s.add_dependency 'ransack', '>= 4.1'
s.add_dependency 'rexml'
+ s.add_dependency 'ruby-oembed', '~> 0.18'
s.add_dependency 'state_machines-activerecord', '~> 0.6'
s.add_dependency 'state_machines-activemodel', '~> 0.7'
s.add_dependency 'stringex'
@@ -62,5 +64,4 @@ Gem::Specification.new do |s|
s.add_dependency 'request_store', '~> 1.7'
s.add_dependency 'wannabe_bool'
s.add_dependency 'any_ascii', '~> 0.3.2'
- s.add_dependency 'geocoder'
end
diff --git a/spree.gemspec b/spree.gemspec
index f6ad8e7ffe..3ddd97eec8 100644
--- a/spree.gemspec
+++ b/spree.gemspec
@@ -7,7 +7,7 @@ Gem::Specification.new do |s|
s.version = Spree.version
s.authors = ['Sean Schofield', 'Spark Solutions Sp. z o.o.', 'Vendo Connect Inc.']
s.email = '[email protected]'
- s.summary = 'Headless API-first open source eCommerce platform'
+ s.summary = 'A complete open source e-commerce solution'
s.description = 'A complete open source e-commerce solution with multi-store, multi-currency and multi-language capabilities'
s.homepage = 'https://spreecommerce.org'
s.licenses = ['AGPL-3.0-or-later', 'BSD-3-Clause']